[英]How do I deal with multiple spaces before or in between the words in CS50's initials (more comfortable)?
[英]How do I keep words with spaces between them on the same line?
我正在阅读一个文本文件,其中包含以下单词:
hi
there
was it
fuzzy man lady
但是,每次我逐行打印这些内容时,都会得到以下信息:
hi
there
was
it
fuzzy
man
lady
我希望将在一起的单词放在同一行,例如:
was it
fuzzy man lady
这是代码:
int main(int argc, char* argv []) {
int i = 0, line = 7;
char ch[200];
char const* const fileName = argv[1];
FILE *file = fopen(fileName,"r");
while(line--){
fscanf(file,"%s",ch);
i++;
printf("%s", ch);
}
fclose(file);
return 0;
我猜想\\ n在检测到新行上的空格或单词时并没有区别。 那么我如何修改它以得到期望的结果呢?
提前致谢。
也可以使用fread()
。 以下将能够从文件中读取最多199个字符。
fread(ch, sizeof(ch[0]), sizeof(ch)/sizeof(ch[0])-1, file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.