简体   繁体   中英

Text Editor in C select text with shift

I am trying to design text editor in C. For example I am creating a new text file and getting content from user until pressing .

void Create() {
    fp1 = fopen("temp.txt", "w");
    printf("\n\tEnter the text and press '.' to save\n\n");
    while (1) {
        c = getchar();
        fputc(c, fp1);
        if (c == '.') {
            fclose(fp1);
            printf("\n\tEnter then new filename: ");
            scanf("%s", fn);
            fp1 = fopen("temp.txt", "r");
            fp2 = fopen(fn, "w");
            while (!feof(fp1)) {
                c = getc(fp1);
                putc(c, fp2);
            }
            fclose(fp2);
            break;
        }
    }
}

Everything is fine till here, my problem is I wish the user could select text with the shift key and navigate in the text with the up/down arrows...

The standard C99 or C11 (read the n1570 draft specification) does not know about keyboards or buttons. You probably need some operating system related library.

Assuming your OS is POSIX (eg Linux), you want to know more about terminals and ttys (so read the tty demystified page); then you should use a library like ncurses (but then don't use any <stdio.h> function for terminal I/O).

Or you want some graphical user interface , then better use some graphics library or toolkit (such as GTK , SDL , Qt etc....).

You will need many months (or years) of work to make a text editor. So study the source code of existing free software editors for inspiration.

Everything is not fine here. Your program has multiple issues:

  • You do not define c , fp1 , fp2 , fn ...
  • You use scanf() in an unsafe way.
  • You dont check for EOF when reading from stdin .
  • You store the . into temp.txt
  • Also check Why is “while ( !feof (file) )” always wrong?
  • Using a single character . as the final mark has side effects: you will not be able to use that character in the file, that's a strong limitation. You should at least only consider . at the start of a line.

Here is an improved version:

int Create(void) {
    FILE *fp1, *fp2;
    char fn[1024];
    int last = '\n', c;

    fp1 = fopen("temp.txt", "w+");
    if (fp1 == NULL) {
        printf("Cannot create file temp.txt\n");
        return -1;
    }
    printf("\n\tEnter the text and press '.' to save\n\n");
    while ((c = getchar()) != EOF && (c != '.' || last != '\n')) {
        fputc(c, fp1);
        last = c;
    }
    for (;;) {
        printf("\n\tEnter then new filename: ");
        if (scanf(" %1023[^\n]%*c", fn) != 1) {
            printf("input error\n");
            fclose(fp1);
            return -2;
        }
        fp2 = fopen(fn, "w");
        if (fp2 == NULL) {
            printf("Cannot create output file %s\n", fn);
        } else {
            break;
        }
    }
    rewind(fp1);
    while ((c = getc(fp1)) {
        putc(c, fp2);
    }
    fclose(fp2);
    fclose(fp1);
    return 0;
}

If you wish to write an interactive editor, you need to set the terminal in raw mode with stty() and use a library such as ncurses to handle full screen output and cursor key input. You can also assume the terminal supports ANSI escape sequences and hard code input and output accordingly.

Such a project is quite an endeavor. I strongly suggest you look at existing open source editors, read the source code and learn how they handle various tasks. I personally co-authored an Emacs clone called qemacs (for Quick Emacs). You can read about it and get the source code from http://qemacs.org , but it is a large project to tackle for a beginner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM