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

  • SEARCH
  • Home
  • 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 732039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:05:46+00:00 2026-05-14T07:05:46+00:00

Why does this fail, it’s supposed to be simple and work ? fisier.seekg(0, ios::end);

  • 0

Why does this fail, it’s supposed to be simple and work ?

fisier.seekg(0, ios::end);
long lungime = fisier.tellg();

This returns a larger value than that of the file resulting in a wrong

char *continut = new char[lungime];

Any idea what the problem could be ?

I also tried counting to the end of the file one char at a time, that rendered the same result, a higher number than expected. But upon using getline() to read one line at a time, it works, there are no extra spaces…

  • 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-05-14T07:05:47+00:00Added an answer on May 14, 2026 at 7:05 am

    At a guess, you’re opening the file in translated mode, probably under Windows. When you simply seek to the end of the file, the current position doesn’t take the line-end translations into account. The end of a line (in the external file) is marked with the pair “\r\n” — but when you read it in, that’s converted to just a “\n”. When you use getline to read one line at a time, the \ns all get discarded as well, so even on a system (e.g. Unix/Linux) that does no translation from external to internal representation, you can still expect those to give different sizes.

    Then again, you should really forget that new [] exists at all. If you want to read an entire file into a string, try something like this:

    std::stringstream continut;
    continut << fisier.rdbuf();
    

    continut.str() is then an std::string containing the data from the file.

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

Sidebar

Related Questions

Why does this code fail to display the category name Apples using the current
Why does this fail L is [1,2,3,4] , and this works: L = [1,2,3]
Why does this fail: DECLARE @DATE VARCHAR(50) = 'dasf' SELECT CASE WHEN ISDATE(@DATE) =
Why does this work: exec 3<>/dev/tcp/www.google.com/80 echo -e GET / HTTP/1.1\n\n>&3 cat <&3 And
Why does this fail to compile? (g++-4.5) template < typename U > static void
Why does this code fail? #include <algorithm> int main() { int a[10]; enum {
I can't understand why does this code fail with segfault: #include <cstdlib> #include <iostream>
Why does this fail to compile: int myVar = 0; myVar ? []()->void{} :
Why does this piece of code fail in Scilab? N=1000; U=rand(N, 1); X=(9*U -
Does this pattern: setTimeout(function(){ // do stuff }, 0); Actually return control to the

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.