3

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;

}

alk
  • 69,737
  • 10
  • 105
  • 255
Emrah
  • 67
  • 1
  • 1
  • 6

2 Answers2

8

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, ...
alk
  • 69,737
  • 10
  • 105
  • 255
0
int fscanf ( FILE * stream, const char * format, ... );

it reads formatted input from a stream. and stdin is a standard input stream

and fseek is used for Sets the position indicator associated with the stream to a new position.

SEEK_SET is a flag which used for set position from beggining of file

Example of fseek

#include <stdio.h>

int main ()
{
  FILE * pFile;
  pFile = fopen ( "example.txt" , "wb" );
  fputs ( "Fseek Hello World." , pFile );
  fseek ( pFile , 9 , SEEK_SET );
  fputs ( "no" , pFile );
  fclose ( pFile );
  return 0;
}

ouptut: Fseek Helno World

developer.ahm
  • 180
  • 10