c++ File input/output
- by Myx
Hi:
I am trying to read from a file using fgets and sscanf. In my file, I have characters on each line of the while which I wish to put into a vector. So far, I have the following:
FILE *fp;
fp = fopen(filename, "r");
if(!fp)
{
fprintf(stderr, "Unable to open file %s\n", filename);
return 0;
}
// Read file
int line_count = 0;
char buffer[1024];
while(fgets(buffer, 1023, fp))
{
// Increment line counter
line_count++;
char *bufferp = buffer;
...
while(*bufferp != '\n')
{
char *tmp;
if(sscanf(bufferp, "%c", tmp) != 1)
{
fprintf(stderr, "Syntax error reading axiom on "
"line %d in file %s\n", line_count, filename);
return 0;
}
axiom.push_back(tmp);
printf("put %s in axiom vector\n", axiom[axiom.size()-1]);
// increment buffer pointer
bufferp++;
}
}
my axiom vector is defined as vector<char *> axiom;. When I run my program, I get a seg fault. It happens when I do the sscanf. Any suggestions on what I'm doing wrong?