/* Program to perform I/O in a given file using Integer I/O */
#include<stdio.h>
main()
{
//declaring pointer to file
FILE *fp;
int num;
char chr='\n';
printf("------------Writing in file:-----------------\n");
/* Writing in File*/
//Opening file for writing
fp=fopen("number.dat","wb");
//Checking file exists or not
if(fp==NULL)
{
printf("Error in opening file:\n");
return 0;
}
else
//If the file exists then writing numbers into it
{
printf("Writing numbers in File...");
for(num=1;num<=10;num++)
{
putw(num,fp);
}
printf("Done...");
} //Closing the file
fclose(fp);
/* Reading in File*/
//Opening file for reading printf("\n\n------------Reading from file:-----------------\n");
fp=fopen("number.dat","rb");
if(fp==NULL)
{
printf("Error in opening file:\n");
return 0;
} //If the file exists then reading through it
else
{
printf("Text found in the file is:\n");
while((num=getw(fp))!= EOF)
printf("%d \n",num);
} //closing the file fclose(fp); }
0 comments: