Text editor in C

             Text editor is a simple application to start with developing applications. A simple text editor makes you to create a file, write the contents to the newly created file. It also add some more data to already existing files.

If you want read the file, open the existing file and read the contents. Finally, exit option also available in this text editor.

Let us implement the simple text editor.

C implementation:

The functionalities are created as options. It includes f_create(),f_write(),f_read(),f_append(), and main() functions.

Here, the implementation uses ‘FILE’ built in pointer to create this text editor in c.

The built-in functions used here: “fopen(),fclose(),fgets(),fputs(),fprintf()”.

C Program:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define max 1000

char text[max];

char ch;

void f_create(const char *fname) {

    FILE *fp = fopen(fname, "w");

    if (fp == NULL) {

            printf("Error creating file.\n");

            return;

    }

    printf("File '%s' is created successfully.\n", fname);

    fclose(fp);

}

void f_write(const char *fname) {

    FILE *fp = fopen(fname, "w");

    if (fp == NULL) {

            printf("Error opening file for writing.\n");

            return;

    }

    printf("Enter text to write (end ~ with newline):\n");

    while (fgets(text, max, stdin)) {

            if (strcmp(text, "~\n") == 0) break;

            fputs(text, fp);

    fclose(fp);

    printf("Text is written to '%s'.\n", fname);

}

void f_append(const char *fname) {

    FILE *fp = fopen(fname, "a");

    if (fp == NULL) {

            printf("Error opening file for appending.\n");

            return;

    }

    printf("Enter text to append (end'~'with new line):\n");

    while (fgets(text, max, stdin)) {

            if (strcmp(text, "~\n") == 0) break;

            fputs(text, fp);

    }

    fclose(fp);

    printf("Text appended to '%s'.\n", fname);

}

void f_read(const char *fname) {

    FILE *fp = fopen(fname, "r");

    if (fp == NULL) {

            printf("Error opening file for reading.\n");

            return;

    }

    printf("Contents of '%s':\n", fname);

    while ((ch = fgetc(fp)) != EOF) {

            putchar(ch);

    }

    fclose(fp);

}

 int main() {

    int choice;

    char fname[100];

     while (1)

    {

        printf("\n--- Simple Text Editor ---\n");

        printf("1. Create File\n");

        printf("2. Write to File\n");

        printf("3. Append to File\n");

        printf("4. Read File\n");

        printf("5. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

                getchar();

         if (choice == 5) break;

        printf("Enter filename: ");

        fgets(fname, sizeof(fname), stdin);

        fname[strcspn(fname, "\n")] = 0;

        switch (choice) {

            case 1: f_create(fname); break;

            case 2: f_write(fname); break;

            case 3: f_append(fname); break;

            case 4: f_read(fname); break;

            default: printf("Invalid choice.\n");

        }

    }

        printf("Thank you for using Text Editor\n");

        return 0;

}

 Output:

--- Simple Text Editor ---

1. Create File

2. Write to File

3. Append to File

4. Read File

5. Exit

Enter your choice: 1

Enter filename: r1.txt

Created r1.txt Successfully.

--- Simple Text Editor ---

1. Create File

2. Write to File

3. Append to File

4. Read File

5. Exit

Enter your choice: 2

Enter filename: r1.txt

Enter text to write (end ~ with newline)

It is c  world

~

Text is written to r1.txt

--- Simple Text Editor ---

1. Create File

2. Write to File

3. Append to File

4. Read File

5. Exit

Enter your choice: 3

Enter filename: r1.txt

Enter text to append (end'~'with new line):

Keep coding

~

Text appended to r1.txt

--- Simple Text Editor ---

1. Create File

2. Write to File

3. Append to File

4. Read File

5. Exit

Enter your choice: 4

Enter filename: r1.txt

Contents of r1.txt

It is c world

Keep coding

--- Simple Text Editor ---

1. Create File

2. Write to File

3. Append to File

4. Read File

5. Exit

Enter your choice: 5

Thank you for using Text Editor

Yes. The code is completed. Hope, this code is useful to you. Keep developing code like this. Enjoy Coding!!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.