File Handling in C – Set 3

In the last instructional exercise, I enlightened you regarding the program to ascertain the number of spaces, spaces, tabs and characters.

You may have seen that I have utilized the comparable rationale of document perusing program in that instructional exercise.

Today I will proceed with the journey of learning record information yield work by indicating you a program to duplicate substance starting with one document then onto the next.

Document Handling in C

C program to duplicate substance of one document to another record

Before composing the program I might want to acquaint you with one new capacity fputc(). It’s a partner of fgetc() work. As its name recommends this capacity is utilized to think of some substance in the record. Consider the underneath program.

#include<stdio.h>
#include<stdlib.h>
 
void main() 
{
 FILE *fsource,*ftarget;
 char ch;
 
 fsource=fopen("demo.txt","r");  
 if(fsource==NULL)  
 {
  puts("Error while opening source file");   
  exit(0); 
 }  
 
 ftarget=fopen("new.txt","w");
 
 while(ch!=EOF)  
 {   
  ch=fgetc(fsource);
  fputc(ch,ftarget);  
 }  
 
 fclose(fsource);  
 fclose( ftarget); 
}

Output

This program will copy contents of demo.txt file to new.txt.

Clarification

In this program I have made two record structure pointer named as fsource and ftarget. As its name recommends they are utilized for source and target records. I have additionally announced one character variable ch.

After that, I have checked whether the source document is effectively opened for perusing or not. I have utilized two if explanations for that. This can be checked by putting the condition (file_pointer_name==NULL). The equivalent checking should be possible for the second document which we are utilizing for composing reason.

Presently I have begun while circling and inside it fgetc() work is utilized to bring one character from the source record and the composing that character to target document utilizing fputc() work.

The while circle will proceed till the record pointer fsource ranges to end of document demo.txt. The end is experienced when the estimation of ch become EOF.

In the past I have shut both the records utilizing fclose() capacities.

Note: Generally we utilize support to think of some substance into some document since it will be wasteful to access plate over and over to compose only one character. The usefulness of cushion will be like the document understanding project.

Record Opening Modes in C

In the above model, I have opened the source record with understanding mode and target document with composing mode. Anyway, there are some more modes that are accessible in C. Lets study their utilization as well.

“r” – It is utilized for perusing from document. It looks for the record on the plate. In this mode fopen() returns NULL in the event that it can’t open the document else it arrangement the pointer to point the primary character of the record.

“w” – It is utilized for composing into the record. It looks through the record inside the plate, in the event that it is missing, at that point it will make one on the circle.

“a” – It does comparable assignment like “w”. Utilized for adding, that implies it focuses the pointer toward the finish of the record for composing.

“r+” – Open record for both perusing and composing. The document should as of now exist.

“w+” – Open record for both perusing and composing. Makes another record if the document doesn’t exist.

“a+” – Open document for perusing and affixing reason.

Note: In all the above modes if fopen() work can’t open the document because of some explanation then it will return NULL.

Leave a Comment

error: Alert: Content is protected!!