C 中的连续空格去除
文本文件包含一堆字符。文件中没有制表符。编写一个程序,用一个空格替换两个或多个连续的空格。该程序的输入应来自名称已通过 argv[1] 提供的文件。该程序的输出应转到标准输出。
输入:
Let’s go to the movies.
输出:
Let’s go to the movies.
这就是我到目前为止所拥有的:
#include <stdio.h>
int main(int argc, char* argv[]){
char line;
FILE* fin;
int i=0;
fin=fopen("textfile38", "r");
fscanf(fin,"%c",&line);
while((i<=line || line ==' '));
{
if(line !=' ')
{
putchar(line);
i=i+1;
}
else
{
putchar(' ');
}
while(line == ' ')
{
i=i+1;
}
}
printf("%c \n", getchar());
getchar();
return 0;
}
它没有给我输出,我不确定我做错了什么,如果有人可以帮助我,而不仅仅是给我答案,那就太好了,谢谢。
A text file contains a bunch of characters. There are no tab characters within the file. Write a program that replaces two or more consecutive blanks by a single blank. The input from this program should come from a file whose name has been supplied via argv[1]. The output from this program should go to standard output.
Input:
Let’s go to the movies.
Output:
Let’s go to the movies.
This is what i have so far:
#include <stdio.h>
int main(int argc, char* argv[]){
char line;
FILE* fin;
int i=0;
fin=fopen("textfile38", "r");
fscanf(fin,"%c",&line);
while((i<=line || line ==' '));
{
if(line !=' ')
{
putchar(line);
i=i+1;
}
else
{
putchar(' ');
}
while(line == ' ')
{
i=i+1;
}
}
printf("%c \n", getchar());
getchar();
return 0;
}
It doesn't give me an output I'm not sure what I did wrong if anyone could help me NOT just give me the answer that would be great thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能会帮助您取得进步:
看来您在 while 循环之前只读取了一个字符。您可能希望在 while 循环内连续读取字符,并在到达文件末尾时停止循环。
第二个提示:
您让文件保持打开状态。退出程序之前你应该做什么?
This might help you to progress:
It seems you read only one character before the while loop. You might want to read characters continuously inside the while loop and stop looping when end of file is reached.
Second hint:
You leave the file open. What should you do before exiting your program?