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

时间:2016-04-18 12:38:10

标签: c if-statement struct file-management

我正在尝试构建一个简单的函数,该函数将接收数据文件,并将数据文件中的各种值分配到全局结构数组中。但是,我很难让它正常工作。我已经写了我认为最需要的代码,但我的测试行printf("time is %d\n", BP[i].time);只是读出"时间是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 个答案:

答案 0 :(得分:1)

编译并启用警告。你应该得到类似的东西:

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

那还不足以让你入门吗?这对我来说! :)

答案 1 :(得分:1)

我做了一些更改并立即运行它。

    #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

答案 2 :(得分: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);
}