Simple text editor in C

Asked

Viewed 1,711 times

2

I have the following exercise proposed:

Make a program that mimics a text editor. Initially you will read the data typed by the user (lines of text) and create a vector in memory where the texts provided by the user will be stored (text of 1 up to a maximum of 50 lines). The user will write his or her text, ending with a line where he will write only the 'end' word, which determines that he no longer wishes to type lines of text. Thus, the final text can have a variable number of lines, between 1 and 50. Save the content stored in memory in this vector, in a text file on disk.

But I’m not getting the string vector logic right, not to mention that the typed text is not inserted in the file . txt

Follows below my code until the moment.

#include <stdio.h>
#include <string.h>
#include <locale.h>

int main()
{
    setlocale(LC_ALL,"Portuguese");         // acentuação adequada
    FILE *arq;                              // cria variável ponteiro para referenciar na
                                            // memoria um tipo de arquivo txt, no caso o
                                            // user.txt
    char linha[50];
    int i;

    arq = fopen("editor.txt", "w");         // abrindo o arquivo

    if (arq == NULL)                      // testando se o arquivo foi realmente criado
    {
        printf("Erro na abertura do arquivo!");
        return 1;
    }
    printf("Digite um texto de no máximo 50 linhas.\n");
    for (i = 0; i < 50; i++)            //contador de linhas
    {
        fgets(linha, i, arq);           //armazenando no arquivo .txt as linhas digitadas
        if (strcmp(linha,'FIM')==0)     //se digitado "FIM" terminar o texto
        {
            i = 50;
            printf("Você terminou o seu texto.");
        }
    }
    fclose(arq);                            // fechando o arquivo
}

From now on, thank you.

1 answer

2


You were on the right track, but you missed a few things:

  1. char linha[50]; does not mean a 50-line text, but a single 50-character line. I think what you wanted was something like char texto[MAX_LINHAS][COMPRIMENTO];, where MAX_LINHAS is 50 and COMPRIMENTO is the maximum size of each line.

  2. fgets serves to read, only that you want to read what the user type, and not read the contents of the file. The second parameter is the maximum size of the text to be typed, not the line number. Therefore, you should use fgets(texto[i], COMPRIMENTO, stdin);

  3. For strings, use double quotes. So, it should be "FIM" instead of 'FIM'. It is important to understand the difference between single quotes (for characters that can be represented numerically) of double quotes (for strings).

  4. After reading the user’s text, save it to the file. To do this, you can use the fprintf.

  5. The fgets includes the "\n" at the end of the text the user enters (if not too large). Therefore, it is necessary to remove it.

I think your code should look like this:

#include <stdio.h>
#include <string.h>
#include <locale.h>

#define MAX_LINHAS 50
#define COMPRIMENTO 200

int main() {
    setlocale(LC_ALL,"Portuguese");
    char linha[MAX_LINHAS][COMPRIMENTO];

    int linhas_lidas;

    printf("Digite um texto de no máximo %d linhas.\n", MAX_LINHAS);
    for (linhas_lidas = 0; linhas_lidas < MAX_LINHAS; linhas_lidas++) {
        fgets(texto[linhas_lidas], COMPRIMENTO, stdin);

        // Remove o \n do final, se houver.
        int t = strlen(texto[linhas_lidas]);
        if (texto[linhas_lidas][t - 1] == '\n') {
            texto[linhas_lidas][t - 1] = 0;
        }

        if (strcmp(texto[linhas_lidas], "FIM") == 0) {
            printf("Você terminou o seu texto.");
            break;
        }
    }

    FILE *arq = fopen("editor.txt", "w");

    if (arq == NULL) {
        printf("Erro na abertura do arquivo!");
        return 1;
    }

    for (int i = 0; i < linhas_lidas; i++) {
        fprintf(arq, "%s\n", texto[i]);
    }
    fclose(arq);

    return 0;
}
  • Thank you very much! the only thing that is still not working is the comparison with the "END", I’ll give you another search on how this works. but you have helped me a lot, mainly to better understand the concepts of these file manipulation functions.

  • 1

    @Brunomarques Reply edited. I think it should now be round.

  • Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.