POP Lab Manual

Program 12

Write a C program to copy a text file to another, read both the input file name and target file name. This program demonstrates file handling operations, error checking, and character-by-character file copying using standard C library functions for secure file operations.
File Operations Concepts:
fopen(filename, mode) - Opens file in specified mode
fgetc(file_pointer) - Reads single character from file
fputc(character, file_pointer) - Writes character to file
fclose(file_pointer) - Closes the file and flushes buffers

Algorithm

  1. Input File Names: Read source and target file names from user
  2. Open Source File: Open source file in read mode ("r")
  3. Check Source File: Verify source file opened successfully, exit if error
  4. Open Target File: Open target file in write mode ("w")
  5. Check Target File: Verify target file created successfully, close source and exit if error
  6. Copy Contents: Read each character from source using fgetc()
  7. Write Character: Write each character to target using fputc()
  8. Continue Until EOF: Repeat copying until end of file reached
  9. Close Files: Close both source and target files
  10. Display Success: Print confirmation message to user

Flowchart

START
Input File Names
Open Source File
Check Source Error
Open Target File
Check Target Error
Copy Characters
Close Files
STOP

Code

#include <stdio.h>
#include <stdlib.h>

int main() {
    char source[100], target[100];
    FILE *fs, *ft;
    char ch;

    // Get file names
    printf("Enter source file name: ");
    scanf("%s", source);
    printf("Enter target file name: ");
    scanf("%s", target);

    // Open source file
    fs = fopen(source, "r");
    if (fs == NULL) {
        printf("Error: Cannot open source file %s\n", source);
        exit(1);
    }

    // Open target file
    ft = fopen(target, "w");
    if (ft == NULL) {
        printf("Error: Cannot create target file %s\n", target);
        fclose(fs);
        exit(1);
    }

    // Copy contents
    while ((ch = fgetc(fs)) != EOF) {
        fputc(ch, ft);
    }

    printf("File copied successfully from %s to %s\n", source, target);

    // Close files
    fclose(fs);
    fclose(ft);

    return 0;
}

Output


// Sample Output 1 - Successful File Copy
Enter source file name: input.txt
Enter target file name: output.txt
File copied successfully from input.txt to output.txt

// Sample Output 2 - Source File Not Found
Enter source file name: nonexistent.txt
Enter target file name: backup.txt
Error: Cannot open source file nonexistent.txt

// Sample Output 3 - Copy Text Document
Enter source file name: document.txt
Enter target file name: document_backup.txt
File copied successfully from document.txt to document_backup.txt

// Sample Output 4 - Copy to Different Directory
Enter source file name: data.txt
Enter target file name: backup/data_copy.txt
File copied successfully from data.txt to backup/data_copy.txt

// Sample Output 5 - Permission Error (Target)
Enter source file name: readme.txt
Enter target file name: /protected/copy.txt
Error: Cannot create target file /protected/copy.txt

// Sample Output 6 - Copy Configuration File
Enter source file name: config.ini
Enter target file name: config_backup.ini
File copied successfully from config.ini to config_backup.ini

// Sample Output 7 - Copy Large Text File
Enter source file name: large_document.txt
Enter target file name: large_document_copy.txt
File copied successfully from large_document.txt to large_document_copy.txt

// Sample Output 8 - Copy with Special Characters
Enter source file name: special_chars.txt
Enter target file name: special_chars_backup.txt
File copied successfully from special_chars.txt to special_chars_backup.txt

// Sample Output 9 - Empty File Copy
Enter source file name: empty.txt
Enter target file name: empty_copy.txt
File copied successfully from empty.txt to empty_copy.txt

// Sample Output 10 - Copy Program Source
Enter source file name: program.c
Enter target file name: program_backup.c
File copied successfully from program.c to program_backup.c