C 中的连续空格去除

发布于 2024-12-15 00:33:27 字数 786 浏览 4 评论 0原文

文本文件包含一堆字符。文件中没有制表符。编写一个程序,用一个空格替换两个或多个连续的空格。该程序的输入应来自名称已通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋情葬爱 2024-12-22 00:33:27

这可能会帮助您取得进步:
看来您在 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文