C语言 在最后一个单词的末尾添加额外的空格

eyh26e7m  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(162)

这是我的代码。它应该写下每一个第二个单词,从START到STOP,从一个文件到另一个文件。代码工作正常,但在最后一个单词的末尾有一个额外的空格。有什么建议吗?我尝试了一些东西,但最终只在前面有额外的空格,没有空格,或者多个不需要的空格。

#include <stdio.h>

#define MAX_BUFFER_SIZE 100

typedef struct {
    FILE *inputFile;
    FILE *outputFile;
} FilePair;

void initializeFiles(FilePair *files, char *inputFileName, char *outputFileName);
void closeFiles(FilePair *files);
void processFile(FilePair *files);

int main(int argc, char *argv[]) {
    FilePair files;

    initializeFiles(&files, argv[1], argv[2]);

    if (files.inputFile == NULL || files.outputFile == NULL) {
        return 1;
    }

    processFile(&files);

    closeFiles(&files);

    return 0;
}

void initializeFiles(FilePair *files, char *inputFileName, char *outputFileName) {
    files->inputFile = fopen(inputFileName, "r");
    files->outputFile = fopen(outputFileName, "w");

    if (files->inputFile == NULL || files->outputFile == NULL) {
        char error[] = "Error opening files\n";
        for (int i = 0; error[i] != '\0'; i++) {
            fputc(error[i], files->outputFile);
        }
        fclose(files->outputFile);
        files->outputFile = NULL; 
        fclose(files->inputFile);
        files->inputFile = NULL; 
        return;
    }
}

void closeFiles(FilePair *files) {
    if (files->inputFile != NULL) {
        fclose(files->inputFile);
        files->inputFile = NULL;
    }
    if (files->outputFile != NULL) {
        fclose(files->outputFile);
        files->outputFile = NULL;
    }
}

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;

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

        for (int i = 0; i < MAX_BUFFER_SIZE; i++) {
            buffer[i] = '\0';
        }

        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) {
                if (spaceCount % 2 != 0) {
                    fputc(ch, files->outputFile);
                }
                if (ch == ' ') {
                    spaceCount++;
                }

                if (ch == stopMarker[stopIdx]) {
                    stopIdx++;
                } else {
                    stopIdx = 0;
                }
                if (stopMarker[stopIdx] == '\0') {
                    isStart = 0;
                }
            } else {
                if (ch == startMarker[startIdx]) {
                    startIdx++;
                } else {
                    startIdx = 0;
                }
                if (startMarker[startIdx] == '\0') {
                    isStart = 1;
                }
            }
        }
    }
}

字符串
我只是想去掉最后一个词末尾的空格

tp5buhyn

tp5buhyn1#

你可以稍微修改一下你的逻辑,去掉最后一个单词末尾的多余空格。一种方法是维护一个标志来检查当前单词是否是最后一个单词。

修改代码如下:

#include <stdio.h>

#define MAX_BUFFER_SIZE 100

typedef struct {
    FILE *inputFile;
    FILE *outputFile;
} FilePair;

void initializeFiles(FilePair *files, char *inputFileName, char *outputFileName);
void closeFiles(FilePair *files);
void processFile(FilePair *files);

int main(int argc, char *argv[]) {
    FilePair files;

    initializeFiles(&files, argv[1], argv[2]);

    if (files.inputFile == NULL || files.outputFile == NULL) {
        return 1;
    }

    processFile(&files);

    closeFiles(&files);

    return 0;
}

void initializeFiles(FilePair *files, char *inputFileName, char *outputFileName) {
    files->inputFile = fopen(inputFileName, "r");
    files->outputFile = fopen(outputFileName, "w");

    if (files->inputFile == NULL || files->outputFile == NULL) {
        char error[] = "Error opening files\n";
        for (int i = 0; error[i] != '\0'; i++) {
            fputc(error[i], files->outputFile);
        }
        fclose(files->outputFile);
        files->outputFile = NULL;
        fclose(files->inputFile);
        files->inputFile = NULL;
        return;
    }
}

void closeFiles(FilePair *files) {
    if (files->inputFile != NULL) {
        fclose(files->inputFile);
        files->inputFile = NULL;
    }
    if (files->outputFile != NULL) {
        fclose(files->outputFile);
        files->outputFile = NULL;
    }
}

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 lastWord = 0;

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

        for (int i = 0; i < MAX_BUFFER_SIZE; i++) {
            buffer[i] = '\0';
        }

        buffer[0] = character;
        for (int i = 1; i < MAX_BUFFER_SIZE - 1; i++) {
            character = fgetc(files->inputFile);
            if (character == EOF || character == '\n') {
                lastWord = 1;  // Mark that this is the last word
                break;
            }
            buffer[i] = character;
        }

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

            if (isStart) {
                if (spaceCount % 2 != 0) {
                    fputc(ch, files->outputFile);
                }
                if (ch == ' ') {
                    spaceCount++;
                }

                if (ch == stopMarker[stopIdx]) {
                    stopIdx++;
                } else {
                    stopIdx = 0;
                }
                if (stopMarker[stopIdx] == '\0') {
                    isStart = 0;
                }
            } else {
                if (ch == startMarker[startIdx]) {
                    startIdx++;
                } else {
                    startIdx = 0;
                }
                if (startMarker[startIdx] == '\0') {
                    isStart = 1;
                }
            }
        }

        if (lastWord) {
            // Check if this is the last word and avoid adding an extra space
            fputc('\n', files->outputFile);
            break;
        }
    }
}

字符串
引入lastWord变量是为了跟踪当前单词是否是最后一个单词。如果它是最后一个单词,它可以避免在末尾添加额外的空格。此外,它会在输出文件中添加一个换行符以保持干净的输出。我希望这有助于解决您的错误。如需更多帮助,请在下面评论。

相关问题