Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Freopen - max file size

+4 votes
asked Feb 28, 2024 by Cielo Davide (160 points)
Good morning, we need to open text files in C++, but a message appears saying that the file exceeds 100k. Is there any way to overcome this limit?

3 Answers

0 votes
answered Mar 6, 2024 by Peter Minarik (101,340 points)

The documentation of std::freopen doesn't say anything about such limitation.

Can you try to use a different way to open the file?

What is the exact error message?

0 votes
answered Feb 28, 2025 by RAHUL VIJAYAN (190 points)
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream file("largefile.txt"); // Open the file

    if (!file) {
        std::cerr << "Error opening file!" << std::endl;
        return 1;
    }

    std::string line;
    while (std::getline(file, line)) { // Read line by line
        std::cout << line << std::endl; // Print to console
    }

    file.close();
    return 0;
}
0 votes
answered Feb 28, 2025 by RAHUL VIJAYAN (190 points)

The 100K limit is not a restriction in C++ itself but could be due to:

Limitations of the text editor or output console you are using.

Buffering restrictions in your code.

File handling restrictions in certain environments.


Solutions

1. Use std::ifstream Instead of std::fstream (if not already)

If you're opening a file in text mode, ensure you're using std::ifstream correctly:

#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream file("largefile.txt"); // Open the file

    if (!file) {
        std::cerr << "Error opening file!" << std::endl;
        return 1;
    }

    std::string line;
    while (std::getline(file, line)) { // Read line by line
        std::cout << line << std::endl; // Print to console
    }

    file.close();
    return 0;
}

2. Read in Chunks Instead of Loading Everything at Once

If the file is too large to handle all at once, use buffered reading:

#include <iostream>
#include <fstream>

int main() {
    std::ifstream file("largefile.txt", std::ios::binary); // Open in binary mode
    if (!file) {
        std::cerr << "Error opening file!" << std::endl;
        return 1;
    }

    const size_t BUFFER_SIZE = 1024; // Read 1 KB at a time
    char buffer[BUFFER_SIZE];

    while (file.read(buffer, BUFFER_SIZE)) {
        std::cout.write(buffer, file.gcount()); // Print read characters
    }

    std::cout.write(buffer, file.gcount()); // Print the remaining part

    file.close();
    return 0;
}

3. Increase the Console Buffer Size (If Output is Cut Off)

If your console is truncating output, you can:

Use a file for output instead of the console:

std::ofstream output("output.txt");
output << line << std::endl;

Use a pagination tool (e.g., more or less in Linux).

4. Use std::ios::ate for Fast Positioning (If Needed)

To efficiently jump to the end and check file size:

std::ifstream file("largefile.txt", std::ios::ate | std::ios::binary);
std::streamsize size = file.tellg(); // Get file size
file.seekg(0); // Go back to the beginning

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...