c - Using fscanf to scan a value or use default if no value exists -


i have function read text file following format

 string int int  string int int  string int int 

i want write function assign values text file variables, there cases format of text file be

string int string int string int 

in case, i'd set value of last int variable 1. code have far works first example i'm bit stuck on getting second scenario work:

void readfile(linkedlist *inlist, char* file)     {     char tempname[30];     int temploc, tempnum;      file* f;     f = fopen(file, "r");     if(f==null)          {         printf("error: not open file");         }     else         {         while (fscanf(f, "%s %d %d\n", tempname, &temploc, &tempnum) != eof)             {             insertfirst (inlist, tempname, temploc, tempnum);             }         }        } 

in second case, fscanf return 2 instead of 3. can rewrite code this:

while (1) {     int ret = fscanf(f, "%s %d %d\n", tempname, &temploc, &tempnum);     if (ret == eof) {         break;     }     if (ret == 2) {         tempnum = 1;     } else if (ret != 3) {         // line appear invalid, deal error     }     insertfirst (inlist, tempname, temploc, tempnum); 

}

a more hacky way set tempnum 1 before calling fscanf , check eof did above. think code above clearer.

edit: avoid overflows, better. code perform better harder write. above, did not write code error conditions want handle them

char linebuf[255]; while (fgets(linebuf, sizeof(linebuf), f) != null) {     int spaceidx, ret;     const int len = strlen(linebuf);     if (len == (sizeof(linebuf) - 1) {          // line long - either buf small , should tell user          // input bad          // recommend treat error     }     linebuf[len - 1] = '\0'; // remove \n     --len;  // update len, we've removed 1 character     if (isspace(*linebuf)) {         // error, line should not start space     }     spaceidx = strcspn(linebuf, "\t ");     if (spaceidx == len) {         // error, no space in line     }      // ok, we've found space.  deal rest.     // note purpose, sscanf bit heavy handed (but makes code     // simpler). strtol.     // also, first space in format string important, sscanf skips      // space @ beginning of string.  if format requires     // 1 space between fields, can sscanf(linebuf + spaceidx + 1, "%d %d"...      ret = sscanf(linebuf + spaceidx, " %d %d", &temploc, &tempnum);     if (0 == ret) {         // error, no ints     }     else if (1 == ret) {         tempnum = 1;     }      // @ point, copy first part of linebuf tempname,     // have deal potential overflow (and spend time on useless copy),     // use linebuf instead      linebuf[spaceidx] = '\0';      insertfirst (inlist, linebuf, temploc, tempnum); } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -