【发布时间】:2015-03-06 03:36:24
【问题描述】:
我正在尝试编写一个程序,该程序从文本文件中读取一个字符和一个浮点数,然后将这些值保存到一个变量中。 文本文件以这种方式格式化:
S 15.24
G 28.00
S 56.90
H 90.00
0
我正在尝试读取字符,然后让它通过一个开关运行,根据浮点数之前的哪个字符进行一些计算(所有 S 值组合在一起,所有 G 值组合在一起,等等)
为了让它遍历所有可用的行,我使用了一个 while 循环,当读取的字符到达列表末尾的 0 时终止。
仅出于测试目的,我让程序在退出循环时打印“完成”。但是,当我运行程序时,它会遍历每个字符并将其正确打印到终端,直到它打印 0 之前的值。一旦到达 0,程序不会执行任何其他操作,但不会打印“完成” “就像我认为的那样。
这是我正在使用的代码:
int main()
{
int s_num,g_num,h_num,S,H,G,n_total;
float amount,s_total,s_sum,g_sum,h_sum;
char char_name;
FILE*Input;
Input = fopen("Input.txt","r");
FILE*Output;
Output = fopen("Output.txt","w+");
char_name = 1; //Just using 1 as a way to make the while loop occure
while(char_name != '0')
{
fscanf(Input,"%c%f",&char_name,&amount);
switch(char_name)
{
case 'S':
printf("S,%f\n",amount);
break;
case 'G':
printf("G,%f\n",amount);
break;
case 'H':
printf("H,%f\n",amount);
break;
}
}
printf("done");
return 0;
}
(忽略未使用的值,将在实际程序中使用)
一旦循环达到 0,我需要进行哪些更改才能终止循环?
【问题讨论】:
-
尝试用
fscanf(Input,"%c%f%*c",&char_ name,&amount);或fscanf(Input,"%c",&char_ name);fscanf(Input,"%f%*c",&amount);替换fscanf(Input,"%c%f",&char_ name,&amount); -
第一个似乎奏效了。你能解释一下 *c 的意义吗?
-
%*c与%c做同样的事情,只是它不会将结果存储在任何地方。%*c扫描一个字符并丢弃它。在您的情况下,它将扫描每行末尾的换行符并将其丢弃。
标签: c while-loop switch-statement