Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:55:06+00:00 2026-06-05T16:55:06+00:00

std::ifstream sr(path.c_str()); if (!sr) throw runtime_error(Could not open file ‘+path+\’); sr.seekg(0, ios::end); streampos lastPos

  • 0
std::ifstream sr(path.c_str());
if (!sr)
    throw runtime_error("Could not open file '"+path+"\'");

sr.seekg(0, ios::end);
streampos lastPos = sr.tellg(); //returns 3161125
unsigned dataSize = (int)lastPos - 100; //dataSize becomes 3161025

char* data = (char*)malloc(dataSize);
if (!data)
    throw runtime_error("Out of memory whean allocating read buffer");
sr.clear();
sr.seekg(0, ios::beg);
sr.read(data, dataSize);  //sr.read(data, 3110000) works!
if(sr.fail()) //fails

This code fails, but if I read 3110000 bytes, the read() succeeds and fail() is false. I can load the file via stream iterators like this:

string data(std::istreambuf_iterator<char>(sr),
            std::istreambuf_iterator<char>());

Any idea why read() fails?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:55:09+00:00Added an answer on June 5, 2026 at 4:55 pm

    The value returned by seekg() + tellg() is reliable only if the file is opened in binary mode. In text mode line endings are translated, with a different result depending on the way your operating system stores files.

    Specifically, on Windows the CR+LF pair is translated to a single character '\n'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does the following not work: #include <iostream> #include <fstream> #include <stack> std::stack<std::ifstream> s;
I have a program that load data from a file using std::ifstream and store
i have the next code: std::string line; std::ifstream myfile (text.txt); if (myfile.is_open()) { while
I'm converting a project from vs2008 to vs2010 and getting linker errors for std:ifstream/ofstream
I am trying to do the following: template <class T> std::ifstream& operator>> (std::ifstream& fin,
#include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads
I'm trying to update a random-access binary file using the std::iostream interface with separate
//using namespace std; using std::ifstream; using std::ofstream; using std::cout; class Dog { friend ostream&
i am having a problem in getting the File Time Creation.. could anyone know
ifstream::tellg() is returning -13 for a certain file. Basically, I wrote a utility that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.