Skip to study material
← Question index

QUESTION 02

Explain files, FILE pointers, opening modes and closing.

5 marks

Answer in brief

A file stores data outside the lifetime of one program execution. C accesses file streams through functions declared in stdio.h.

Basic sequence

  1. Declare FILE *fp.
  2. Call fopen with a path and a suitable mode.
  3. Check for NULL before any stream operation.
  4. Read or write while checking operation results.
  5. Call fclose and check important output errors.

Modes

r opens an existing file for reading. w creates or truncates a file for writing. a appends writes at the end. Adding + permits reading and writing. Adding b selects binary mode where the implementation distinguishes it. Opening a missing file with r fails; opening an existing file with w discards its old contents.

Example

The demonstration opens a dedicated sample file with w, writes one line, closes it and reopens it with r. It does not treat fopen success as proof that every later write succeeds. File data can outlast the stream and the program that created it.

Precautions

Use only a stream obtained successfully and not already closed. fclose releases stream resources; it does not delete the file. A path is interpreted relative to the process working directory unless given otherwise. The examples intentionally create or replace clearly named demonstration files, so use a separate test directory.

Cost

The C standard does not prescribe one universal filesystem-operation complexity. Processing n bytes generally involves work proportional to that data plus implementation and device costs.

Reference table

↔ Scroll horizontally on a small screen
Common opening modes
ModePurposeExisting contents
rRead existingPreserved
wWrite/createTruncated
aAppend/createPreserved; writes at end
r+Read/update existingPreserved
w+Read/write/createTruncated
a+Read/append/createPreserved; writes at end
b suffixBinary modeCombine with the appropriate base mode

Diagram or flowchart

CodeBhavya illustration of file flow
Explain files, FILE pointers, opening modes and closing. · Download diagram

Complete C example

This demonstration creates or replaces its codebhavya_* sample file in the working directory. Run it in a separate test directory. Native binary examples use one compatible implementation.

main.c

Download main.c
/* CodeBhavya | Data Structures AR25 | Learning example */
#include <stdio.h>

int main(void)
{
    const char *path = "codebhavya_text_demo.txt";
    FILE *fp = fopen(path, "w");
    if (fp == NULL) { puts("Open failed"); return 1; }
    if (fputs("First line\nSecond line\n", fp) == EOF) { fclose(fp); puts("Write failed"); return 1; }
    if (fclose(fp) != 0) { puts("Close failed"); return 1; }
    fp = fopen(path, "r");
    if (fp == NULL) { puts("Open failed"); return 1; }
    char line[80];
    while (fgets(line, sizeof line, fp) != NULL) { fputs(line, stdout); }
    int failed = ferror(fp);
    if (fclose(fp) != 0 || failed) { puts("Read/close failed"); return 1; }
    return 0;
}

Sample input and output

The example uses values in the code.

Output

First line
Second line