Link to home
Start Free TrialLog in
Avatar of Ida_fan

asked on

sscanf - reading from a file

How can I read the following 7 fields from an input file?

  Stud_no  Stud_name               Labs
1 12345678 John Smith               12

Ass1  Ass2  Exam
12.5  14.5  8.5

Does the following work?

sscanf(buf,"[^ ] %8d %20s %5f%5f%5f%5f", &std_no, &name, &lab, &ass1, &ass2, &exam)

What if one of the columns in the file is left empty, will the above sscanf statementstill work?
Avatar of ozo
Flag of United States of America image

char *buf="1 12345678 John Smith               12\n12.5  14.5  8.5 ";
sscanf(buf,"%*[^ ] %8d %20c %5f%5f%5f%5f", &std_no, &name, &lab, &ass1, &ass2, &exam);
printf("1 %d %20s %f\n%f %f %f\n",std_no,name,lab,ass1,ass2,exam);
/* if one of the columns is left empty the above sscanf statement won't work */
Avatar of Ida_fan


Is there anything that can be used to skip the column if it is left empty?
Avatar of Ida_fan


What I meant was how to scan the file and store the field values into corresponding variables if any field is left empty?

That is, to ensure that the first floating point value will be assigned to lab and the second, if empty, will still have a null value to ass2 etc...
Are the fields of fixed length?
Ida_fan: Ozo has a good point. If the fields *aren't* of fixed length, how will you tell if a column is empty anyway? You'll parse to the end of the line and suddenly realise you're one item short, but there's no way to tell which one is missing.
Avatar of sburck

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sscanf() also returns the number of items it has read.  Therefore you can do some error checking on what is returned to see if the amount read in was correct.

For instance if you had a line in a file that looked like the following:

1  John Smith 100 89

and assuming you have read in the line from the file into a character buffer named buf, then you would use the following sscanf() code:


int n;

int firstNum, secondNum, thirdNum;
char firstName[100];
char secondName[100];

n = sscanf(buf, "%d %s %s %d %d",
                 &firstNum, firstName, secondName, &secondNum, &thirdNum);

if (n != 5) {
    printf("ERROR: Format of line is incorrect!\n");
    return 0;

Avatar of Ida_fan


The empty fields will have to give null values to the variables.  But how to achieve it, this is what I'm after.
As stated from the previous posts, it would be difficult to do what you want to do with sscanf().  I think the best way is to simply ignore a faulty input line (or report the error).  You can detect a faulty line with the return value from sscanf().

Use the format

program_name: Error: Line %d of \"%s\" is missing one or more fields.

to make sure the user can tell which program is reporting the error, which line number has the error, and which input file is being read.  Give the user as much information as you know.
Avatar of Ida_fan


Comment accepted as answer