C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • 1. Opening Files
  • 2. Reading from Files
  • 3. Writing to Files
  • 4. Closing Files
  • 5. Binary Files
  • 6. Handling File Errors
  • 7. Seeking in Files

Was this helpful?

  1. File Input and Output

Accessing Files

Accessing files in C involves a series of operations to open, read, write, and close files. This page provides a detailed overview of how to perform these actions.

1. Opening Files

  • To open a file, use the fopen function. It requires the file path and the mode in which the file is opened (read, write, etc.).

    FILE *filePointer;
    filePointer = fopen("example.txt", "r");
  • Common file modes include:

    • "r": Read

    • "w": Write (creates or truncates file)

    • "a": Append (creates or appends to file)

    • "r+": Read and Write

    • "w+": Read and Write (creates or truncates file)

2. Reading from Files

  • Use functions like fscanf or fgets to read data from a file.

    char buffer[100];
    fscanf(filePointer, "%s", buffer);
  • Ensure proper error checking to handle situations where the file does not exist or cannot be opened.

    if (filePointer == NULL) {
        // Handle error
    }

3. Writing to Files

  • Use functions like fprintf or fputs to write data to a file.

    fprintf(filePointer, "Hello, World!\n");
  • Check for successful file opening before performing write operations.

4. Closing Files

  • Always close a file after operations using the fclose function. This ensures that resources are released.

    fclose(filePointer);

5. Binary Files

  • File I/O can be performed in binary mode ("rb", "wb", etc.) for non-text files.

    filePointer = fopen("binaryfile.bin", "rb");

6. Handling File Errors

  • Check the return value of file functions to handle errors effectively.

    if (fscanf(filePointer, "%s", buffer) == EOF) {
        // Handle end-of-file or read error
    }

7. Seeking in Files

  • Use functions like fseek to move the file pointer to a specific position in the file.

    fseek(filePointer, 0, SEEK_SET); // Move to the beginning of the file

Accessing files in C is a crucial skill for handling data persistently. Whether reading or writing, proper error handling and resource management ensure robust file operations.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousFile Input and OutputNextReading from a File

Was this helpful?