Hi, I'm trying to read from a file and I'm getting a segmentation fault, can someone help me with what I'm doing wrong:
My .txt file looks exactly like this:
Those years might not be right but whatever, its a test :)Code:-CD-
Artist: Outkast
Title: Stankonia
Year: 1997
-CD-
Artist: Jay Z
Title: Reasonable Doubt
Year: 1993
-CD-
Artist: Atmosphere
Title: Overcast
Year: 1995
Now this is my code:
The first part should ignore the first line because that just says "-CD-". Then I tried to strtok with the delimeter of " " and ignore that part because thats the part that says "Artist:", "Title: ", and "Year: ". Then I try to strtok with "\n" and save that as the actual artist, title, and year. I strtok'ed with "\n" because artists and titles could have a space in their name so if I strtok with the " " it will break it up. But I'm getting a segmentation fault and I put some cout's in there to find out how far it got and it dumps the core right after it strtok's the artist.Code:#include <iostream.h>
#include <fstream.h>
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];
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);
temp = strtok(NULL, " ");
strcpy(garbage,temp);
temp = strtok(NULL, "\n");
strcpy(title,temp);
temp = strtok(NULL, " ");
strcpy(garbage,temp);
temp = strtok(NULL, "\n");
strcpy(year,temp);
infile.getline(garbage,100);
}
}
Can someone please Help!