Program Copy File

CCBeginner
Practice Now

Introduction

In this lab, we will create a C program to copy the content of one file to another file. We will read from the source file and write the contents to the destination file.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FileHandlingGroup(["`File Handling`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/ControlFlowGroup -.-> c/while_loop("`While Loop`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FileHandlingGroup -.-> c/create_files("`Create Files`") c/FileHandlingGroup -.-> c/write_to_files("`Write To Files`") c/FileHandlingGroup -.-> c/read_files("`Read Files`") subgraph Lab Skills c/output -.-> lab-123311{{"`Program Copy File`"}} c/variables -.-> lab-123311{{"`Program Copy File`"}} c/data_types -.-> lab-123311{{"`Program Copy File`"}} c/operators -.-> lab-123311{{"`Program Copy File`"}} c/if_else -.-> lab-123311{{"`Program Copy File`"}} c/while_loop -.-> lab-123311{{"`Program Copy File`"}} c/pointers -.-> lab-123311{{"`Program Copy File`"}} c/create_files -.-> lab-123311{{"`Program Copy File`"}} c/write_to_files -.-> lab-123311{{"`Program Copy File`"}} c/read_files -.-> lab-123311{{"`Program Copy File`"}} end

File Structure

Create a new C file named main.c. This file will contain the program logic.

Include the necessary libraries

We need to include the stdio.h library in our program to work with files.

#include <stdio.h>

Declare file pointers

We need to declare two file pointers, one for the source file and one for the destination file.

FILE *fp1, *fp2;

Open source file

We need to open the source file for reading. If the file cannot be opened, we will print an error message and exit the program.

if ((fp1 = fopen("source.txt", "r")) == NULL) {
    printf("\nFile cannot be opened.");
    return;
}

Open destination file

We need to create and open the destination file for writing.

fp2 = fopen("destination.txt", "w");

Copy file contents

We will read the source file character by character and write into the destination file until the end of the file is reached.

char ch;
while ((ch = fgetc(fp1)) != EOF) {
    fputc(ch, fp2);
}

Close the files

After copying the contents, we need to close both the files.

fclose(fp1);
fclose(fp2);

Summary

In this lab, we learned how to read contents of one file and write them to another file. We used the fopen() function to open files and fgetc() and fputc() functions to read and write file contents. It is essential to close the files after completing the task using the fclose() function.

Other C Tutorials you may like