Jump to content

Need to understand the part of the code

- - - - -

  • Please log in to reply
5 replies to this topic

#1
saumya

saumya

    Newbie

  • Members
  • PipPip
  • 12 posts
void addmember()

  {

     clrscr();

     [COLOR="red"]rewind(fm);[/COLOR]

     [COLOR="red"]while( fread(&M,sizeof(M),1,fm)==1)[/COLOR]

     ;

     M.mid+=1;

    [COLOR="red"] fseek(fm,0,SEEK_END);[/COLOR]

     printf("\n\t Enter Name::");

     fflush(stdin);gets(M.memName);  M.memName[19]=' ';  M.memName[20]='\0';

     printf("\n\t Enter Add::");

     fflush(stdin);gets(M.memAdd);   M.memAdd[29]=' ';   M.memAdd[30]='\0';


     getdate(&d);

     M.ms.year=d.da_year;     M.ms.day=d.da_day;     M.ms.mon=d.da_mon;

     M.me.day=M.ms.day;       M.me.year=M.ms.year;   M.me.mon=(M.ms.mon+6);

     /*To Set Month After Dec To Jan*/

     if(M.me.mon>12)

      {

	M.me.year+=1;  M.me.mon=(M.me.mon-12);

      }

     /*If Say The Old Date is 31/12/2003 i.e 31 Dec Then   The New Date is 31/06/2004 i.e 31 Jun

     But Jun consists of Only 30 days so we shall add 1 day extra so the correct Date is 01/07/2004 */

     if(M.me.day==31)

     { /*Months Like Apr,Jun,Sep, & Nov have 30 days*/

       if(M.me.mon==4 || M.me.mon==6 || M.me.mon==9 || M.me.mon==11)

       {

	M.me.day=1;

	M.me.mon+=1;

       }


       /*For the Month of Feb there are 28 OR 29 days*/

       if(M.me.mon==2)

	{/*For Years like(1996,2000,2004 etc there are 29 days in Feb */

	 if( (M.me.year%4)==0)

	  {

	    M.me.day=31-29;   M.me.mon+=1;

	  }

	 else

	  {

	    M.me.day=31-28;  M.me.mon+=1;

	  }

	}

     }

     [COLOR="red"]fwrite(&M,sizeof(M),1,fm);

[/COLOR]     transac1(M.mid,'A');

  }

Need to understand the part of the code....especially the part in red font

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Part in red:
rewind is C function that rewinds (resets )file pointer to begging of file. Then in while loop it reads binary data from file to structure M. With fseek you can set where file pointer points to and there it points to end of file. fwrite is like fread only that it writes binary data to file. It writes data to end since file pointer was pointed to end with fseek.

rewind
fread
fseek
fwrite
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Double post somehow, ignore this one.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
saumya

saumya

    Newbie

  • Members
  • PipPip
  • 12 posts
when this function in the above code..., fread(&M,sizeof(M),1,fm) will return 1

#5
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,



 rewind(fm);


rewind function places the file pointer to the start of the file. So when you have called this function, you ll start reading the file from the first byte.

More details are on www.cplusplus.com/reference/clibrary/cstdio/rewind/


 while( fread(&M,sizeof(M),1,fm)==1)

     ;

    


In above code, file is being read up to bytes specified by sizeof(M) until there are no more bytes to read.
fread is the function to read data from file. You can find more details on

www.cplusplus.com/reference/clibrary/cstdio/fread/


 fseek(fm,0,SEEK_END);



the above code places the file pointer to the end of the File.
fseek is a function to move file pointer within the file. You can find more details on

fseek - C++ Reference


    fwrite(&M,sizeof(M),1,fm);



The above code writes bytes in 'M' buffer upto bytes specified by sizeof(M).
fwrite is used to write bytes to a file. You can find more details on

fwrite - C++ Reference

I hope this helps!

Munir

#6
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts

Quote

when this function in the above code..., fread(&M,sizeof(M),1,fm) will return 1

Quote

The total number of elements successfully read is returned as a size_t object, which is an integral data type.
If this number differs from the count parameter, either an error occured or the End Of File was reached.
You can use either ferror or feof to check whether an error happened or the End-of-File was reached.
You are reading 1 element of data, fread() will return 1 indicating that it successfully read 1 element until there is more elements to read.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users