如何修改C代码以在没有用户输入的情况下替换文件中的单词?

kmbjn2e3  于 2023-06-05  发布在  其他
关注(0)|答案(1)|浏览(438)
/**
 * C program to find and replace all occurrences of a word in file.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 1000

/* Function declaration */
void replaceAll(char *str, const char *oldWord, const char *newWord);

int main()
{
/* File pointer to hold reference of input file */
FILE * fPtr;
FILE * fTemp;
char path[100];

char buffer[BUFFER_SIZE];
char oldWord[100], newWord[100];

printf("Enter path of source file: ");
scanf("%s", path);

printf("Enter word to replace: ");
scanf("%s", oldWord);

printf("Replace '%s' with: ");
scanf("%s", newWord);

/*  Open all required files */
fPtr  = fopen(path, "r");
fTemp = fopen("replace.tmp", "w"); 

/* fopen() return NULL if unable to open file in given mode. */
if (fPtr == NULL || fTemp == NULL)
{
    /* Unable to open file hence exit */
    printf("\nUnable to open file.\n");
    printf("Please check whether file exists and you have read/write privilege.\n");
    exit(EXIT_SUCCESS);
}

/*
 * Read line from source file and write to destination 
 * file after replacing given word.
 */
while ((fgets(buffer, BUFFER_SIZE, fPtr)) != NULL)
{
    // Replace all occurrence of word from current line
    replaceAll(buffer, oldWord, newWord);

    // After replacing write it to temp file.
    fputs(buffer, fTemp);
}

/* Close all files to release resource */
fclose(fPtr);
fclose(fTemp);

/* Delete original source file */
remove(path);

/* Rename temp file as original file */
rename("replace.tmp", path);

printf("\nSuccessfully replaced all occurrences of '%s' with '%s'.", oldWord, newWord);

return 0;
}


/**
 * Replace all occurrences of a given a word in string.
 */
void replaceAll(char *str, const char *oldWord, const char *newWord)
{
char *pos, temp[BUFFER_SIZE];
int index = 0;
int owlen;

owlen = strlen(oldWord);

// Fix: If oldWord and newWord are same it goes to infinite loop
if (!strcmp(oldWord, newWord)) {
    return;
}

/*
 * Repeat till all occurrences are replaced. 
 */
while ((pos = strstr(str, oldWord)) != NULL)
{
    // Backup current line
    strcpy(temp, str);

    // Index of current found word
    index = pos - str;

    // Terminate str after word found index
    str[index] = '\0';

    // Concatenate str with new word 
    strcat(str, newWord);
    
    // Concatenate str with remaining words after 
    // oldword found index.
    strcat(str, temp + index + owlen);
}
}

我有一段C语言的代码,可以把所有的“oldWords”变成“newWords”。工作正常,但每次我想改变代码,改变自己的话,我完全愚蠢。我不想把那些需要修改的单词放到控制台里,而是想把它们放在代码里。我只想告诉控制台源文件的路径,仅此而已。
如果你能帮我举一些例子,比如Hello to Bye和Morning to Night。

wwtsj6pe

wwtsj6pe1#

如果你不想从用户输入中获取oldWordnewWord,你可以在代码中将它们定义为常量:

const char* oldWord = "Hello";
const char* newWord = "Bye";

相关问题