Read Int & String from a File
Hi,
I'm having trouble reading an int and character string from a text file. They are separated by a single space.
For example the file would be formatted like this:
7 I like strings
844 This is another
string
49 A
third string
The string can contain newlines and whitespaces and I only have to store the first 300 characters. I'm only trying to scan one line.
I'm using the following variables.
char buffer[300];
char string[300];
int length;
FILE *fp;
I read from previous posts on this site that fscanf will not work properly with whitespace so I'm using fgets and then scanning that string for the int and string instead.
fgets(buffer, 300, fp);
sscanf(buffer, "%d %s", length, string);
The file is opened and closed properly. When I print buffer, it contains the correct data (both an integer and the string), but length and string do not. The program runs, but crashes at sscanf.
Thanks for your help.
Tim