Read Int & String from a File
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
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.
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.