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 7721421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:55:14+00:00 2026-06-01T03:55:14+00:00

I’m trying to read a file one character at a time, but for some

  • 0

I’m trying to read a file one character at a time, but for some reason the get pointers’ position isn’t being reported correctly:

std::wifstream stream(L"tokenizer.txt");

int pos = static_cast<int> (stream.tellg());

stream.get();
stream.get();

pos = static_cast<int> (stream.tellg());

stream.close();

The value of pos before the two calls to stream.get is 0, as it should be, but only 1 after them. The strange thing is that any calls to get after the first all affect the result of stream.tellg. In other words, the first call to get will not cause a change in result from tellg.

If I call unget after the first call to get, it still reports the position as being 0, but it treats it as being at position 1, as the next 2 characters are ‘/’ and ‘c’, while they should be ‘/’ ‘/’.

The contents of the file are as follows:

//comment
123.45 100 "delimited string" str ing

(The space between str and ing is intentional.)

The file itself is saved in ANSI using Notepad++.
I’m using Visual Studio 2010 Ultimate SP1.

  • 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-01T03:55:15+00:00Added an answer on June 1, 2026 at 3:55 am

    When compiled with G++ 4.7.0 on Mac OS X 10.7.3 (that’s a ‘home built’ compiler), then the following code, closely derived from your outline, produces the output:

    pos = 0
    c = 47
    c = 47
    pos = 2
    

    which is what you’d expect.

    Compilation command line:

    g++ -O3 -g   -Wall -Wextra  xx.cpp -o xx  
    

    Actual code:

    #include <iostream>
    #include <fstream>
    
    int main(void)
    {
        std::wifstream stream("tokenizer.txt");
    
        int pos = static_cast<int> (stream.tellg());
        std::cout << "pos = " << pos << std::endl;
    
        int c = stream.get();
        std::cout << "c = " << c << std::endl;
    
        c = stream.get();
        std::cout << "c = " << c << std::endl;
    
        pos = static_cast<int> (stream.tellg());
        std::cout << "pos = " << pos << std::endl;
    
        stream.close();
    }
    

    Apart from adding diagnostic printing, headers and the main() function, the only significant difference is that there was no constructor for std::wifstream that accepts an array of wchar_t, so I dropped the L.

    This suggests that there is something odd about either the library you’re linking with or the compiler you’re using. Could it be optimizing out one of the two calls to get()? Are you sure there isn’t a //...comment ending in backslash\ on the line before one of the two calls to get()?

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of 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.