C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

Move And Get the Read-Write File Pointer in C++

In C++, file handling provides functions to move the read and write pointers in a file. The read and write pointers determine the position where the next read or write operation will occur. The seekg and seekp functions are used to move the read and write pointers, respectively, while tellg and tellp are used to get the current position of the read and write pointers.

In this tutorial, we'll cover the basics of using seekg, seekp, tellg, and tellp with file streams in C++.

  • Include the required headers: To work with file streams, include the fstream header.
#include <iostream>
#include <fstream>
  • Using seekg and tellg for Input File Streams: The seekg function is used to move the read pointer in an input file stream, while tellg is used to get the current position of the read pointer.

Example:

#include <iostream>
#include <fstream>

int main() {
    std::ifstream infile("input.txt");

    if (!infile.is_open()) {
        std::cerr << "Error opening file for reading." << std::endl;
        return 1;
    }

    // Get the initial position of the read pointer
    std::streampos initialPos = infile.tellg();

    // Move the read pointer 10 bytes ahead
    infile.seekg(10, std::ios::beg);

    // Get the new position of the read pointer
    std::streampos newPos = infile.tellg();

    std::cout << "Initial position: " << initialPos << std::endl;
    std::cout << "New position: " << newPos << std::endl;

    infile.close();

    return 0;
}
  • Using seekp and tellp for Output File Streams: The seekp function is used to move the write pointer in an output file stream, while tellp is used to get the current position of the write pointer.

Example:

#include <iostream>
#include <fstream>

int main() {
    std::ofstream outfile("output.txt", std::ios::app);

    if (!outfile.is_open()) {
        std::cerr << "Error opening file for writing." << std::endl;
        return 1;
    }

    // Get the initial position of the write pointer
    std::streampos initialPos = outfile.tellp();

    // Write some content to the file
    outfile << "This is a C++ tutorial." << std::endl;

    // Get the new position of the write pointer
    std::streampos newPos = outfile.tellp();

    std::cout << "Initial position: " << initialPos << std::endl;
    std::cout << "New position: " << newPos << std::endl;

    outfile.close();

    return 0;
}
  1. How to move file pointer in C++:

    • Description: Introduces the concept of the file pointer in C++ and demonstrates how to move it using various functions.
    • Example Code:
      #include <iostream>
      #include <fstream>
      
      int main() {
          std::ifstream inFile("example.txt");
      
          if (inFile.is_open()) {
              // Move the file pointer to the 5th character from the beginning
              inFile.seekg(4);
      
              // Read and print the content from the current position
              char ch;
              while (inFile.get(ch)) {
                  std::cout << ch;
              }
      
              inFile.close();
          } else {
              std::cerr << "Failed to open the file." << std::endl;
          }
      
          return 0;
      }
      
  2. C++ fseek and ftell for file pointer:

    • Description: Demonstrates the usage of fseek and ftell functions in C++ for moving and determining the file pointer position.
    • Example Code:
      #include <cstdio>
      
      int main() {
          FILE* file = std::fopen("example.txt", "r");
      
          if (file) {
              // Move the file pointer to the 10th byte from the beginning
              std::fseek(file, 9, SEEK_SET);
      
              // Get the current file pointer position
              long position = std::ftell(file);
      
              std::fclose(file);
      
              // Print the current position
              std::printf("Current file position: %ld\n", position);
          } else {
              std::cerr << "Failed to open the file." << std::endl;
          }
      
          return 0;
      }
      
  3. Moving file pointer to the end in C++:

    • Description: Shows how to move the file pointer to the end of the file, enabling appending or reading content from the end.
    • Example Code:
      #include <iostream>
      #include <fstream>
      
      int main() {
          std::fstream file("example.txt", std::ios::in | std::ios::out | std::ios::ate);
      
          if (file.is_open()) {
              // Move the file pointer to the end
              file.seekg(0, std::ios::end);
      
              // Read and print content from the end
              char ch;
              while (file.get(ch)) {
                  std::cout << ch;
              }
      
              file.close();
          } else {
              std::cerr << "Failed to open the file." << std::endl;
          }
      
          return 0;
      }