好的,我试图用我的信息读取文本文件。文本文件内容如下:
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
CODE
#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 :(得分:2)
您可能会溢出缓冲区并覆盖'\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;
}
不应该失败。
答案 1 :(得分:1)
在整合了@ ihorob的解决方案后,我得到了一个正常的代码。
非常感谢。
增加:
删除#include <unistd.h>
,在此示例中不需要它,并导致其与Windows操作系统的相关性存在疑问。
以下是代码:
#include <stdio.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[])
{
float gallons = 0.0;
int element = 0;
char id[10] = { 0 }, odometer[10] = { 0 };
FILE *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);
return(0);
}