代码之家  ›  专栏  ›  技术社区  ›  MomoDevi

将数据从文件读取到结构数组(C)

  •  2
  • MomoDevi  · 技术社区  · 8 年前

    我正在尝试构建一个简单的函数,它将接收一个数据文件,并将数据文件中的各种值分配到一个全局结构数组中。然而,我很难让它正常工作。我已经编写了我认为最需要的代码,但我的测试行 printf("time is %d\n", BP[i].time); 简单地读出“Time is 0.”10次,让我相信这些值并没有像我想象的那样分配给结构数组。

    我该如何进一步?

    示例数据文件(.txt):

    0001    553    200
    0002    552    100
    ....    ...   ...
    

    当前代码:

    #include <stdio.h>
    #include <stdlib.h>
    
    // Function Prototype
    void readFileBP(char fileName[1000]);
    
    // Definition of BP Structure
    struct bloodPressure
    {
        int *time;
        int *sys;
        int *dia;
    }BP[50]; // end struct BP
    
    int main()
    {
        char fileName[1000] = "C:\\Users\\User\\Desktop\\DataFiles\\BP_1.txt";
        readFileBP(fileName);
    
        int i = 0;
    
        for (i; i<10; i++)
        {
            printf("Time is %d\n", BP[i].time);
        }
    } // end int main()
    
    void readFileBP(char fileName[1000])
    {
        FILE *filePtr; // declare file pointer
        int time;
        int sys;
        int dia;
        int position = 0;
    
    
        if (filePtr = fopen(fileName, "r") == NULL) // error check opening file
        {
            printf("Opening file failed. Please reenter filename.");
            exit(1); 
        } // end if
    
        while (fscanf(filePtr, "%d, %d, %d", &time, &sys, &dia) != EOF) // read in BP values
        {
            BP[position].time = time;
            BP[position].sys = sys;
            BP[position].dia = dia;
            position++;
    
        } // end while
    
        fclose(filePtr);
    
    
    
    } // end void readFile()
    
    3 回复  |  直到 8 年前
        1
  •  1
  •   gsamaras    8 年前

    编译时启用警告。你应该得到这样的东西:

    gsamaras@gsamaras-A15:~$ gcc -Wall -o px px.c
    px.c: In function ‘main’:
    px.c:22:5: warning: statement with no effect [-Wunused-value]
         for (i; i<10; i++)
         ^
    px.c:24:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
             printf("Time is %d\n", BP[i].time);
             ^
    px.c: In function ‘readFileBP’:
    px.c:37:17: warning: assignment makes pointer from integer without a cast [enabled by default]
         if (filePtr = fopen(fileName, "r") == NULL) // error check opening file
                     ^
    px.c:37:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         if (filePtr = fopen(fileName, "r") == NULL) // error check opening file
         ^
    px.c:45:27: warning: assignment makes pointer from integer without a cast [enabled by default]
             BP[position].time = time;
                               ^
    px.c:46:26: warning: assignment makes pointer from integer without a cast [enabled by default]
             BP[position].sys = sys;
                              ^
    px.c:47:26: warning: assignment makes pointer from integer without a cast [enabled by default]
             BP[position].dia = dia;
                              ^
    px.c: In function ‘main’:
    px.c:26:1: warning: control reaches end of non-void function [-Wreturn-type]
     } // end int main()
     ^
    

    这不足以让你开始吗?这是给我的!:)

        2
  •  1
  •   Arjun    8 年前

    我做了一些更改,并刚刚运行了它。

        #include <stdio.h>
        #include <stdlib.h>
    
    // Function Prototype
    void readFileBP(char fileName[1000]);
    
    // Definition of BP Structure
    struct bloodPressure
    {
        int time;
        int sys;
        int dia;
    }; // end struct BP
    struct bloodPressure BP[50];
    
    int main()
    {
        char *fileName = "file.txt";
        readFileBP(fileName);
        int i = 0;
        for (i; i<10; i++)
        {
            printf("Time is %d\n", BP[i].time);
        }
        getch();
    }
    
    void readFileBP(char fileName[1000])
    {
        FILE *filePtr; // declare file pointer
        int time=0;
        int sys=0;
        int dia=0;
        int position = 0;
        filePtr= fopen(fileName,"r");
        while (fscanf(filePtr, "%d, %d, %d", &time, &sys, &dia) != EOF) // read in BP values
        {
            BP[position].time = time;
            BP[position].sys = sys;
            BP[position].dia = dia;
            position++;
    
        } // end while
    
        fclose(filePtr);
    } // end void readFile()
    

    现在的输出是:

    Time is 1
    Time is 553
    Time is 200
    Time is 2
    Time is 552
    Time is 100
    Time is 0
    Time is 0
    Time is 0
    Time is 0
    
        3
  •  0
  •   sk1984    8 年前

    尝试更改行:

    while (fscanf(filePtr, "%d, %d, %d", &time, &sys, &dia) != EOF)
    

    while (fscanf(filePtr, "%d%d%d", &time, &sys, &dia) != EOF)
    

    这也是我尝试过的,并且基于我所做的测试,它似乎有效

    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_ARRAY_SIZE 50
    
    typedef struct BloodPressure
    {
        int time;
        int sys;
        int dia;
    }BloodPressure;
    
    BloodPressure bloodPressure[MAX_ARRAY_SIZE];
    
    void ReadFile(char *fileName);
    
    int main(int argc, char *argv[])
    {
        char *fileName = "BP_1.txt";
    
        ReadFile(fileName);
    
        int i = 0;
    
        for (i = 0; i < MAX_ARRAY_SIZE; i++)
        {
            printf("Dia is : %d\n", bloodPressure[i].dia);
            printf("Sys is : %d\n", bloodPressure[i].sys);
            printf("Time is : %d\n", bloodPressure[i].time);
            printf("\n");
        }
    
        exit(EXIT_SUCCESS);
    }
    
    void ReadFile(char *fileName)
    {
        FILE *filePtr = NULL;
        int  i = 0;
    
        if ((filePtr = fopen(fileName, "r")) == NULL)
        {
            printf("Error : Unable to open %s for reading\n");
            exit(EXIT_FAILURE);
        }
    
        while (fscanf(filePtr, "%d%d%d", &bloodPressure[i].dia, &bloodPressure[i].sys, &bloodPressure[i].time) != EOF)
        {
            i++;
        }
    
        fclose(filePtr);
    }