You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言文件处理代码求助:最后一个单词末尾多余空格问题

解决C语言提取文本时末尾多余空格的问题

你的代码核心逻辑是通过spaceCount奇偶性判断输出内容,但遇到空格就直接输出的方式,会导致最后一个单词后的空格被写入,触发STOP标记时已经来不及回退。以下是针对性的修改方案:

核心调整思路

换一种空格控制逻辑:不为空格单独输出,而是在需要保留的单词前添加空格——用标记判断是否是第一个要输出的单词,第一个单词直接写,后续单词先写空格再写内容,从根源避免开头或末尾的多余空格。

修改后的processFile函数

#include <string.h> // 要加这个头文件用memset

void processFile(FilePair *files) {
    char startMarker[7] = "START ";
    char stopMarker[5] = "STOP";
    char buffer[MAX_BUFFER_SIZE];
    int startIdx = 0, stopIdx = 0;
    int isStart = 0;
    int spaceCount = 0;
    int isFirstOutputWord = 1; // 标记第一个要输出的单词
    char tempWord[MAX_BUFFER_SIZE] = {0}; // 缓存当前要输出的单词
    int wordLength = 0;

    while (1) {
        int character = fgetc(files->inputFile);
        if (character == EOF) break;

        memset(buffer, 0, MAX_BUFFER_SIZE);
        buffer[0] = character;
        for (int i = 1; i < MAX_BUFFER_SIZE - 1; i++) {
            character = fgetc(files->inputFile);
            if (character == EOF || character == '\n') break;
            buffer[i] = character;
        }

        for (int i = 0; buffer[i] != '\0'; i++) {
            char ch = buffer[i];

            if (isStart) {
                // 先检测STOP标记,优先终止处理
                if (ch == stopMarker[stopIdx]) {
                    stopIdx++;
                } else {
                    stopIdx = 0;
                }
                if (stopMarker[stopIdx] == '\0') {
                    // 遇到STOP,先把缓存的单词输出(如果需要)
                    if (spaceCount % 2 != 0 && wordLength > 0) {
                        if (!isFirstOutputWord) fputc(' ', files->outputFile);
                        fputs(tempWord, files->outputFile);
                    }
                    isStart = 0;
                    wordLength = 0;
                    memset(tempWord, 0, MAX_BUFFER_SIZE);
                    continue;
                }

                // 处理字符:缓存单词或判断输出时机
                if (ch == ' ') {
                    // 遇到空格,判断当前缓存的单词是否需要输出
                    if (spaceCount % 2 != 0 && wordLength > 0) {
                        if (!isFirstOutputWord) fputc(' ', files->outputFile);
                        fputs(tempWord, files->outputFile);
                        isFirstOutputWord = 0;
                    }
                    spaceCount++;
                    wordLength = 0;
                    memset(tempWord, 0, MAX_BUFFER_SIZE);
                } else {
                    // 非空格字符,加入缓存(仅当当前单词需要保留时)
                    if (spaceCount % 2 != 0 && wordLength < MAX_BUFFER_SIZE - 1) {
                        tempWord[wordLength++] = ch;
                    }
                }
            } else {
                // 检测START标记
                if (ch == startMarker[startIdx]) {
                    startIdx++;
                } else {
                    startIdx = 0;
                }
                if (startMarker[startIdx] == '\0') {
                    isStart = 1;
                    spaceCount = 0;
                    isFirstOutputWord = 1;
                    wordLength = 0;
                    memset(tempWord, 0, MAX_BUFFER_SIZE);
                }
            }
        }
    }

    // 处理文件结束时未输出的缓存单词
    if (isStart && spaceCount % 2 != 0 && wordLength > 0) {
        if (!isFirstOutputWord) fputc(' ', files->outputFile);
        fputs(tempWord, files->outputFile);
    }
}

修改点说明

  1. 新增缓存与标记:用tempWord缓存要输出的单词,isFirstOutputWord控制空格添加时机,彻底避免无意义的空格输出。
  2. 调整STOP检测顺序:先判断是否触发STOP标记,再处理字符,防止STOP前的空格被写入。
  3. 文件收尾处理:如果到文件结尾仍处于START范围内,确保最后一个要输出的单词能被正确写入。

内容的提问来源于stack exchange,提问作者misucik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 22:57:50