从文件扫描
我目前正在尝试从文件中扫描一行,但在字符串上遇到了障碍。 这是我的教授告诉我要研究的示例行。
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);
但是如何将字符串转换为整数或浮点数?
I am currently trying to scan a single line in from a file but having a snag at strings.
This is the example line my professor told me to work on.
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
I'm fine with everything accept at the point (MEM Cuba Christmas) when I'm scanning it in from a FILE. I read the first part of the data just using fscanf()
, but MEM is a enumerated type with a union type that dictates the following input. My problem is with the syntax of the scanning. I tried using getline starting at MEM but I hit snags with the tokenizing since the city / country can have spaces. Not sure what other scans to use I was looking at sscanf()
but wasn't sure if it works with files.
UPDATED:
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);
}
}
This was the code I was using as a base that I came up with but not sure how to handle the enumerated and union. I was thinking of doing something like:
getline(&status, &length, file_point);
but how do I convert string to integer or float?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你的问题(我不确定我是否理解),那么你将面临将“MEM”(或“PREP”或“TRAV”)视为输入中的字符串的问题,并且你必须了解如何处理以下数据。
enum
建议您可能希望将字符串 MEM 转换为枚举中的 MEM 值。很难完全自动化这种转换。最简单的方法是识别字符串并根据字符串决定要执行的操作:
但是,您可以创建一个结构来处理从字符串到枚举值的转换。
您需要比“错误退出”更好的错误处理策略。
您的扫描代码需要读取单词,然后调用
state_conversion()
。然后,根据您返回的内容,您可以按照给定状态的正确方式读取剩余(以下)数据。If I understand your problem properly (I'm not sure I do), then you face the problem of seeing 'MEM' (or 'PREP' or 'TRAV') as a string in the input, and you have to understand how to handle the following data. The
enum
suggests that you might want to convert the string MEM to the value of MEM in the enumeration.It is hard to fully automate such a conversion. It would be simplest simply to recognize the strings and decide what to do based on the string:
However, you can create a structure to handle the conversion from string to enumeration value.
You need a better error handling strategy than 'exit on error'.
Your scanning code will need to read the word, and then call
state_conversion()
. Then depending on what you get back, you can read the remaining (following) data in the correct way for the state you were given.不,你不能按照你正在尝试的方式做到这一点。文件中的 MEM 是字符串类型,您需要像解析字符串一样解析它,然后根据该字符串设置枚举的值。
例如,当你想解析你的状态类型(MEM,PREP,TRAV)时:
然后手动比较typeBuffer的内容:
string类型和enum之间的转换不能是隐式的。
No, you can't do that in the way you are trying. MEM in your file is a string type, you need to parse it like you parse a string and then set the value of your enum according to that string.
For example, when you want to parse your status type (MEM,PREP,TRAV):
Then manually compare the content of typeBuffer:
The conversion between string type and enum cannot be implicit.