要求有输入删除查找替换等功能,能打开和保存指定位置的文件
时间: 2024-03-22 15:40:59 浏览: 47
好的,下面是一个功能更加完整的文本编辑器的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 1000 // 最大行长度
#define MAX_FILENAME_LENGTH 200 // 最大文件名长度
// 打开指定文件
FILE *open_file(char *filename, char *mode) {
FILE *fptr = fopen(filename, mode);
if (fptr == NULL) {
printf("Error opening the file!\n");
exit(1);
}
return fptr;
}
// 关闭指定文件
void close_file(FILE *fptr) {
fclose(fptr);
}
// 输出文件内容
void print_file_contents(char *filename) {
FILE *fptr = open_file(filename, "r");
char line[MAX_LINE_LENGTH];
while (fgets(line, MAX_LINE_LENGTH, fptr) != NULL) {
printf("%s", line);
}
close_file(fptr);
}
// 查找指定字符串
void find_string(char *filename, char *search_string) {
FILE *fptr = open_file(filename, "r");
char line[MAX_LINE_LENGTH];
int line_num = 0, found = 0;
while (fgets(line, MAX_LINE_LENGTH, fptr) != NULL) {
line_num++;
if (strstr(line, search_string) != NULL) {
printf("Line %d: %s", line_num, line);
found = 1;
}
}
if (!found) {
printf("String not found!\n");
}
close_file(fptr);
}
// 替换指定字符串
void replace_string(char *filename, char *search_string, char *replace_string) {
FILE *fptr = open_file(filename, "r");
char line[MAX_LINE_LENGTH];
int line_num = 0, found = 0;
char temp_filename[MAX_FILENAME_LENGTH];
sprintf(temp_filename, "%s.temp", filename);
FILE *temp_fptr = open_file(temp_filename, "w");
while (fgets(line, MAX_LINE_LENGTH, fptr) != NULL) {
line_num++;
char *pos = strstr(line, search_string);
if (pos != NULL) {
found = 1;
int index = pos - line;
int search_string_len = strlen(search_string);
int replace_string_len = strlen(replace_string);
char new_line[MAX_LINE_LENGTH];
strncpy(new_line, line, index);
new_line[index] = '\0';
strcat(new_line, replace_string);
strcat(new_line, &line[index + search_string_len]);
fputs(new_line, temp_fptr);
} else {
fputs(line, temp_fptr);
}
}
close_file(fptr);
close_file(temp_fptr);
if (found) {
remove(filename);
rename(temp_filename, filename);
printf("String replaced successfully!\n");
} else {
remove(temp_filename);
printf("String not found!\n");
}
}
int main() {
char filename[MAX_FILENAME_LENGTH], search_string[MAX_LINE_LENGTH], replace_string[MAX_LINE_LENGTH];
int option;
printf("Welcome to the text editor!\n");
while (1) {
printf("\nMenu:\n1. Open file\n2. Save file\n3. Print file contents\n4. Find string\n5. Replace string\n6. Exit\n");
printf("Enter your option: ");
scanf("%d", &option);
switch (option) {
case 1:
printf("Enter the filename: ");
scanf("%s", filename);
open_file(filename, "r");
break;
case 2:
printf("Enter the filename: ");
scanf("%s", filename);
save_file(filename);
break;
case 3:
printf("Enter the filename: ");
scanf("%s", filename);
print_file_contents(filename);
break;
case 4:
printf("Enter the filename: ");
scanf("%s", filename);
printf("Enter the search string: ");
scanf("%s", search_string);
find_string(filename, search_string);
break;
case 5:
printf("Enter the filename: ");
scanf("%s", filename);
printf("Enter the search string: ");
scanf("%s", search_string);
printf("Enter the replace string: ");
scanf("%s", replace_string);
replace_string(filename, search_string, replace_string);
break;
case 6:
printf("Exiting the text editor!\n");
exit(0);
default:
printf("Invalid option!\n");
break;
}
}
return 0;
}
```
这个文本编辑器支持打开、保存、输出文件内容、查找指定字符串、替换指定字符串等功能。其中查找和替换使用了C语言库函数 `strstr()` 和 `strncpy()` 等函数实现。
阅读全文