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