remember that fread and fwrite are basically used to read a block or chunk of data from a file. fread is used to read a block of data from the file. to write a block use fwrite. u can specify the no. of bytes to be read or written to the file, while using that fucntion. An example program which is gvien below is way of how to use a fread fucntion to read a text file all at one and place the contents of the text file into a buffer.
Code:
#include<stdio.h>
int main()
{
FILE *fp;
char *buff;
int size;
if((fp = fopen("test.txt","r"))==NULL)
{
printf("Error: File cannot be opened\n");
getchar();
return 1;
}
else
{
fseek(fp,0,SEEK_END);
size = ftell(fp);
fseek(fp,0,0);
buff = malloc(size * sizeof(char));
fread(buff,1,size,fp);
*(buff+size)='\0';
printf("%s",buff);
fclose(fp);
free(buff);
getchar();
return 0;
}
}
/*my output
this is test file
---- > My test.txt file <----
this is test file
*/
if u are using structure to store the reacords of the student and then writing onto the file. it better use binary file. so that u can read and write in terms of records. as i said earlier using fread/fwrite u can write/read a block of data.
ssharish2005