DEV Community

Cover image for Creating a File Copy Program in C
Labby for LabEx

Posted on

1

Creating a File Copy Program in C

Introduction

MindMap

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.

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>
Enter fullscreen mode Exit fullscreen mode

Declare file pointers

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

FILE *fp1, *fp2;
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Open destination file

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

fp2 = fopen("destination.txt", "w");
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

Close the files

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

fclose(fp1);
fclose(fp2);
Enter fullscreen mode Exit fullscreen mode

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.


πŸš€ Practice Now: Program Copy File


Want to Learn More?

Google AI Education track image

Build Apps with Google AI Studio 🧱

This track will guide you through Google AI Studio's new "Build apps with Gemini" feature, where you can turn a simple text prompt into a fully functional, deployed web application in minutes.

Read more β†’

Top comments (1)

Collapse
 
pauljlucas profile image
Paul J. Lucas β€’

Copying files character-by-character is really inefficient. Any real-world program needs to accept the file to be copied via command-line arguments as well as check for errors.

Google AI Education track image

Build Apps with Google AI Studio 🧱

This track will guide you through Google AI Studio's new "Build apps with Gemini" feature, where you can turn a simple text prompt into a fully functional, deployed web application in minutes.

Read more β†’

πŸ‘‹ Kindness is contagious

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple β€œthank you” or question in the comments goes a long way in supporting authorsβ€”your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay