C language - Fscanf and sprint commands in unix environment

I am trying to read a file with 30 rows and 5 columns with a tab separator. Each time I get only part of the lines. In a windowed environment, it works well. Any idea why it doesn't work on unix?

while (fscanf(FFMapFile, "%s\t%s\t%s\t%s\t%s\t", fnfMap[i].COS_ID, fnfMap[i].FF_First_Act, fnfMap[i].FF_Next_Act, nfMap[i].Free_FF_allowed, fnfMap[i].FF_Change_Charge) != EOF)
{ 
    sprintf(s,"%s\t%s\t%s\t%s\t%s\t", nfMap[i].COS_ID, fnfMap[i].FF_First_Act, fnfMap[i].FF_Next_Act, fnfMap[i].Free_FF_allowed, fnfMap[i].FF_Change_Charge);
    error_log(s,ERROR);
    i++; }
+3
source share
3 answers

Check the return value more carefully fscanf(); it will return, for example, 3 if it matches 3 fields. You might want to be careful about the size of the various lines in order to avoid buffer overflows, that is, probably for another day.

, , , , fscanf() .

( , ) MacOS X.

#include <stdio.h>

int main(void)
{
    char b1[20], b2[20], b3[20], b4[20], b5[20];
    while (fscanf(stdin,"%s\t%s\t%s\t%s\t%s\t", b1, b2, b3, b4, b5) == 5)
        printf("%s\t%s\t%s\t%s\t%s\n", b1, b2, b3, b4, b5);
    return 0;
}

'k k k k k' (5 , ) 5 . , Β§7.19.6.2 C :

: , (%, ), .

( isspace) , [, c n.

, '% s', :

.

, . " (fgets()) ". 5 , , . fscanf() . 8 , , ..

0

\t fscanf() - - , "% s% s% s% s" - , scanf -. , , fscanf , EOF, , .

+1

fscanf . .

fscanf, , .

0

Source: https://habr.com/ru/post/1748968/


All Articles