为什么我的fscanf会一直跳过随机数?

时间:2014-11-19 04:23:38

标签: c file-io scanf

基本上我需要扫描这个文件中的所有值...它几乎可以做到,但它似乎跳过随机的值,所以它最终没有正确排列。 (这只是我需要为作业做的一部分,所以你不是“给出答案”而我仍然想要自己解决这个问题,只需要一点帮助就可以搞清楚这一点,因为我是相当困难)

https://www.dropbox.com/sh/wterbuyxm76wwrz/AADEtolX68OFrKELI_lxT8_Ra/assignments%20and%20labs/labs/lab5_inputFile.txt?dl=0

以下是代码:

int main()
{
    FILE *fp;
    char x[15];
    float ID [1000];
    int i=0, j=0;
    float homework [1000];
    float lab [1000];
    float midterm [1000];
    float Final [1000];
    int count=0;
    char headers[35];
    char y;

    fp= fopen("lab5_inputFile.txt", "r");

    while (fscanf(fp, "%s", x)!=EOF){
        if (count > 728){
            fscanf(fp, "%f", &ID[i]);
            printf("ID : %.1f\n", ID[i]);
            fscanf(fp, "%f", &homework[i]);
            printf("Homework: %.1f\n", homework[i]);
            fscanf(fp, "%f", &lab[i]);
            printf("lab: %.1f\n", lab[i]);
            fscanf(fp, "%f", &midterm[i]);
            printf("Midterm: %.1f\n", midterm[i]);
            fscanf(fp, "%f", &Final[i]);
            printf("Final: %.1f\n", Final[i]);

            i++;
        }
            count ++;

    }
    printf("count = %d\n", count);
    fclose(fp);

我只扫描了最后几个值,以便于阅读/调试,在实际代码中我只会跳过标题,还有很多其他的东西我打算在提交之前更改,就像我可能会使用的那样指针类型和malloc而不是数组和其他一些东西,但我的主要问题是如何解决我从文件读入数组时遇到的问题。

提前致谢!

3 个答案:

答案 0 :(得分:0)

您每次都会跳过第一行x。你应该把它重写为:

char x[100];
/* while (fscanf(fp, "%s", x)!=EOF){ */
fgets(x, 100, fp);  /* Skip first line */
for(; /* ever */; ) {
    if(5 == fscanf(fp, "%f%f%f%f%f", &ID[i], &homework[i], &lab[i], &midterm[i], &Final[i])) {
       printf("ID : %.1f\n", ID[i]);
       printf("Homework: %.1f\n", homework[i]);
       printf("lab: %.1f\n", lab[i]);
       printf("Midterm: %.1f\n", midterm[i]);
       printf("Final: %.1f\n", Final[i]);
       i++;
    } else break;
}

Live example here

答案 1 :(得分:0)

OP的方法重复读取“一组非空白文本,然后是5个数字”。代码应该读取一次,抛出它,然后重复读取5个数字组。

读取并折腾第一行,然后使用EOF读取行fgets()。使用sscanf()strtod()扫描缓冲区。

char buf[100];
fgets(buf, sizeof buf, fp);  // Read and toss first line
i = 0;

while (fgets(buf, sizeof buf, fp) != NULL) {  // Read until EOF
  if ((i >= 1000) || (sscanf(buf, "%f%f%f%f%f", &ID[i], &homework[i], &lab[i], 
      &midterm[i], &Final[i]) != 5)) {
    fputs("Quit as too many lines or ill formatted\n", stderr);
    break;
  }
  printf("ID : %.1f\n", ID[i]);
  printf("Homework: %.1f\n", homework[i]);
  printf("lab: %.1f\n", lab[i]);
  printf("Midterm: %.1f\n", midterm[i]);
  printf("Final: %.1f\n", Final[i]);
  i++;
}

答案 2 :(得分:0)

#include <stdio.h>
#include <stdlib.h>

struct grade
{
    float ID;
    float homework;
    float lab;
    float midterm;
    float final;
};

#define ARRAYSIZE (1000)

int main()
{
    FILE *fp;
    struct grade studentGrades[ARRAYSIZE] = {{0},{0},{0},{0},{0}};
    int i=0;
    int count=0;


    if( NULL == (fp= fopen("lab5_inputFile.txt", "r") ) )
    {
        perror( "fopen for read of lab5_inputFile.txt" );
        exit(1);
    }

    // implied else fopen successful

    char* dummy = malloc(1000*sizeof(char) );
    if( NULL == dummy )
    {
        perror( "malloc for dummy buffer" );
        exit(2);
    }

    // implied else, malloc successful

    char * firstLine = fgets( dummy, sizeof(dummy), fp ); // step past first line of file
    if( NULL == firstLine )
    {
        perror( "fget of first line of file" );
        exit(3);
    }

    // implied else, fgets successful

    free( dummy );

    int itemCount = 0;
    for ( i = 0; i < ARRAYSIZE; i++ )
    {
        itemCount = fscanf(fp, " %f", &studentGrades[i].ID);
        if( EOF == itemCount ) // Note:making assumption that every line, 
                               // after first, is properly formatted
        {
            break; // exit for loop
        }
        if( 1 != itemCount )
        {
            perror("fscanf");
            exit(4);
        }

        // implied else read of ID successful, not end of file

        printf("ID : %.1f\n", studentGrades[i].ID);

        if( 1 != fscanf(fp, " %f", &studentGrades[i].homework) )
        {
            perror( "fscanf of homework");
            exit(5);
        }

        // implied else, read of homework successful

        printf("Homework: %.1f\n", studentGrades[i].homework);

        if( 1 != fscanf(fp, " %f", &studentGrades[i].lab) )
        {
            perror( "fscanf of lab" );
            exit(6);
        }

        // implied else, read of lab successful

        printf("lab: %.1f\n", studentGrades[i].lab);

        if( 1 != fscanf(fp, " %f", &studentGrades[i].midterm) )
        {
            perror( "fscanf of midterm" );
            exit(7);
        }

        // implied else, read of midterm successful

        printf("Midterm: %.1f\n", studentGrades[i].midterm);

        if( 1 != fscanf(fp, " %f", &studentGrades[i].final) )
        {
            perror( "fscanf of final" );
            exit(8);
        }

        // implied else, read of final successful

        printf("Final: %.1f\n", studentGrades[i].final);

        count ++;

    } // end for

    printf("count = %d\n", count);
    fclose(fp);

    return(0);
}