This is a C program that copies the contents of one file to another file.
The program asks the user for the names of the source file and the output file. It then tries to open the source file for reading and the output file for writing.
If either file cannot be opened, an error message is printed to the console. If both files are successfully opened, the program copies the contents of the source file to the output file character by character, using the getc()
and putc()
functions, until the end of the file is reached (EOF).
Finally, the program prints a message indicating that the operation was successful and closes both files using the fclose()
function.
#include <stdio.h>
int main()
{
char source_file[100], output_file[100], c;
FILE *fp_source, *fp_output;
printf("Source File: ");
scanf("%s", source_file);
printf("Output File: ");
scanf("%s", output_file);
if((fp_source = fopen(source_file, "r")) == NULL)
{
printf("Unable to open source file for reading.\n");
}
else if((fp_output = fopen(output_file, "w")) == NULL)
{
printf("Unable to open destination file for writing.\n");
}
else
{
while((c = getc(fp_source)) != EOF)
{
putc(c, fp_output);
}
printf("Operation OK.\n");
}
fclose(fp_source);
fclose(fp_output);
return 0;
}
Result:
Source File: orginal.txt
Output File: backup.txt
Operation OK.
Process returned 0 (0x0) execution time : 7.140 s
Press any key to continue.
Here's a breakdown of the code:
#include <stdio.h>
This line includes the standard input/output library, which provides functions for working with files.
int main()
{
char source_file[100], output_file[100], c;
FILE *fp_source, *fp_output;
This defines the main function and declares some variables. source_file
and output_file
are character arrays that can hold up to 100 characters each, which will be used to store the names of the source file and the output file. c
is a character variable that will be used to read and write characters from and to the files. fp_source
and fp_output
are pointers to file structures, which will be used to point to the source and output files.
printf("Source File: ");
scanf("%s", source_file);
printf("Output File: ");
scanf("%s", output_file);
These lines ask the user for the names of the source file and the output file, and read them into the source_file
and output_file
variables using the scanf()
function.
if((fp_source = fopen(source_file, "r")) == NULL)
{
printf("Unable to open source file for reading.\n");
}
else if((fp_output = fopen(output_file, "w")) == NULL)
{
printf("Unable to open destination file for writing.\n");
}
else
{
// File copying code goes here
}
These lines try to open the source file and the output file using the fopen()
function. If either file cannot be opened, an error message is printed to the console. If both files are successfully opened, the program enters the else
block, which contains the code to copy the contents of the source file to the output file.
while((c = getc(fp_source)) != EOF)
{
putc(c, fp_output);
}
This is the code that copies the contents of the source file to the output file. The while
loop reads characters from the source file one by one using the getc()
function, and writes them to the output file using the putc()
function. The loop continues until the end of the file is reached (EOF).
printf("Operation OK.\n");
This line prints a message indicating that the operation was successful.
fclose(fp_source);
fclose(fp_output);
return 0;
}
These lines close the source file and the output file using the fclose()
function, and return 0 to indicate that the program has completed successfully.
No comments:
Post a Comment