Implement one of the simplest vim text editors (there may be bugs, write a random one)

Source: Internet
Author: User
Tags save file

Simple to write a text editor, the function is very simple, but enough to the file IO related operations familiar with the possible function or the size of the allocation is not perfect. Please refer to the reference:

#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};
    Exit Text Editor int exit_editor () {printf ("Exit editor!\n");
    memset (buffer, 0,1024*1024);
    System ("CLS");
return-1;
    }//text editing mode int text_editor_mode () {int flag;
    char input_ch = 0;
    Flag = 0;
    Count = 0;
    System ("CLS");
    Enter edit text mode printf ("\a");
       Empty array while (1) {input_ch = Getch ();
           ESC key if (input_ch = = 0x1b) {printf ("Exit editor\n");
           System ("CLS");
           flag = 1;
       return flag;
       //enter key----> Implement Text Editor newline if (input_ch = = 0x0d) {putchar (' \ n ');
           If the input content is not an ESC key, save the text information to the array if (input_ch!= 0x1b) {Buffer[count] = input_ch;
      Putchar (INPUT_CH);     count++;
    }}//text save mode 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;
    //Read information for one file 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", &AMP;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;
    Re-start rep:printf ("Welcome to Yyx develop vim-edituo\n");
    printf ("Please input I-editor, R to read, W-save, Q to quit\n");
        while (1) {ch = getch ();
           Switch (CH) {case ' W ': Save_ret = Save_text_mode ();    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;
 }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.