I have a file like this:
10 15
something
I want to read this into tree variables, let’s say number1, number2, and mystring. I have doubts about what kind of pattern to give to fscanf. I am thinking something like this;
fscanf(fp,"%i %i\n%s",number1,number2,mystring);
Should this work, and also, is this the correct way of reading this file? If not, what would you suggest?
Read each line with
fgets(orgetlineif you have it), split up the line withstrsep(better, if available) orstrtok_r(more awkward API but more portable), and then usestrtoulto convert strings to numbers as necessary.*scanfshould never be used, because:"%s") are just as eager to overflow your buffers asgetsis.fgetsand thensscanfinstead offscanf.)