Pages

Saturday, December 4, 2010

Reading and writing at the same time.

All the above examples were either reading or writing a file. None of the examples were doing both the operations at the same time. Therefore to write a file and then to read it we will have to create two separate programs. Let's see if we can write and read a file in a single program The code given below will first create a file using the "w" mode. After the writing is complete, we will close the file and reopen it in "r" mode. A file can not be opened in two modes at the same time therefore we must close it before changing the mode.

Example 1 : To write and read a single string in a single program

#include
#include
void main()
{

FILE *fp; // Line 1
fp=fopen("example","w"); // Line 2
char text[50]; // Line 3
printf("\nEnter text to be stored in the file (max 50 chars) "); // Line 4
gets(text); // Line 5
fputs(text,fp); // Line 6
fclose(fp); // Line 7
fp=fopen("example","r"); // Line 8
fgets(text,50,fp); // Line 9
printf("\nThs is the content of the file "); // Line 10
printf("\n%s",text); // Line 11
fclose(fp); // Line 12
}

Description

Line 4 and 5
Asks the user to enter the data that has to be stored in the file.
Line 6
Stores the text entered by the user in the file using the "fputs()" function.
Line 7
Till now the file is open in "w" mode, reading in this mode is not possible, so we are closing this file from its current mode.
Line 8
To read the file we are opening the file in "read" mode.
Line 9
Now that the file is open, the function "fgets()" is reading the first 50 chars of the file.
Line 11
The read data is printed on the screen.
Line 12
The file is closed again, so that the memory is freed.


Example 2 : To write and read multiple strings the data in a single program.

#include
#include
void main()
{
FILE *fp; // Line 1
char ch; // Line 2
fp=fopen("example","w"); // Line 3
char ename[10]; // Line 4
do // Line 5
{
fflush(stdin); // Line 6
printf("\nEnter name of the employee "); // Line 7
gets(ename); // Line 8
fflush(stdin); // Line 9
fputs(ename,fp); // Line 10
printf("\nContinue (y/n) "); // Line 11
scanf("%c",&ch); // Line 12
}while(ch=='y' || ch=='Y'); // Line 13
fclose(fp); // Line 14
fp=fopen("example","r"); // Line 15
printf("\nThs is the content of the file "); // Line 16
while(fgets(ename,10,fp)!=NULL) // Line 17
printf("\n%s",ename); // Line 18
fclose(fp); // Line 19
}

Description
The above code works similar to the previous example. The difference is that this program is asking the user to enter multiple values inside the loop.
Line 7 and 8
Asks the user to enter the data that has to be stored in the file.
Line 10
Stores the text entered by the user in the file using the "fputs()" function.
Line 14
Closes the file so that it can be reopened.
Line 17
To read the file the function "fgets()" has been used with the while loop. The while loop has been used since we don't know how much data is there to be read. The loop will continue to read the file till it reachs the end of the file.
Line 18
The read data is printed on the screen.
Line 19
The file is closed again, so that the memory is freed.

0 comments:

Post a Comment