Plain text editor in C

2

I have the following proposed exercise:

  

Make a program that mimics a text editor. Initially you will read the data entered by the user (lines of text) and create a vector in memory where the texts provided by the user will be stored (text from 1 to a maximum of 50 lines). The user will write his text, ending with a line where he will only write the word 'END', which determines that he no longer wants to enter lines of text. Therefore, the final text can have a variable number of lines, between 1 and 50. Save the contents stored in memory in this vector, in a text file to disk.

But I'm not hitting on the string vector logic, not counting the typed text is not inserted into the .txt file

Follow my code below so far.

#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
}

Thanks in advance.

    
asked by anonymous 12.06.2018 / 04:41

1 answer

2

You were on the right track, but missed some things:

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

  • fgets is for reading, only 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 entered, not the line number. So you should use fgets(texto[i], COMPRIMENTO, stdin);

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

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

  • The fgets includes " \n " at the end of the text that the user types (if it is 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;
    }
    
        
    12.06.2018 / 05:05