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 a File
  • 2. Reading Characters
  • 3. Reading Strings
  • 4. Reading Lines
  • 5. Reading Numbers
  • 6. End-of-File (EOF) Handling
  • 7. Closing the File

Was this helpful?

  1. File Input and Output

Reading from a File

Reading from a file in C involves using various functions to access and process the contents of a file. This page provides a detailed overview of how to read data from a file in a C program.

1. Opening a File

  • Begin by opening the file using the fopen function.

    FILE *filePointer;
    filePointer = fopen("example.txt", "r");
  • Ensure the file is successfully opened before proceeding.

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

2. Reading Characters

  • Use the fgetc function to read characters from a file.

    char character;
    character = fgetc(filePointer);

3. Reading Strings

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

    char buffer[100];
    fgets(buffer, sizeof(buffer), filePointer);
  • Alternatively, you can use fscanf to read formatted data.

    fscanf(filePointer, "%s", buffer);

4. Reading Lines

  • For reading entire lines, use fgets or getline functions.

    char line[100];
    fgets(line, sizeof(line), filePointer);
  • getline is useful for dynamic memory allocation.

    char *line = NULL;
    size_t len = 0;
    getline(&line, &len, filePointer);

5. Reading Numbers

  • Use functions like fscanf to read numeric data.

    int number;
    fscanf(filePointer, "%d", &number);

6. End-of-File (EOF) Handling

  • Check for the end of the file using the feof function.

    while (!feof(filePointer)) {
        // Read data
    }
  • Alternatively, use the return value of file reading functions.

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

7. Closing the File

  • Always close the file after reading operations using the fclose function.

    fclose(filePointer);

Reading from a file in C requires careful error handling and consideration of the data format. Whether reading characters, strings, lines, or numeric data, appropriate functions should be used based on the expected content.

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

PreviousAccessing FilesNextProgram: Finding the Total Number of Lines in a Text File

Was this helpful?