【问题标题】:Is there a way to replace spaces in a sentence with ellipses in C?有没有办法用C中的省略号替换句子中的空格?
【发布时间】:2022-01-08 20:04:51
【问题描述】:

我想知道如何用省略号替换 txt 文件中不同句子中的所有空格。

例如:

  • 原句:Hi my name is Joseph.
  • 修饰句:Hi... my... name... is... Joseph...

文本结构如下:

  • 句子 1.
  • 第 2 句。
  • 第 3 句。
  • 第 4 句。
  • .......

(每个句子都以点结尾)。我做了一个代码,但它只能从手动输入的句子中工作:

#include<stdio.h>

int main() {  
    char ch = getchar();

    while (ch != EOF) {
        if (ch != '\n' && ch != ' ') {
            putchar(ch);
            ch = getchar();    
        }
        else {   
            printf("... ");
            while (ch == '\n' || ch == ' ') {
                ch = getchar();
            }  
        }  
    }
    putchar('\n');
    return 0;
}

有谁知道如何修改此代码以从文件中读取句子并使用点进行更改?

【问题讨论】:

  • 您的示例没有替换,而是在空格前添加 '...'。 2个相邻的空间应该怎么办? "... ... " 还是什么?
  • 代码替换了' ' '\n',但声明的目标只是空格。如果目标确实包括'\n',为什么不也包括'\t''\r'、...等所有空格?
  • 您的示例似乎将句尾的句号(句点,点)替换为三个点——那里还有间距吗?如果句子不是每行一个——如果一行中有两三个句子,或者一个句子有多行,会发生什么?

标签: arrays c string file


【解决方案1】:

有谁知道如何修改此代码以从文件中读取句子

两种方式:

  1. 使用 shell 重定向,例如./a.out &lt; input.txt
  2. 使用fopenfgetsfclose。见man fopen

【讨论】:

    【解决方案2】:

    类型错误

    getchar() 返回unsigned char 范围内的int 或负数EOF。这 257 个不同的值不能单独保存在 char 中。使用int 或冒无限循环或过早停止的风险。

    // char ch = getchar();
    int ch = getchar();
    

    读取/编辑/写入同一个文件

    • 从文件1(fopen(... "r"))读取,将" "调整为"...",将结果写入使用(fopen(... "w"))打开的新文件2。

    • 关闭文件 (fclose())。

    • 如果成功,rename()file1 进入 file_temp。将文件 2 重命名为文件 1。

    • 如果成功,删除 file_temp (remove())。

    重要的是在新文件成功创建和命名之前不要删除旧文件的数据 - 这有助于错误恢复。检查各种 FILE 函数的返回值以查找错误。

    【讨论】:

      【解决方案3】:

      您真的不想费心在代码中进行文件操作。 shell 在处理文件重定向方面做得很好,你应该使用它。如果您确实想要执行文件操作代码,请不要尝试写回原始文件。如果您尝试编辑文件,则数据损坏的风险太大。将文件视为不可变的。相反,写入临时文件,然后根据需要将临时文件移到原始文件上。这样做几乎总是想要你真正想要的。您并不关心生成的文件是否与原始文件相同;您只关心它在文件系统中具有相同的名称。用... 替换所有空格可以用下面的代码来完成。如果您还要替换换行符,您可能应该替换所有空格,所以我选择只替换问题陈述中的空格。

      #include <errno.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <unistd.h>
      
      FILE * xfopen(const char *path, const char *mode);
      FILE * xmkstemp(char * template, char *mode);
      
      int
      main(int argc, char **argv)
      {
          int ch;
          char template[] = "/tmp/tmpfile.XXXXXXX";
          const char *path = argc > 1 ? argv[1] : "stdin";
          FILE *ifp = argc > 1 ? xfopen(path, "r") : stdin;
          char *tmp_path = argc > 1 ? template : "stdout";
          FILE *ofp = argc > 1 ? xmkstemp(template, "w") : stdout;
      
          while( (ch = getc(ifp)) != EOF ){
              int rv = ch != ' ' ? fputc(ch, ofp) : fputs("...", ofp);
              if( rv == EOF ){
                  perror(tmp_path);
                  return EXIT_FAILURE;
              }
          }
          if( ferror(ifp) ){
              perror(path);
              return EXIT_FAILURE;
          }
          if( fclose(ofp) ){
              perror(tmp_path);
              return EXIT_FAILURE;
          }
          if( argc > 1 && rename(tmp_path, path) ){
              int errno_sav = errno;
              fprintf(stderr, "rename %s -> ", tmp_path);
              errno = errno_sav;
              perror(path);
              return EXIT_FAILURE;
          }
          return 0;
      }
      
      FILE *
      xfopen(const char *path, const char *mode)
      {
          FILE *fp = path[0] != '-' || path[1] != '\0' ? fopen(path, mode) :
              *mode == 'r' ? stdin : stdout;
          if( fp == NULL ){
              perror(path);
              exit(EXIT_FAILURE);
          }
          return fp;
      }
      
      FILE *
      xmkstemp(char * template, char *mode)
      {
          FILE *fp;
          int fd = mkstemp(template);
          if( fd == -1 ){
              perror("mkstemp");
              exit(EXIT_FAILURE);
          }
          fp = fdopen(fd, mode);
          if( fp == NULL ){
              perror(template);
              exit(EXIT_FAILURE);
          }
          return fp;
      }
      

      【讨论】:

        猜你喜欢
        • 2020-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-12
        • 2020-09-04
        • 2019-08-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多