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

Reading data from a file into a structure array (C)

我正在尝试构建一个简单的函数,它将接收一个数据文件,并将数据文件中的各种值分配给一个全局结构数组。但是,我无法让它正常工作。我已经编写了我认为大部分需要的代码,但我的测试行 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()

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

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()
 ^

这还不足以让您入门吗?这是给我的! :)

我做了一些修改 运行 刚刚。

    #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

尝试更改行:

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);
}