[英]How to read string with spaces, integers and float values from a file in c?
[英]How to take a string with spaces from a file in C
main()
{
FILE *fin;
char line[50];
char exp[SIZE];
fin=fopen("prefix.txt","r");
if(fin==NULL)
{
printf("\nFile Cannot be Opened\n");
}
else
{
printf("\nfile opened\n");
while(fgets(line, sizeof(line), fin)!=NULL)
{
sscanf(line, "%s", exp);
delete_spaces(exp);
convert(exp);
}
}
fclose(fin);
getch();
}
我的文件包含带空格的字符串,当我从文件中读取一行时,该行仅包含第一个单词,而忽略空格后的字符串。
是的,因为sscanf()
(以及所有scanf()
函数)都使用%s
说明符来读取直到第一个空格字符的字符串。 因此,您可以通过该调用明确截断行。 在我看来,您想处理整行,因此sscanf没用-删除它。
您需要使用EOF语句来完全读取文件,例如,在您的情况下:
while ((line = getc(fin)) != EOF)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.