17.8 – How to perform write/read operations on a file

by subbu on October 10, 2014

So far we know how to read from a file and how to write onto a file. Now we will see how to perform both operations in a single program.

Before going further, we will discourse a file opening mode flag “w+” and rewind() function.

Performing write and read operation (w+)

Generally while writing onto a file, the file active position always there at the end of file. Hence it may not be possible to perform opposite operation like reading from file immediately after the completion of write operation because file active position remains at the end of file.

If we want to read from the file immediately after the completion of write operation then we need to close the file and then must be opened in read mode to keep the file active position at the beginning of file.

FILE *p;
p=fopen("black","w");
------
------   /* write operation */
------
fclose(p);
p=fopen("black","r");
------
------   /* read operation     */
------

Another way of doing the same is opening the file in write/read mode using the flag “w+” and using rewind() function to place the file active position to the beginning of the file after completion of write operation and just before performing the read operation

rewind() function

It is the function sends the file active position to the beginning of file

rewind() function

It accepts the file pointer as argument and sends the active file position to the beginning of the file. It returns nothing.

Now we will see how we can perform write and read operations one after another

FILE *p;
p=fopen("mgate","w+");

Opening the file in write/read mode.

while(1)
{
 ch=gatchar();
 if(ch==-1)
   break;
 fputf(ch,p);
}

The above loop read the text from the console input and writes on to the file through the file pointer until user press end of file key

rewind(p);

It sends the file active position to the beginning of file

sending file pointer to BOF

while(1)
{
 ch=fgetc(p);
 if(ch==-1)
   break;
 printf("%c",ch);
}

The above loop read the data from the file and prints on to the console out.

Now let us put altogether in a program

#include<stdio.h>
int main()
{
 FILE *p;
 char fname[20],ch;
 printf("Enter the file name:");
 scanf("%s",fname);
 p=fopen(fname,"w+"); /* opening the file write/read mode */
 /* writing onto the file */
 while(1)
 {
   ch=getchar();
   if(ch==-1)
     break;
   fputc(ch,p);
 }
 printf("1 file created...");
 getchar();
 printf("The contents of file are..");
 rewind(p);               /* sending active position to the beginning of file */
 /* reading from the file */
 while(1)
 {
   ch=fgetc(p);
   if(ch==-1)
     break;
   printf("%c",ch);
 }
 fclose(p);
 return 0;
}

Execution
Enter the file name:mgate
mgate solutions
Millon Road
Mexico city
{End of file character}
1 file created…

The contents of file are..
mgate solutions
Millon Road
Mexico city

Summery
In the above example we have performed write and read operations one after another without closing file in between, using file opening flag as “w+”

We can also use “a+” to perform append and read operations in a sequence and “r+” to perform read and write operations in a sequence

Previous post:

Next post: