---
title: "Copy contents of one file to another"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-copy-contents"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-copy-contents
site: BCT Engineering
---

# Copy contents of one file to another

Computer Programming · Chapter 10: Data Files

- Asked in: 2074 Chaitra, 8 marks; 2072 Chaitra, 6 marks (asked 2 times)
- Video: [C_131 C program to copy content of one file into another file | File Handling in C](https://www.youtube.com/watch?v=WjUPmLZSsO0) — Jenny's Lectures CS IT
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/file-copy-contents
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

Write a program in C to copy content of one file source.txt to another destination.txt. (Read information of a file named "data.txt" and write its contents to another file "record.txt".)

## Short answer

Open source file in read mode and destination in write mode, checking for NULL. Read characters with fgetc until EOF, writing each with fputc. Use int for the character to detect EOF. Close both files.

## Answer

**Logic.**
1. Open the source file in read mode `"r"` and the destination in write mode `"w"`, checking both for `NULL`.
2. Read one character at a time with `fgetc()` until `EOF` and write it with `fputc()`.
3. Close both files.

The variable that holds the character is an `int` (not `char`), so that `EOF` can be detected correctly. The same program works for "data.txt" to "record.txt" by changing the two file names.

```c
#include <stdio.h>

int main(void)
{
    FILE *src, *dst;
    int ch;

    src = fopen("source.txt", "r");
    if (src == NULL) {
        printf("Cannot open source.txt\n");
        return 1;
    }
    dst = fopen("destination.txt", "w");
    if (dst == NULL) {
        printf("Cannot create destination.txt\n");
        fclose(src);
        return 1;
    }
    while ((ch = fgetc(src)) != EOF)       /* read one character at a time */
        fputc(ch, dst);

    fclose(src);
    fclose(dst);
    printf("File copied successfully\n");
    return 0;
}
```

**Sample run** (source.txt has three lines; destination.txt has the same three lines afterwards)

```text
File copied successfully
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which modes are used to open the source and destination files for copying?
   A. "w" for source and "r" for destination
   B. "r" for source and "w" for destination
   C. "a" for both
   D. "r" for both
   Answer: B. The source is read and the destination is written. (Key point: Modes used)

2. Why is the variable holding the character declared int and not char?
   A. So EOF can be detected correctly
   B. To save memory
   C. Because fputc needs int only
   D. So it can store strings
   Answer: A. EOF is an int value outside the char range; a char variable could confuse it with a valid character. (Key point: Type of ch)

3. Which loop copies the file?
   A. while ((ch = fgetc(src)) != EOF) fputc(ch, dst);
   B. while (ch != EOF) fgetc(dst);
   C. for (;;) fgetc(src);
   D. while (fputc(ch, src)) fgetc(dst);
   Answer: A. Each character read from src is written to dst until EOF. (Key point: Copy loop)

4. Why are both fopen() results checked for NULL?
   A. To speed up copying
   B. Because opening can fail, for example when the source does not exist
   C. To convert the mode
   D. To find the file size
   Answer: B. Using a NULL pointer would crash; the program should report the error and stop. (Key point: Checking files)

## Related in Computer Programming

- [RECORD.txt item cost with VAT](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-record-txt-invoice) — Computer Programming, chapter 10
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [96 students record; pass marks](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-96-students-ioe-txt) — Computer Programming, chapter 10
- [Copy string converting lower to upper using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-array-elements-copy-string-upper) — Computer Programming, chapter 9, asked 3×
- [INVENTORY file of items](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-inventory) — Computer Programming, chapter 10
- [Employee text file; sort into binary file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-employee-sorted-binary) — Computer Programming, chapter 10

## Related in other subjects

- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Student file read/write and modify by roll](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-modify-roll) — Object Oriented Programming, chapter 8
- [8085 ALP: copy data within a range](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-copy-range) — Microprocessor, chapter 2
- [Pycnometer data, water content, γsub](https://www.bctengineer.com/question.md?key=semester_4%2Fsoil_mechanics%3Apycnometer-water-content-submerged) — Soil Mechanics, chapter 2
