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.
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.
– Bruno Marques
@Brunomarques Reply edited. I think it should now be round.
– Victor Stafusa
Thank you very much!
– Bruno Marques