在读像1,2,3,4这样的行时,如何在c中正确地fscanf?

在读像1,2,3,4这样的行时,如何在c中正确地fscanf?,c,file-io,input,output,C,File Io,Input,Output,终端中的输出变为:132714-275292160 32767 如何将其修复为:1234?您也需要读入整数之间的字符 void readFile(void) { FILE *file; int i; char line[100]; int testnr, testnr2, testnr3, testnr4; //testnr = 1; //testnr2 = 2; //testnr3 = 3; //testnr4 = 4;

终端中的输出变为:
132714-275292160 32767


如何将其修复为:
1234

您也需要读入整数之间的字符

void readFile(void) {

    FILE *file;    
    int i;
    char line[100];
    int testnr, testnr2, testnr3, testnr4;
    //testnr = 1;
    //testnr2 = 2;
    //testnr3 = 3;
    //testnr4 = 4;

    file = fopen("test.txt","r");

    fgets(line,500,file); // does not do anything with the first line

    // read in 1,2,3,4
    fscanf(file, "%i %i %i %i", &testnr, &testnr2, &testnr3, &testnr4);      
    printf("%i %i %i %i \n", testnr, testnr2, testnr3, testnr4);    

    fclose(file);
}               
还要注意,
fscanf
返回成功读取的次数。所以你可以做:

fscanf( file, "%i,%i,%i,%i", &nr, &nr2, &nr3, &nr4 );

您还需要读入整数之间的字符

void readFile(void) {

    FILE *file;    
    int i;
    char line[100];
    int testnr, testnr2, testnr3, testnr4;
    //testnr = 1;
    //testnr2 = 2;
    //testnr3 = 3;
    //testnr4 = 4;

    file = fopen("test.txt","r");

    fgets(line,500,file); // does not do anything with the first line

    // read in 1,2,3,4
    fscanf(file, "%i %i %i %i", &testnr, &testnr2, &testnr3, &testnr4);      
    printf("%i %i %i %i \n", testnr, testnr2, testnr3, testnr4);    

    fclose(file);
}               
还要注意,
fscanf
返回成功读取的次数。所以你可以做:

fscanf( file, "%i,%i,%i,%i", &nr, &nr2, &nr3, &nr4 );

旁注:
char行[100]。。。fgets(第500行,文件)是一个问题。建议
fgets(行、行大小、文件)-更安全。旁注:
字符行[100]。。。fgets(第500行,文件)是一个问题。建议
fgets(行、行大小、文件)-更安全。