📅  最后修改于: 2023-12-03 15:27:12.606000             🧑  作者: Mango
在开发中,有时候需要用一个单词替换掉文件中的另一个单词。这个时候就需要一个查找和替换的程序。本文介绍如何用 C 语言实现一个用另一个给定的单词查找和替换文件中的单词的程序。
整个程序分为以下几个步骤:
下面是读取文件内容的函数:
#include <stdio.h>
#include <stdlib.h>
char* read_file(const char* filename) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file: %s\n", filename);
exit(EXIT_FAILURE);
}
fseek(fp, 0, SEEK_END);
long file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* buffer = (char*)malloc(file_size + 1);
fread(buffer, file_size, 1, fp);
fclose(fp);
buffer[file_size] = '\0';
return buffer;
}
下面是查找给定单词的函数:
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
bool contains_word(const char* haystack, const char* needle) {
const char* p = haystack;
const size_t len = strlen(needle);
bool found = false;
while ((p = strstr(p, needle)) != NULL) {
if ((p == haystack || !isalnum(*(p - 1))) &&
!isalnum(*(p + len))) {
found = true;
break;
}
p += len;
}
return found;
}
下面是替换为另一个给定单词的函数:
#include <ctype.h>
void replace_word(char* haystack, const char* needle,
const char* replacement) {
const size_t len_needle = strlen(needle);
const size_t len_replacement = strlen(replacement);
char* p = haystack;
while ((p = strstr(p, needle)) != NULL) {
if ((p == haystack || !isalnum(*(p - 1))) &&
!isalnum(*(p + len_needle))) {
char* tmp = strdup(p + len_needle);
snprintf(p, len_replacement + 1, "%s", replacement);
strcat(p, tmp);
free(tmp);
p += len_replacement;
} else {
p += len_needle;
}
}
}
下面是写入到新的文件中的函数:
void write_file(const char* filename, const char* content) {
FILE* fp = fopen(filename, "w");
if (fp == NULL) {
printf("Failed to open file: %s\n", filename);
exit(EXIT_FAILURE);
}
fputs(content, fp);
fclose(fp);
}
下面是完整的代码:
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* read_file(const char* filename) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file: %s\n", filename);
exit(EXIT_FAILURE);
}
fseek(fp, 0, SEEK_END);
long file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* buffer = (char*)malloc(file_size + 1);
fread(buffer, file_size, 1, fp);
fclose(fp);
buffer[file_size] = '\0';
return buffer;
}
bool contains_word(const char* haystack, const char* needle) {
const char* p = haystack;
const size_t len = strlen(needle);
bool found = false;
while ((p = strstr(p, needle)) != NULL) {
if ((p == haystack || !isalnum(*(p - 1))) &&
!isalnum(*(p + len))) {
found = true;
break;
}
p += len;
}
return found;
}
void replace_word(char* haystack, const char* needle,
const char* replacement) {
const size_t len_needle = strlen(needle);
const size_t len_replacement = strlen(replacement);
char* p = haystack;
while ((p = strstr(p, needle)) != NULL) {
if ((p == haystack || !isalnum(*(p - 1))) &&
!isalnum(*(p + len_needle))) {
char* tmp = strdup(p + len_needle);
snprintf(p, len_replacement + 1, "%s", replacement);
strcat(p, tmp);
free(tmp);
p += len_replacement;
} else {
p += len_needle;
}
}
}
void write_file(const char* filename, const char* content) {
FILE* fp = fopen(filename, "w");
if (fp == NULL) {
printf("Failed to open file: %s\n", filename);
exit(EXIT_FAILURE);
}
fputs(content, fp);
fclose(fp);
}
int main(int argc, char* argv[]) {
if (argc < 4) {
printf("Usage: %s <filename> <old_word> <new_word>\n", argv[0]);
exit(EXIT_FAILURE);
}
const char* filename = argv[1];
const char* old_word = argv[2];
const char* new_word = argv[3];
char* content = read_file(filename);
if (contains_word(content, old_word)) {
replace_word(content, old_word, new_word);
write_file(filename, content);
} else {
printf("The word '%s' was not found in the file.\n", old_word);
}
free(content);
return 0;
}
以上是用 C 语言实现用另一个给定的单词查找和替换文件中的单词的程序。通过运用这个程序,可以快捷、高效的实现将文本文件中的一个单词替换成另一个单词,从而提高程序员的开发效率。