I just started learning files in c few days ago. In order to understand how to manipulate correctly the functions, I wrote a code who create a new file (w+) read a string and using fputs() put it in the file. Using a function it find how many characters are there. the problem is if I don't rewind before calling the function, the output returned is very large compared to the string the output+string is exactly 4096 every time no matter how big the array. I don't understand why it should just return 0 if I don't rewind the pointer, and why it doesn't return 4096 when I rewind it, instead it returns the correct answer. Here is my code:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int daft(FILE* f){
    int s=0,c;
    while((c=fgetc(f))!=EOF){
        if(!isspace(c))
        s++;
    }
    return s;
}
int main(){
    char *ch;
    ch=malloc(20*sizeof(char));
    FILE *f;
    f=fopen("test.txt","w+");
    if(f!=NULL){
        gets(ch);
        fputs(ch,f);
        printf("n= %d\n",daft(f));
        fclose(f);
        
    }
    free(ch);
    return 0;
    
}
 
    