0
I have the following problem to solve the college, but so far I have not seen a valid solution... I made the following code up to the time of sending since help request.
//
// 2.c
// IFTM Exercises
//
// Created by Lelre Ferreira on 10/17/19.
// Copyright © 2019 Lelre Ferreira. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, const char * argv[]){
FILE *p;
char texto[20];
char pular = '\n';
int i = 0, count = 0, rpt = 1;
p = fopen("Text.txt", "w");
if (p == NULL) {
printf("Erro na leitura");
exit(1);
}
while(rpt != 0){
printf("Digite uma linha: ");
scanf("%s", texto);
fprintf(p, "%s", texto);
printf("Deseja repetir? ");
scanf("%d", &rpt);
}
fclose(p);
p = fopen("Text.txt", "w");
fread(&texto, sizeof(char), 20, p);
for (i = 0; i < strlen(texto); i++) {
if (fscanf(p, "%s\n", &texto[i]) == pular) {
count++;
}
}
fclose(p);
printf("Total de linhas: %d\n", count);
return 0;
}
Could someone give me a hint on how to solve the above problem?
It makes no sense to use fread and fscanf mixed in the same file. Certainly fread is not the best option for reading a text file. Another consideration: if the end of the file is detected and the character read immediately before is not a ' n' you should not add 1 to the number of lines?
– anonimo