实现一个最简单的VIM文本编辑器(可能有bug,随便写了一个)

简单的写了一个文本编辑器,功能很简单,但足以把文件IO相关的操作熟悉了,可能功能或者分配的大小还不够完善。请参考参考:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <fcntl.h>
int count ;
char ch = 0;
char file_name[20]={0};
char buffer[1024*1024]={0};
//退出文本编辑器
int exit_editor()
{
    printf("exit editor!\n");
    memset(buffer, 0,1024*1024);
    system("cls");
    return -1 ;
}
//文本编辑模式
int text_editor_mode()
{
    int flag ;
    char input_ch = 0 ;
    flag = 0 ;
    count = 0 ;
    system("cls");
    //进入编辑文本模式
    printf("\a");
    //清空数组
    while(1)
    {
       input_ch = getch();
       //ESC按键
       if(input_ch == 0x1B)
       {
           printf("exit editor\n");
           system("cls");
           flag = 1 ;
           return flag ;
       }
       //ENTER按键---->实现文本编辑器换行
       if(input_ch == 0x0d)
       {
           putchar('\n');
       }
       //输入的内容不是ESC按键的情况下就将文本信息保存到数组里去
       if(input_ch != 0x1B)
       {
           buffer[count] = input_ch ;
           putchar(input_ch);
           count++;
       }
    }
}

//文本保存模式
int save_text_mode()
{
    int flag ;
    flag = 0 ;
    system("cls");
    printf("plase input file name\n");
    scanf("%s",file_name);
    int openfd = open(file_name,O_CREAT | O_TRUNC | O_RDWR);
    if(-1 == openfd){
       printf("open file fair!\n");
       memset(buffer, 0,1024*1024);
       memset(file_name,0,20);
       return -1 ;
     }
     write(openfd,buffer,count);
     printf("save file success\n");
     memset(buffer, 0,1024*1024);
     memset(file_name,0,20);
     close(openfd);
     printf("file save success!file_name:%s\n",file_name);
     system("cls");
     flag = 1 ;
     return flag ;
}

//读取一个文件的信息
int read_file()
{
    char *file = NULL;
    char *file_buffer = NULL ;
    system("cls");
    int fle_flag = 0 ;
    int count_fd ;
    file = malloc(20);
    file_buffer = malloc(1024);
    printf("please input file name!\n");
    scanf("%s",file);
    system("cls");
    printf("please input file size!\n");
    scanf("%d",&count_fd);
    int fd = open(file,O_RDONLY);
    if(-1 == fd)
        fprintf(stderr,"open file error");
    system("cls");
    int i ;
    read(fd,file_buffer,count_fd);
    for(i = 0 ; i < count_fd ; i++)
    {
        printf("%c",file_buffer[i]);
    }
    while(1)
    {
        char rch = getch();
        if(rch == 0x1B){
           system("cls");
           break ;
        }
    }
    free(file);
    free(file_buffer);
    close(fd);
    fle_flag = 1 ;
    return fle_flag ;
}
int main(void)
{
    int file_fd = -1;
    int ret = 0;
    int exit_ret = 0;
    int save_ret = 0;
    int read_ret = 0 ;
    //重新开始
    Rep:
    printf("Welcome to yyx develop VIM-EDITUO\n");
    printf("please input i to editor ,r to read ,  w to save , q to quit\n");
    while(1)
    {
        ch = getch();
        switch(ch)
        {
           case 'w':  save_ret = save_text_mode(); break ;
           case 'i':  ret = text_editor_mode();    break ;
           case 'q':  exit_ret = exit_editor();    break ;
           case 'r':  read_ret = read_file();      break ;
        }
        if(ret == 1 || save_ret == 1 || read_ret == 1)
            goto Rep ;
        if(exit_ret == -1)
            break ;
    }
    return 0 ;
}
上一篇:H5取经之路——随便写点儿


下一篇:java用POI操作excel——随便写一下,最基础的东西