0
I have the following code:
#include <stdio.h>
int main(void){
FILE *ptr_file;
ptr_file=fopen("archive.txt", "r");
short size=0;
char c;
while(!feof(ptr_file)){
c=getc(ptr_file);
if(c=='\n'){
size++;
}
}
ptr_file=freopen("archive.txt", "r", ptr_file);
printf("Size=%d\n\n", size);
while(!feof(ptr_file)){
c=getc(ptr_file);
if(c=='\n'){
size--;
}
}
printf("Size=%d\n\n", size);
fclose(ptr_file);
return 0;
}
Basically this code has the function of reading the file until its end (EOF
) counting the number of lines and then reading the file again until its end, but this time decrementing the variable indicating the number of lines incremented in the loop previous. Ok, where do I go with this? Note that if I take the function freopen
the code does not work as expected, just the second loop does not rotate, but now why it does not rotate? Why is it necessary to use the function freopen
in that situation?