我目前正在尝试从文件中扫描单行,但在字符串处遇到障碍。 这是我的教授告诉我要处理的示例行。从文件扫描
enum status{MEM,PREP,TRAV}
union type { double int day, char* title, float cost}
13953 P 12 26 2011 1 5 2012 2 A 3.30 249.00 A 2.0 148.00 MEM Cuba Christmas 3 0 2 Sierra Del Rosario, Cuba
我很好,当我从文件中扫描它的时候接受的点(MEM古巴圣诞节)。我只是使用fscanf()
来读取数据的第一部分,但是MEM是枚举类型,具有指定以下输入的联合类型。我的问题是扫描的语法。我试着从MEM开始使用getline,但是由于城市/国家可能有空间,所以我在标记化方面遇到了困难。不知道还有什么其他的扫描我正在看sscanf()
,但不知道它是否适用于文件。
更新:
int main(void);
{
int m, length = 100;
char *word, file_name[100];
FILE *file_point
printf("Please enter file name with .txt extension:");
scanf("%s", file_name);
file_point = fopen(file_name,"r");
while (fscanf(file_point, "%d", &m) != EOF)
{
temp.dest_code = m;
fscanf(file_point, " %c %d %d %d %d %d %d %d",
&temp.area_code,
&temp.Smonth, &temp.Sday, &temp.Syear,
&temp.Emonth, &temp.Eday, &temp.Eyear,
&temp.leg_num);
for (n=0; n < temp.leg_num; n++)
{
fscanf(file_point," %c %f %f",
&temp.tleg[n].travel_type,
&temp.tleg[n].travel_time,
&temp.tleg[n].cost);
}
fscanf(file_point," %d %d %d ",
&temp.adult,
&temp.child,
&temp.infant);
temp_name = (char *)malloc(length + 1);
getline (&temp_name, &length, file_point);
word = strtok(temp_name, ",");
temp.dest_name=(char *)malloc(strlen(word)+1);
strcpy(temp.dest_name, word);
word = strtok(NULL, ",");
temp.dest_country=(char *)malloc(strlen(word)+1);
strcpy(temp.dest_country,word2);
printf("name:%s country:%s\n", temp.dest_name, temp.dest_country);
printf("adult:%d , child:%d , infant:%d \n", temp.adult, temp.child, temp.infant);
}
}
这是我使用的基础,我想到了,但不知道如何处理枚举和工会的代码。我正在考虑做类似于:
getline(&status, &length, file_point);
但我如何将字符串转换为整型或浮点型?
哦,忘了提及我正在从一个文件中扫描此单行文件 – 2011-05-09 00:24:50
您可以发布一些代码,向我们展示从现在开始您尝试做什么? – Heisenbug 2011-05-09 00:41:56
'enum'最后需要一个分号。 'union'最后还需要一个分号,逗号需要是分号。但是,如果您已经编译了代码,那么问题在于您没有复制和粘贴代码。 – 2011-05-09 05:35:32