简单的行编辑器的代码
时间: 2023-11-14 12:05:01 浏览: 32
以下是一个基于C语言的简单行编辑器的代码,可以实现插入、删除、替换、查找和移动光标等操作:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LEN 1024 // 最大行长度
#define MAX_LINES 1000 // 最大行数
char lines[MAX_LINES][MAX_LINE_LEN]; // 存储文件内容的数组
int num_lines = 0; // 文件行数
// 读入文件内容
int read_file(const char *filename)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Error: cannot open file %s\n", filename);
return -1;
}
char line[MAX_LINE_LEN];
while (fgets(line, MAX_LINE_LEN, fp) != NULL)
{
if (num_lines >= MAX_LINES)
{
printf("Error: too many lines in file\n");
fclose(fp);
return -1;
}
strcpy(lines[num_lines], line);
num_lines++;
}
fclose(fp);
return 0;
}
// 显示文件内容
void print_file()
{
for (int i = 0; i < num_lines; i++)
{
printf("%d: %s", i + 1, lines[i]);
}
}
// 插入新行
void insert_line(int line_num, const char *line)
{
if (line_num < 1 || line_num > num_lines + 1)
{
printf("Error: invalid line number %d\n", line_num);
return;
}
if (num_lines >= MAX_LINES)
{
printf("Error: too many lines in file\n");
return;
}
// 将后面的行向后移动一行
for (int i = num_lines - 1; i >= line_num - 1; i--)
{
strcpy(lines[i + 1], lines[i]);
}
strcpy(lines[line_num - 1], line);
num_lines++;
}
// 删除指定行
void delete_line(int line_num)
{
if (line_num < 1 || line_num > num_lines)
{
printf("Error: invalid line number %d\n", line_num);
return;
}
// 将后面的行向前移动一行
for (int i = line_num - 1; i < num_lines - 1; i++)
{
strcpy(lines[i], lines[i + 1]);
}
num_lines--;
}
// 替换指定行
void replace_line(int line_num, const char *line)
{
if (line_num < 1 || line_num > num_lines)
{
printf("Error: invalid line number %d\n", line_num);
return;
}
strcpy(lines[line_num - 1], line);
}
// 查找指定字符串
void search_string(const char *str)
{
for (int i = 0; i < num_lines; i++)
{
if (strstr(lines[i], str) != NULL)
{
printf("%d: %s", i + 1, lines[i]);
}
}
}
// 移动光标
void move_cursor(int line_num)
{
if (line_num < 1 || line_num > num_lines)
{
printf("Error: invalid line number %d\n", line_num);
return;
}
printf("Cursor moved to line %d\n", line_num);
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s <filename>\n", argv[0]);
return 0;
}
if (read_file(argv[1]) != 0)
{
return -1;
}
char cmd[256];
int line_num;
char line[MAX_LINE_LEN];
char str[256];
while (1)
{
printf("Enter command (i/d/r/s/m/q): ");
fgets(cmd, 256, stdin);
switch (cmd[0])
{
case 'i': // 插入新行
printf("Enter line number and line: ");
scanf("%d %[^\n]", &line_num, line);
getchar(); // 读取剩余的换行符
insert_line(line_num, line);
break;
case 'd': // 删除指定行
printf("Enter line number: ");
scanf("%d", &line_num);
getchar(); // 读取剩余的换行符
delete_line(line_num);
break;
case 'r': // 替换指定行
printf("Enter line number and line: ");
scanf("%d %[^\n]", &line_num, line);
getchar(); // 读取剩余的换行符
replace_line(line_num, line);
break;
case 's': // 查找指定字符串
printf("Enter string to search: ");
scanf("%s", str);
getchar(); // 读取剩余的换行符
search_string(str);
break;
case 'm': // 移动光标
printf("Enter line number: ");
scanf("%d", &line_num);
getchar(); // 读取剩余的换行符
move_cursor(line_num);
break;
case 'q': // 退出程序
return 0;
default:
printf("Invalid command\n");
break;
}
print_file();
}
return 0;
}
```
这个行编辑器使用命令行界面,通过读入用户输入的命令和参数来进行相应的操作,并将文件内容存储在内存中,可以对其进行增删改查等操作。