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:
This is the databaseFile.txt: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; }
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?



LinkBack URL
About LinkBacks


