17.9 – Appending and copying files

by subbu on October 11, 2014

Appending to file

Adding contents to the tail of an existed file is called appending.

Generally when we open the file in write mode then the  contents are overwritten if the file with the same name is already existed but will be appended when the file is opened in append mode.

A file can be opened in append mode using the flag “a” or “a+”. Here flag “a” opens the file in append mode, where as “a+” opens the file in append/read mode.

#include<stdio.h>
int main()
{
 FILE *p;
 char fname[20],ch;
 printf("Enter the file name:");
 scanf("%s",fname);
 p=fopen(fname,"a+");             /* opened the file in append/read mode */
 /* Writing onto the file */
 while(1)
 {
   ch=getchar();
   if(ch==-1)
     break;
   fputc(ch,p);
 }
 printf("1 file created...\n");
 getchar();
 printf("\nThe contents of file are..");
 rewind(p);
 /* reading from the file */
 while(1)
 {
   ch=fgetc(p);
   if(ch==-1)
     break;
   printf("%c",ch);
 }
 fclose(p);
 return 0;
}

Execution 1:
Enter the file name: subbu
igate solutions
Million street
Mexico City
{end of file character}
1 file created…

The contents of file are..
igate solutions
Million street
Mexico City

Here during the first execution, file “subbu” is created with the given contents (igate solutions) as if file is opened in write mode.

Execution 2:
Enter the file name:subbu
mgate solutions
Behind Manasa theatre
Kavali-524201
{end of file character}
1 file created…

The contents of file are..
igate solutions
Million street
Mexico City

mgate solutions
Behind Manasa theatre
Kavali-524201

Here we can observe that, the details (mgate solutions) given during the second execution are appended to the initial details (igate solutions)

Copying a file

We can copy a file by opening the existed file in read mode and creating a new file in write mode. Here in the following example we are going to open an existed file (source) in read mode using “r” flag and new file (target) would be created in write/read mode using the flag “w+” because we need to test the target file by reading and printing the copied contents.

FILE *s,*t;

Here two file pointers are declared that are one for reading and another fire writing

s=fopen(sfname,"r");

Opening the source file in read mode

t=fopen(tfname,"w+");

Creating a target file in write/read mode

while(1)
{
ch=fgetc(s);
if(ch==-1)
break;
fputc(ch);
}

The above loop reads character by character from the source file through file pointer “s” and writing character on to the target file through the file pointer “t”. Loop ends on reaching the end of file

copying a file

Let us put all together as a program

#include<stdio.h>
int main()
{
 char sfname[20],tfname[20];
 char ch;
 FILE *s,*t;
 printf("Enter the source file name:");
 scanf("%s",sfname);
 printf("Enter the target file name:");
 scanf("%s",tfname);
 s=fopen(sfname,"r");        /* opening source file read mode */
 t=fopen(tfname,"w+");     /* opening target file in write/read mode */
 /* copying from source to target */
 while(1)
 {
  ch=fgetc(s);
  if(ch==-1)
    break;
  fputc(ch,t);
}
fclose(s);
printf("1 file copied..\n\n");
getchar();
printf("Contents of target file...");
rewind(t);
/* reading from target file */
while(1)
{
 ch=fgetc(t);
 if(ch==-1)
   break;
 printf("%c",ch);
}
fclose(t);
return 0;
}

Execution
Enter the source file name:subbu
Enter the target file name:india
1 file copied..

Contents of target file…
igate solutions
Million street
Mexico City

mgate solutions
Behind Manasa theatre
Kavali-524201

Previous post:

Next post: