gpt4 book ai didi

c - 在C中读取和打印文件

转载 作者:太空宇宙 更新时间:2023-11-04 07:10:43 27 4
gpt4 key购买 nike

好的,我正在尝试读取包含我的信息的文本文件。文本文件内容是这样的:

gasData.txt

0 987654 201200 4.000000
1 red 89114 0.000000
2 red 89712 13.500000
3 red 90229 15.300000
4 987654 201001 0.000000
5 987654 201111 5.200000
6 987654 201612 25.299999
7 red 89300 7.100000
8 green 16 0.000000
9 green 216 20.000000
10 green 518 61.000000
11 green 879 50.000000

代码

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

#define N 12

struct record
{
char id[7];
int odometer;
float gallons;
};
typedef struct record record_t;

record_t gasData[N];

int main(int argc, char *argv[])
{

FILE *fileInput;
float gallons;
int element;
char id[10], odometer[10];

fileInput = fopen("gasData.txt", "r");

for(element = 0; element < N; element++)
{
fscanf(fileInput, "%s %s %f", id, odometer, &gallons);

/*if (feof(fileInput))
{
printf("end-of-file detected on file in\n");
exit(1);
}*/

printf("element = %d:, id = %s, odometer = %s, gallons = %f\n", element, id, odometer, gallons);
}

exit (0);
}

输出

element = 0:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 1:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 2:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 3:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 4:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 5:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 6:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 7:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 8:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 9:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 10:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000
element = 11:, id = (, odometer = ₧j↑u►↓@, gallons = 0.000000

我说的是废话,这是我的程序输出。唯一有效的是元素,但这只是循环的计数,所以没有问题。此外,当我取消注释我的文件结束循环时,我的程序崩溃了。抱歉,我不知道如何编辑列表。在此先感谢您的帮助。

编辑代码

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

#define N 12

/*struct record
{
char id[7];
int odometer;
float gallons;
};
typedef struct record record_t;

record_t gasData[N];*/

int main(int argc, char *argv[])
{

FILE *fileInput;
float gallons;
int element;
char id[10]; char odometer[10];

fileInput = fopen("gasData.txt", "r");
if (fileInput == NULL)
return -1;

for(element = 0; element < N; element++)
{
if (fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons) == 3)
{
printf("element = %d:, id = %s, odometer = %s, gallons = %f\n",
element, id, odometer, gallons);
}
}

exit (0);
}

最佳答案

您可能会溢出缓冲区并覆盖 '\0' 终止符试试这个

fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons);

同时,检查 fscanf() 是否成功,否则值将未初始化并且会发生同样的事情

if (fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons) == 3)
{
printf("element = %d:, id = %s, odometer = %s, gallons = %f\n",
element, id, odometer, gallons);
}

不要假设事情会很顺利,还要检查 fopen() 没有失败

fileInput = fopen("gasData.txt", "r");
if (fileInput == NULL)
return -1;

这段代码

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

#define N 12

struct record
{
char id[7];
int odometer;
float gallons;
};
typedef struct record record_t;

record_t gasData[N];

int main(int argc, char *argv[])
{

FILE *fileInput;
float gallons;
int element;
char id[10];
char odometer[10];

fileInput = fopen("gasData.txt", "r");
if (fileInput == NULL)
{
fprintf(stderr, "cannot open `gasData.txt'\n");
return -1;
}
for(element = 0; element < N; element++)
{
if (fscanf(fileInput, "%9s%9s%f", id, odometer, &gallons) == 3)
{
printf("element = %d:, id = %s, odometer = %s, gallons = %f\n",
element, id, odometer, gallons);
}
}

return 0;
}

不应该失败。

关于c - 在C中读取和打印文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28647791/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com