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

3
我试图构建一个简单的函数,它将接收一个数据文件,并将数据文件中的各种值分配到全局结构体数组中。然而,我无法使其正常运行。我已经编写了我认为大部分需要的代码,但我的测试行 printf("time is %d\n", BP[i].time); 只会输出"Time is 0.",这让我相信值没有像我想象的那样分配到结构体数组中。
我应该怎么继续?
示例数据文件(.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()

现在是学习如何使用调试器的绝佳机会。通过逐步执行代码来检查所有相关变量,看看到底发生了什么!:-) - alk
3
请注意这里两次出现的 (filePtr = fopen(fileName, "r") == NULL)。首先会发生什么? - alk
作为最基本的调试支持,您希望在循环内部打印读取的值。是否有任何内容被读取? - alk
@alk 感谢您帮我找出问题所在的那一行。就像您说的那样,我真的应该在循环中输出值。作为一个初学者,我不理解编译器给我的大部分警告(例如它“期望一个 int 类型的参数,但参数是 int * 类型”),但我设法看到了可能会将错误的东西赋给 filePtr。因此,我将打开文件的代码移到了 if 语句上面的一行。这样做虽然不太干净,但我认为可以完成同样的工作! - MomoDevi
3个回答

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 typeint’, but argument 2 has typeint *’ [-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
这本应该是一条注释,但显然太长了。所以点个赞吧。;-) - alk
1
经验教训:将编译器的警告级别调至最高,并修复代码,直到不再出现任何警告。如果仍然存在问题,请回到这里... - alk
1
@alk 说实话,我看到了你的评论,觉得应该让你来回答。但后来我想,我可以展示一下编译器对你的评论有何反应,并将其作为评论发布。结果发现这是一个相当广泛的问题,所以我认为这应该会引发 OP 的调试经验,再加上 gdb,获得最大的乐趣! :) - gsamaras
1
"... 为了最大的愉悦!" 很棒的措辞!-) - alk

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

你不想像这样盲目地转换类型来修复警告:BP[position].time = (int *)time;只需更正 struct bloodPressure 成员的定义以适合正确的类型。当前它将所有成员定义为指向 int 的指针,但这似乎不是你想要的。只需通过去掉星号使它们变成 int,因此 int * time 变为 int time - alk
@alk 当我第一次看到这段代码时,我确实考虑过这个问题,但后来我认为他们需要它作为int * time。但是,感谢您的建议...不再进行盲目转换。 - Arjun
@alk 答案现已编辑为 int time,而不是 int * time - Arjun

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

是的 - 我犯了一个括号错误,不过是在 if 语句中,而不是 while 语句中。你的代码:if((filePtr = fopen(fileName, "r")) == NULL) 和我的代码:if (filePtr = fopen(fileName, "r") == NULL) - MomoDevi
不用太担心,我也遇到过几次这种情况 :D - sk1984

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接