C程序在文件

时间:2017-06-02 22:06:27

标签: c

我编写了一个写入文件的程序,然后对该文件进行查找和替换,条件是新单词的长度必须与旧单词的长度相同。

程序会将文件的块读入缓冲区,在缓冲区上执行查找和替换,将其写入副本,当原始文件结束时,它会将编辑后的文本复制回到原始

如果它没有读取整行(意味着该单词可以在2次读取之间拆分),它会向后回滚文件处理程序的旧单词长度。这是我感兴趣的部分,我可以通过lengthlength - 1回滚,当前的方式似乎正常工作,即length

请注意,这是一项学校作业,我将在老师面前展示,是否有任何可能的错误?我想确保它正在做我认为它正在做的事情。

这是代码,为了测试它是否正常,我将缓冲区大小设置得非常小,我会将其更改为适当的。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 10
#define WORD_SIZE 24

void write_to_file(const char *s);
void replace_word(const char *s);
void clear(void);

int main(void){
    const char file_name[] = "text.txt";
    int c;
    puts("Enter 'w' to write to the file, 'r' to replace a word");
    while((c = getchar()) != EOF){
        switch(c){
        case 'w':
            clear();
            write_to_file(file_name);
            break;
        case 'r':
            clear();
            replace_word(file_name);
            break;
        default:
            clear();
            printf("Invalid command:%c\n", c);
            break;
        }
        puts("Enter 'w' to write to the file, 'r' to replace a word");
    }
    return 0;
}

void write_to_file(const char *s){
    puts("Enter input:");
    FILE *in_file;
    if((in_file = fopen(s, "w")) == NULL){
        perror(s);
        exit(EXIT_FAILURE);
    }

    int c;
    while((c = getchar()) != EOF){
        fputc(c, in_file);
    }

    fclose(in_file);
    puts("Successfully written to the file");
}

void replace_word(const char *s){
    char old_word[WORD_SIZE];
    char new_word[WORD_SIZE];

    puts("Enter the word you want to replace");
    fgets(old_word, sizeof(old_word), stdin);
    old_word[strlen(old_word) - 1] = '\0';
    puts("Enter the new word");
    fgets(new_word, sizeof(new_word), stdin);
    new_word[strlen(new_word) - 1]  = '\0';

    if(strlen(old_word) != strlen(new_word)){
        fprintf(stderr, "Error: can't replace \"%s\" with \"%s\":\nThe length is not the same\n", old_word, new_word);
        return;
    }
    FILE *original_file;
    FILE *copy;

    if((original_file = fopen(s, "r")) == NULL){
        perror(s);
        exit(EXIT_FAILURE);
    }

    if((copy = fopen("copy.txt", "w")) == NULL){
        perror("text");
        exit(EXIT_FAILURE);
    }

    char *buffer = malloc(BUFFER_SIZE);
    int word_len = strlen(old_word);
    int word_frequency = 0;

    while(fgets(buffer, BUFFER_SIZE, original_file)){
        char *init_loc = buffer;
        while((buffer = strstr(buffer, old_word))){
            memcpy(buffer, new_word, word_len);
            word_frequency++;
        }
        buffer = init_loc;
        fputs(buffer, copy);
        if(!strchr(buffer, '\n')){
            fseek(original_file, -(word_len), SEEK_CUR);
        }
    }

    printf("'%s' found %i times\n", old_word, word_frequency);

    fclose(original_file);
    fclose(copy);

    if((original_file = fopen(s, "w")) == NULL){
        perror(s);
        exit(EXIT_FAILURE);
    }

    if((copy = fopen("copy.txt", "r")) == NULL){
        perror("text");
        exit(EXIT_FAILURE);
    }

    int c;
    while((c = fgetc(copy)) != EOF){
        fputc(c, original_file);
    }

    fclose(original_file);
    fclose(copy);
    remove("copy.txt");
    free(buffer);

    if(word_frequency > 0){
        puts("Word replaced successfully");
    }
    else{
        puts("No words were replaced");
    }
}

void clear(void){
    int c;
    while((c = getchar()) != '\n' && (c != EOF));
}

0 个答案:

没有答案