by Dinesh Thakur Category: File Handling

The fputc function and putc macro are used to write a character to an output stream. Their prototypes are given below.

int fputc (int c, FILE *fp);

int putc ( int c, FILE *fP ) ;

The fputc function converts the character specified as its first argument to unsigned char and writes it to a specified output stream fp.It returns the same character as a value of type int. However, if an error occurs in the write operation, the function return EOF.

The putc macro does exactly what fput c does. However, it is usually implemented as a macro for efficient operation. Note that the parameters of putc should not produce side effects as they may be evaluated more than once, giving undesirable output.

Write a program to copy a text file to another file.

To copy the contents of a file to another, we read the source file one character at a time and write it to the target file until the end-of-file mark is reached in the source file. Assuming fp_source and fp_target as file pointers for source and target file, respectively, the program segment given below copies all the characters in the source file to the target file, including the EOF mark.

/* copy contents of source file to target file */

while ((ch= getc(fp_source)) != EOF)

putc(ch, fp_target);

The complete program to copy a file is given below.

#include <stdio.h>

#include "fileopen.c"

int main()

{

   FILE *fp_source, *fp_target;

   char source[50], target[50];

   int ch;

   /* open source and target files */

   fp_source = fileopen (source, "rt", "Source file name: ");

   fp_target = fileopen (target, "wt", "Target file name: ");

   /* copy contents of source file to target file */

   while ((ch= getc(fp_source)) != EOF)

   putc(ch, fp_target);

   /* close the files */

   fclose(fp_source);

   fclose(fp_target);

   return 0;

}

Write a program to create a new file by concatenating the contents of two text files.

To concatenate two text files and create a new file, we open the source files in read mode and the target file in the write mode. Let the file pointers fp_sourcel and fp_source2 be associated with the source files and file pointer fp_target be associated with the target file. The code to concatenate two files and create the target file is given below.

/* copy contents of first file to target file */

while ((ch= getc(fp sourcel))!= EOF)

putc(ch, fp target);

/* copy contents of second file to target file */

while ((ch= getc(fp_source2)) != EOF)

putc(ch, fp_target);

The complete program is given below. Note that the use of the fileopen function from Example has made this program very concise and readable.

#include <stdio.h>

#include "fileopen.c"

int main()

{

   FILE *fp_sourcel, *fp_source2, *fp_target;

   char sourcel[50], source2[50], target[50];

   int ch;

   /* open the files */

   fp_sourcel = fileopen(sourcel, "rt", "First source file name: ");

   fp_source2 = fileopen(source2, "rt", "Second source file name: ");

   fp_target = fileopen(target, "wt", "Target file name: ");

   /* copy contents of first file to target file */

   while ((ch= getc(fp_sourcel)) != EOF)

   putc(ch, fp_target);

   /* copy contents of second file to target file */

   while ((ch= getc(fp_source2)) != EOF)

   putc(ch, fp_target);

   /* close the files */

   fclose(fp_sourcel);

   fclose(fp_source2);

   fclose(fp_target);

   return 0;

}



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.