-
More Reading in problems
Ok I'm having problems reading in a file again. Its the same CD Database but I decided to add a track listing to it. So, since different cds have different numbers of tracks, I added a loop in there to read in the tracks until it reads in a string that I compare to break it out of the loop. However, everything seems to be working fine except I get a segmenation fault. I tried messing around with cout's and found that everything works fine but it wont print my last cout once its supposed to break out of the loop once it reaches the end of the file, meaning it most not be reaching the end of the file.
I don't get it... Here is the code:
Code:
int main()
{
ifstream infile;
char* line = new char[512];
char* temp = new char[100];
char* garbage = new char[50];
char* artist = new char[50];
char* title = new char[50];
char* year = new char[50];
char* song1 = new char[50];
char test[7] = "------";
infile.open("databaseFile.txt");
while ( infile.peek() != EOF )
{
infile.getline(garbage,100);
infile.getline(line,512);
temp = strtok(line, " ");
strcpy(garbage,temp);
temp = strtok(NULL, "\n");
strcpy(artist, temp);
cout << "Artist: " << artist << endl;
infile.getline(line,512);
temp = strtok(line, " ");
strcpy(garbage,temp);
temp = strtok(NULL, "\n");
strcpy(title,temp);
cout << "Title: " << title << endl;
infile.getline(line,512);
temp = strtok(line, " ");
strcpy(garbage,temp);
temp = strtok(NULL, "\n");
strcpy(year,temp);
cout << "Year: " << year << endl;
infile.getline(line,512);
//reading in Track Lising:
temp = strtok(line, "\n"); strcpy(garbage,temp);
do
{
infile.getline(line,512);
temp = strtok(line, "\n");
strcpy(song1,temp);
cout << "Printing out song: ";
cout << song1 << endl;
} while ( strcmp (song1,test) != 0 );
//read in blank line
infile.getline(garbage,100);
}
cout << "After breaking out of reading file loop" << endl;
}
This is the databaseFile.txt:
Code:
--CD--
Artist: Outkast
Title: ATLiens
Year: 1996
Track Listing:
1. You May Die (Intro)
2. Two Dope Boys (In a Caddilac)
3. ATLiens
4. Wheelz of Steel
5. Jazzy Belle
6. Elevators (Me & You)
7. Ova Da Wudz
8. Babylon
9. Wailin'
10. Mainstream
11. Decatur Psalm
12. Millenium
13. E.T. (Extraterrestrial)
14. 13th Floor/Growing Old
15. Elevators (ONP 86 Mix)
------
--CD--
Artist: Outkast
Title: Aquemini
Year: 1998
Track Listing:
1. Hold On Be Strong
2. Return of the 'G'
3. Rosa Parks
4. Skew it on the Bar-B
5. Aquemini
6. Synthesizer
7. Slump
8. West Savannah
9. Da' Art of Storytellin (Part 1)
10. Da' Art of Storytellin (Part 2)
11. Mamacita
12. SpottieOttieDopalicious
13. Y'All Scared
14. Nathaniel
15. Liberation
16. Chonkyfire
------
It wont print the last cout that says After breaking out of reading file.
Anyone got any ideas?
-
Does the program just end? Or does it exit with some sort of error?
Also, you should free all that memory you allocated at the end of the program. Try running it through a debugger and see what's going on with the variables.
-
No it doesnt end at all. It has a segmentation fault before it breaks out of the reading in file loop:
Code:
while ( infile.peek() != EOF)
It never prints that last cout statement outside the loop.
I'm editing because I tried to check all the variables once it breaks outside the loop as you suggested. Comparing it to what the variables are before the loop I don't see anything. I don't think it's trying to strtok a NULL string.
Code:
Before entereing do while loop
Garbage: Track Listing:
Temp: Track Listing:
line: Track Listing:
Code:
After loop
Garbage: Track Listing:
Temp: ------
line: ------
song1: ------
test: ------
-
I was playing around with different kind of loops besides the do-while and my conclusion is I think the do/while (and when I try to convert it to a for and while loop) it runs one more time then I want it to.
Does anyone see that?
I don't know if thats causing the seg fault.
-
Hey swayp, check out my latest post on your last thread here:
http://cboard.cprogramming.com/showthread.php?t=61013
I hope that helps broaden your options
-
as I said before, you're probably passing a NULL pointer to strcpy(). try checking the return value of strtok() before calling strcpy().
-
>>it runs one more time then I want it to.
This sounds like a common problem when looking for end of file as the conditional of a loop. I'd try this:
Code:
while (infile.getline(garbage,100))
{
infile.getline(line,512);
//etc.
instead of this:
Code:
while ( infile.peek() != EOF )
{
infile.getline(garbage,100);
infile.getline(line,512);
//etc.