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.