I have to parse a text file with 3 different data types. I want it to be saved in a structure array with three members. My text file look like this:
我必须解析具有3种不同数据类型的文本文件。我希望它保存在一个包含三个成员的结构数组中。我的文本文件如下所示:
A B 45.78965
A C 35.46731
B C 46.78695
The program that I'm reading it with is the following and it does not work. What am I doing wrong?
我正在阅读它的程序如下,它不起作用。我究竟做错了什么?
#include <stdio.h>
struct gra {
char from;
char to;
double w;
};
int main ()
{
FILE *fp = fopen("graph.txt", "r");
int i = 0;
while (!feof(fp)) {
fscanf(fp, "%[^\t]", &graph[i].from, &graph[i].to, &graph[i].w);
i++;
}
fclose(fp);
}
1 个解决方案
#1
2
One of your problems is that you're reading using %[^\t]
, which reads strings, and store the result to variables that are not character arrays (two characters and a double).
你的一个问题是你正在使用%[^ \ t]进行阅读,它会读取字符串,并将结果存储到非字符数组的变量中(两个字符和一个双精度数)。
Although it's not clear from your question, it seems that the lines of your input contain two characters and one real number separated by one tab character. If that is so, you should use the following fscanf
to read them:
虽然从您的问题中不清楚,但您的输入行似乎包含两个字符和一个由一个制表符分隔的实数。如果是这样,您应该使用以下fscanf来读取它们:
fscanf(fp, "%c\t%c\t%lf\n", &graph[i].from, &graph[i].to, &graph[i].w);
If you are not sure what exactly separates your fields and you want to allow any amount of white space in between and also extra white space in the beginning and end of the line, then use:
如果你不确定你的字段到底是什么区分,并且你想在它们之间允许任意数量的空白区域,并且在行的开头和结尾都有额外的空格,那么使用:
fscanf(fp, " %c %c%lf\n", &graph[i].from, &graph[i].to, &graph[i].w);
that is, use an extra space in the format before each "%c"
to explicitly skip white space.
也就是说,在每个“%c”之前使用格式的额外空格来显式跳过空格。
Your code has also a couple of other problems:
您的代码还有其他一些问题:
-
You are using
feof
to check for end of file. This will usually not work well if you're not reading the file character by character. Instead, you should check if yourfscanf
returned 3, that is, if it successfully read the three things that you wanted it to read.您正在使用feof来检查文件的结尾。如果您不是逐个字符地读取文件,这通常不会很好。相反,你应该检查你的fscanf是否返回3,也就是说,如果它成功读取了你希望它读取的三件事。
-
You are missing a definition of array
graph
.您缺少数组图的定义。
I'm adding the complete code that I'd write for doing the parsing:
我正在添加我为编写解析而编写的完整代码:
#include"stdio.h"
#define MAX 100
struct {
char from, to;
double w;
} graph[MAX];
int main ()
{
FILE *fp = fopen("graph.txt", "rt");
for (int i=0; i<MAX; i++)
if (fscanf(fp, " %c %c%lf\n", &graph[i].from, &graph[i].to, &graph[i].w) < 3)
break;
fclose(fp);
return 0;
}
#1
2
One of your problems is that you're reading using %[^\t]
, which reads strings, and store the result to variables that are not character arrays (two characters and a double).
你的一个问题是你正在使用%[^ \ t]进行阅读,它会读取字符串,并将结果存储到非字符数组的变量中(两个字符和一个双精度数)。
Although it's not clear from your question, it seems that the lines of your input contain two characters and one real number separated by one tab character. If that is so, you should use the following fscanf
to read them:
虽然从您的问题中不清楚,但您的输入行似乎包含两个字符和一个由一个制表符分隔的实数。如果是这样,您应该使用以下fscanf来读取它们:
fscanf(fp, "%c\t%c\t%lf\n", &graph[i].from, &graph[i].to, &graph[i].w);
If you are not sure what exactly separates your fields and you want to allow any amount of white space in between and also extra white space in the beginning and end of the line, then use:
如果你不确定你的字段到底是什么区分,并且你想在它们之间允许任意数量的空白区域,并且在行的开头和结尾都有额外的空格,那么使用:
fscanf(fp, " %c %c%lf\n", &graph[i].from, &graph[i].to, &graph[i].w);
that is, use an extra space in the format before each "%c"
to explicitly skip white space.
也就是说,在每个“%c”之前使用格式的额外空格来显式跳过空格。
Your code has also a couple of other problems:
您的代码还有其他一些问题:
-
You are using
feof
to check for end of file. This will usually not work well if you're not reading the file character by character. Instead, you should check if yourfscanf
returned 3, that is, if it successfully read the three things that you wanted it to read.您正在使用feof来检查文件的结尾。如果您不是逐个字符地读取文件,这通常不会很好。相反,你应该检查你的fscanf是否返回3,也就是说,如果它成功读取了你希望它读取的三件事。
-
You are missing a definition of array
graph
.您缺少数组图的定义。
I'm adding the complete code that I'd write for doing the parsing:
我正在添加我为编写解析而编写的完整代码:
#include"stdio.h"
#define MAX 100
struct {
char from, to;
double w;
} graph[MAX];
int main ()
{
FILE *fp = fopen("graph.txt", "rt");
for (int i=0; i<MAX; i++)
if (fscanf(fp, " %c %c%lf\n", &graph[i].from, &graph[i].to, &graph[i].w) < 3)
break;
fclose(fp);
return 0;
}