有没有办法用 C 中的省略号替换句子中的空格?

Is there a way to replace spaces in a sentence with ellipses in C?

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

例如:

正文结构如下:

(每句话以点结尾)。我已经制作了一个代码,但它只适用于手动输入的句子:

#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;
}

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

Does anyone know how to modify this code to read the sentences from the file

两种方式:

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

类型错误

getchar() return 是 unsigned char 范围内的 int 或负值 EOF。这 257 个不同的值无法在 char 中区分保存。使用 int 或冒无限循环或过早停止的风险。

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

到read/edit/write同一个文件

  • 从file1读取(fopen(... "r")),将" "调整为"...",将结果写入用(fopen(... "w"))打开的新file2。

  • 关闭文件(fclose())。

  • 如果成功,rename() file1 变成file_temp。将 file2 重命名为 file1.

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

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

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

#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] != '[=10=]' ? 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;
}