What is stdin and how is it being used with fscanf?

Emrah picture Emrah · Aug 20, 2016 · Viewed 9.4k times · Source

I dont understand the connection between stdin and fscanf

struct musteri{
    int no;
    char name[40];
    char surname[25];
    double arrear;

};



 int main() {

    struct musteri hesapBilgi={0,"","",0.0};

    FILE *ptr;
    if((ptr=fopen("eleman.txt","r+"))==NULL){
        printf("error");
    }

    else{
        printf("\n enter a no if you want exit enter 0 -->");   
        scanf("%d",&hesapBilgi.no); 

scanf take a input and put the no in sturct musteri

while(hesapBilgi.hesapno !=0){


            printf("enter a surname name and arrear --->"); 
            fscanf(stdin,"%s%s%lf",hesapBilgi.surname,hesapBilgi.name,&hesapBilgi.arrear);

in here does the fscanf reading from data in file ? or Something else is going on?

        fseek(ptr,(hesapBilgi.no-1)*,sizeof(struct musteri),SEEK_SET); 

what is fseek doing ?

        fwrite(&hesapBilgi,sizeof(struct musteri),1,ptr);

        printf("enter a no :");
        scanf("%d",&hesapBilgi.no);


    }
    fclose(ptr);
}


return 0;

}

Answer

alk picture alk · Aug 20, 2016

From the docs (man scanf):

The scanf() function reads input from the standard input stream stdin, fscanf([FILE * stream, ...]) reads input from the stream pointer stream [...]

stdin is a FILE*. It is an input stream.

From the docs (man stdin)

Under normal circumstances every UNIX program has three streams opened for it when it starts up, one for input, one for output, and one for printing diagnostic or error messages. These are typically attached to the user's terminal [...]

So

scanf( ...

in fact is equivalent to

fscanf(stdin, ...