17.6 – Writing unformated text on to the file

by subbu on October 10, 2014

Writing unformatted text onto the file

fputc() function

As we have learned in the previous session  17.3 Unformatted Console I/O putch() and putchar() are used to write a character onto the screen. Where as fputc() is used to print a character onto the file through the file pointer.

fputc() function

It accepts two arguments that are the character that we want to write on to the file and a file pointer.
It returns a character that is written last time on to the file, returns -1 if any thing goes wrong in writing on to the file

Now let us see how we can write a character onto the file

 
FILE *p;
p=fopen("igate","w");

It creates a file with the name “igate”, in write mode

 
fputc('g',p);

Here fputc() writes a character ‘g’ onto the file “igate” through the file pointer “p”.
We can build a loop to accept text from the keyboard and write onto the file continuously

 
FILE *p;
char ch;
p=fopen("igate","w");
while(1)
{
 ch=getchar();
 fputc(ch,p);
}

Here getchar() reads character by character from the keyboard as loop progresses and stores into the variable “ch”.
Where as fpuc(ch,p) writes the character onto the file through the file pointer.

EOF (End Of File)

The above infinite loop reads and writes the text continuously on to the file but, there should be an end to this. So to terminate the process we generally press ctrl + z in case of DOS or Windows based systems and ctrl + d in case of Linux based systems. The ASCII value of these end of file characters is -1.
EOF is a macro in “stdio.h” which would be replaced with -1 by the preprocessor. So we can use either EOF or -1 to find the end of file and break the infinite loop.

 
FILE *p;
char ch;
p=fopen("igate","w");
while(1)
{
 ch=getchar();
 if(ch==-1)
  break;
 fputc(ch,p);
}

So the above code creates a file with the name “igate” and writes the text given from the keyboard onto the file until user press ctrl + z in DOS or ctrl + d in Linux

fclose() function

At the end of either writing, reading or appending the file must be closed so that all the data written on the stream buffer would be flushed and file operation would be completed.

fclose() function

It accepts the file pointer of the opened file as argument and returns “0” on successfully closing the file, returns -1 if any thing goes wrong in closing the file
So putting all together to accept the text from the keyboard and write onto the file until end of file character is given.

 
#include<stdio.h>
int main()
{
 FILE *p;
 char ch;
 p=fopen("igate","w"); /* opeing file */
 while(1)
 {
  ch=getchar();     /* reading from Console Input */
  if(ch==-1)         /* checking end of file */
    break;
  fputc(ch,p);       /* writing on to file */
 }
 fclose(p);             /* closing the file */
 printf("1 file is created..");
 return 0;
}

Execution in Turbo C
The C programming Language
Second Edition
By Brian W. Kernighan and Dennis M. Ritchie
{ctrl+z} {Enter}
1 file is created..

Execution on gcc
The C programming Language
Second Edition
By Brian W. Kernighan and Dennis M. Ritchie
{ctrl+d}
1 file is created..

writing unformatted text on to the file

  1. A new file with the name “igate” is created by opening the file in write mode (“w”) using fopen() function. It returns the file pointer through which we can write the data onto the file.
  2. getchar() function reads the data from the keyboard character by character as loop progresses.
  3. fputc() function writes the character (ch) on to the file using file pointer “p”.
  4. The if(ch==-1) checks for end of file character, breaks the loop if it is reached.
  5. fclose() closes the file after the completion of write operation.

Wring more professional way

The above program can be further developed by adding some essence to it. To improve the above program further, we can accept the file name from the user and also can be checked whether the file is already existed or not, if so warning user to select another name to avoid the damage to the existed file

#include<stdio.h>
int main()
{
 FILE *p;
 char fname[20],ch;
 printf("Give the file name:");
 scanf("%s",fname);
 p=fopen(fname,"r");   /* opening file in read mode */
 if(p==NULL)               /* if file is not existed         */
 {
   fclose(p);                 /* closing file stream in read mode */
   p=fopen("igate","w"); /* opening file in write mode*/
   while(1)
   {
     ch=getchar();     /* reading from Console Input */
     if(ch==-1)         /* checking end of file */
       break;
     fputc(ch,p);       /* writing on to file */
   }
   fclose(p);
   printf("1 file is created..");
 }
 else                             /* if file is already existed */
 {
  printf("File is already existed..");
  printf("\nTry with other name..");
  fclose(p);
 }
 return 0;
}

Execution1:
Give the file name: america
The C programming Language
Second Edition
By Brian W. Kernighan and Dennis M. Ritchie
{end of file key}
1 file is created..

Execution2:
Give the file name: america
File is already existed.
Try with other name..

Previous post:

Next post: