2075 3

CSC115 ยท 2075 ยท 10 marks

Why do we need data files?What are the different file opening modes?Write a program that reads data from a file "input.txt" and writes to "output.txt" file.[2+3+5]

Why do we need data files?What are the different file opening modes?Write a program that reads data from a file "input.txt" and writes to "output.txt" file.[2+3+5]

C ProgrammingChapter 10: File Handling in CConcept of File

Quick answer

When a program runs, all data is stored in main memory (RAM), which is volatile -- it is lost when the program ends or the computer is switched off. Data files solve this problem by providing permanent storage on secondary storage devices (hard disk, etc.).

Data Files, File Opening Modes, and File Copy Program


(a) Why Do We Need Data Files?

Reasons we need data files:

  • Persistence: Data stored in files remains even after the program terminates.
  • Large data handling: Files allow processing of data too large to fit in memory at once.
  • Data sharing: Multiple programs can read from and write to the same file.
  • Reusability: Data entered once can be reused many times without re-entering.
  • Record keeping: Files allow maintaining logs, reports, and databases permanently.

(b) Different File Opening Modes

As per the notes, the file opening modes used in C are:

ModeDescription
"r"Open existing file for reading only. File must exist. Previous data is not erased.
"w"Open file for writing. Creates new file if it does not exist. Erases existing data if file exists.
"a"Open file for appending. Creates new file if it does not exist. New data is added at the end; existing data is not erased.
"r+"Open existing file for reading and writing (update mode). File must exist. Data is not erased.
"w+"Open file for writing and reading. Creates new file if not exists. Erases existing data if file exists.
"a+"Open file for appending and reading. Creates new file if not exists. Cannot modify existing data.
"rb"Open binary file for reading.
"wb"Open binary file for writing.
"ab"Open binary file for appending.
"rb+"Open binary file for reading and writing.
"wb+"Create binary file for reading and writing.
"ab+"Open binary file for appending and reading.

(c) Program to Read from "input.txt" and Write to "output.txt"

Approach:

  • Open input.txt in "r" (read) mode.
  • Open output.txt in "w" (write) mode.
  • Read characters one by one from input.txt using fgetc().
  • Write each character to output.txt using fputc().
  • Close both files after the operation.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp_in, *fp_out;
    char ch;

    /* Open input.txt for reading */
    fp_in = fopen("input.txt", "r");
    if (fp_in == NULL)
    {
        printf("Error: Cannot open input.txt\n");
        exit(1);
    }

    /* Open output.txt for writing */
    fp_out = fopen("output.txt", "w");
    if (fp_out == NULL)
    {
        printf("Error: Cannot open output.txt\n");
        fclose(fp_in);
        exit(1);
    }

    /* Read from input.txt and write to output.txt character by character */
    while ((ch = fgetc(fp_in)) != EOF)
    {
        fputc(ch, fp_out);
    }

    printf("Data successfully copied from input.txt to output.txt\n");

    /* Close both files */
    fclose(fp_in);
    fclose(fp_out);

    return 0;
}

Explanation of Key Steps:

StepCodePurpose
Open input filefopen("input.txt", "r")Opens file for reading
Open output filefopen("output.txt", "w")Opens/creates file for writing
NULL checkif (fp_in == NULL)Checks if file opened successfully
Read characterfgetc(fp_in)Reads one character from input file and advances file pointer
Write characterfputc(ch, fp_out)Writes one character to output file
EOF check!= EOFStops reading when end of file is reached
Close filesfclose()Releases file resources

Sample Output:

Data successfully copied from input.txt to output.txt

The contents of input.txt will be completely copied into output.txt.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.