Let's make some more programs involving file handling functions. The first program is for copying a file to another. In this program we are going to make a change, instead of "hard coding" the file name, we will ask the user to enter the name of the files. This will give user the option to change the file names every time the program is run.
Example 1 : Copying a file : Char by Char
#include
#include
void main()
{
clrscr(); // Line 1
FILE *fp1,*fp2; // Line 2
char src[12],target[12]; // Line 3
char c; // Line 4
int cnt=0; // Line 5
printf("\nEnter source file name "); // Line 6
scanf("%s",&src); // Line 7
fp1=fopen(src,"r"); // Line 8
if(fp1!=NULL) // Line 9
{ // Line 10
printf("\nEnter new file name "); // Line 11
scanf("%s",&target); // Line 12
fp2=fopen(target,"w"); // Line 13
c=fgetc(fp1); // Line 14
while(c!=EOF) // Line 15
{ // Line 16
fputc(c,fp2); // Line 17
cnt++; // Line 18
c=fgetc(fp1); // Line 19
} // Line 20
printf("\nSuccessfully copied %d chars to %s",cnt,target); // Line 21
fclose(fp1); // Line 22
fclose(fp2); // Line 23
} // Line 24
else // Line 25
printf("\nSource file does not exists"); // Line 26
} // Line 27
Description
Line 2 :
Declares 2 FILE pointers one to hold the source file and second for the target file.
Line 3:
Declares 2 strings named "src" and "target", one to contain the source file name and the other for the target file name.
Line 6 and Line 7 :
Asks the user to enter the name of the file to be copied.
Line 8 :
In this line, the function "fopen()" has been passed two arguments. The first argument is the name of the file, which in this case is the name entered by the user in line no. 6 and 7. Since the file name is saved in the variable "src", this is the first argument. Don't put src in double quotes (" ") because doing this will make C think the name of the source file is "src", which is not the case. "src" is not the name of the file, but the name of the variable which has the source file name. The second argument is the "read" mode since this file will be opened in read mode.
Line 9 :
It is possible that the file name entered by the user is not available. In such a case the value of the variable "fp1" will be NULL. The line no. 9 checks if the value of fp is NULL. If it is then the control will move directly to line no. 24 citing that the "Source file does not exists".
Line 11 and Line 12 :
It the source file exists, the program asks for the target file name. It can be the name of any new file. If there is no file with this name, the program will make a new one. If there is a file it will be overwritten.
Line 13:
The target file is opened in the "write" mode in the pointer "fp2" because we have to write(copy) data in it.
Line 14:
The first character of the source file is read and stored in the variable "c".
Line 15:
Since "c" is not equal to EOF, the program enters the loop.
Line 17:
To write the data to another file, the "fputc()" function has been used to write the same char to the second file pointer, "fp2". If the first char which was read at line no. 14 was "H", it will be written to the target file.
Line 18:
The variable "cnt" is incremented by 1 so that we can print the number of characters copied at the end.
Line 19:
Once the first character is written to the target file, we move on to read the second character. The loop will continue the statements given in the line no. 17 to 19 till all the character of the source file are written to the target file. On one hand we are reading from the source file and on the other hand we are writing the same to
the target file.
Line 21:
After copying the file the value of "cnt" has been printed to let the user know how many character were copied.
Line 22 and Line 23
Both file are closed to release memory.
Line 2 :
Declares 2 FILE pointers one to hold the source file and second for the target file.
Line 3:
Declares 2 strings named "src" and "target", one to contain the source file name and the other for the target file name.
Line 6 and Line 7 :
Asks the user to enter the name of the file to be copied.
Line 8 :
In this line, the function "fopen()" has been passed two arguments. The first argument is the name of the file, which in this case is the name entered by the user in line no. 6 and 7. Since the file name is saved in the variable "src", this is the first argument. Don't put src in double quotes (" ") because doing this will make C think the name of the source file is "src", which is not the case. "src" is not the name of the file, but the name of the variable which has the source file name. The second argument is the "read" mode since this file will be opened in read mode.
Line 9 :
It is possible that the file name entered by the user is not available. In such a case the value of the variable "fp1" will be NULL. The line no. 9 checks if the value of fp is NULL. If it is then the control will move directly to line no. 24 citing that the "Source file does not exists".
Line 11 and Line 12 :
It the source file exists, the program asks for the target file name. It can be the name of any new file. If there is no file with this name, the program will make a new one. If there is a file it will be overwritten.
Line 13:
The target file is opened in the "write" mode in the pointer "fp2" because we have to write(copy) data in it.
Line 14:
The first character of the source file is read and stored in the variable "c".
Line 15:
Since "c" is not equal to EOF, the program enters the loop.
Line 17:
To write the data to another file, the "fputc()" function has been used to write the same char to the second file pointer, "fp2". If the first char which was read at line no. 14 was "H", it will be written to the target file.
Line 18:
The variable "cnt" is incremented by 1 so that we can print the number of characters copied at the end.
Line 19:
Once the first character is written to the target file, we move on to read the second character. The loop will continue the statements given in the line no. 17 to 19 till all the character of the source file are written to the target file. On one hand we are reading from the source file and on the other hand we are writing the same to
the target file.
Line 21:
After copying the file the value of "cnt" has been printed to let the user know how many character were copied.
Line 22 and Line 23
Both file are closed to release memory.
Example 2 : Converting a file to upper case in the target file
#include
#include
#include
void main()
{
clrscr();
FILE *fp1,*fp2;
char src[12],target[12];
char c;
int cnt=0;
printf("\nEnter source file name ");
scanf("%s",&src);
fp1=fopen(src,"r");
if(fp1!=NULL)
{
printf("\nEnter new file name ");
scanf("%s",&target);
fp2=fopen(target,"w");
c=fgetc(fp1);
while(c!=EOF)
{
fputc(toupper(c),fp2); // Line No. 17
cnt++;
c=fgetc(fp1);
}
printf("\n Successfully copied %d chars to %s",cnt,target);
}
else
printf("\nSource file does not exists");
}
Description
The above program is same as the previous example, a minor difference is at line 17 where before writing the data to the target file we have converted the char to upper case. This will create the resultant file in capital letters. To use the "fputc()" we have included the header file " ".
Example 3 : Copying a file String by String
#include
#include
void main()
{
clrscr(); // Line 1
FILE *fp1,*fp2; // Line 2
char src[12],target[12]; // Line 3
char str[12]; // Line 4
printf("\nEnter source file name "); // Line 5
scanf("%s",&src); // Line 6
fp1=fopen(src,"r"); // Line 7
if(fp1!=NULL) // Line 8
{ // Line 9
printf("\nEnter new file name "); // Line 10
scanf("%s",&target); // Line 11
fp2=fopen(target,"w"); // Line 12
while(fgets(str,12,fp1)!=NULL) // Line 13
{ // Line 14
fputs(str,fp2); // Line 15
} // Line 16
printf("\nSuccessfully copied"); // Line 17
} // Line 18
else // Line 19
printf("\nSource file does not exists"); // Line 20
}
Description
Same program is created again, with the "fgets()", "fputs()" function. The "fgets()" inside the loop reads 12 chars at a time, copies them to the target file using "fputs()". The program continues till the end of the source file.
0 comments:
Post a Comment