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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:59:09+00:00 2026-05-26T08:59:09+00:00

I have the following C++ program and ran it using Visual Studio 2008 on

  • 0

I have the following C++ program and ran it using Visual Studio 2008 on Windows 7. I get and then unget a character. After doing so, the file position is different. Why? How do I get around this problem?


test.txt (download link below if you want)

/* Comment 1 */

/* Comment 2 */

#include <fstream>

int main (int argc, char ** argv) {
    char const * file = "test.txt";
    std::fstream fs(file, std::ios::in);
    std::streampos const before = fs.tellg();

    // replacing the following two lines with
    // char c = fs.peek(); results in the same problem
    char const c = fs.get();
    fs.unget();

    std::streampos const after = fs.tellg();
    fs.seekg(after);
    char const c2 = fs.get();
    fs.close();
    return 0;
}

  • c: 47 '/' char
  • c2: -1 'ÿ' char
  • before: {_Myoff=0 _Fpos=0 _Mystate=0 } std::fpos<int>
  • after: {_Myoff=0 _Fpos=-3 _Mystate=0 } std::fpos<int>

Adding | std::fstream::binary to the constructor seems to solve the problem. Perhaps it has to do with newlines in the file? If so, why does it affect code that doesn’t even get close to reading a newline?

Updated with a seeking to the after position and getting another character.

It seems that saving via Notepad vs. Vim makes a difference. Saving via Notepad makes the stream work okay.

I have uploaded the file to google docs if you want to dl it:

https://docs.google.com/leaf?id=0B8Ufd7Rk6dvHZmYyZjgwYmItMTI3MC00MDljLWJjYTctMWMxYWM0ODk1MTE2&hl=en_US

  • 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-26T08:59:10+00:00Added an answer on May 26, 2026 at 8:59 am

    Ok using your input file I see the same behavior you do. After some experimentation, it looks like the file was in Unix format, then had the ^M characters edited out (at least that’s how I was able to reproduce it).

    To fix it, I edited the file in Vim, executed “:set ff=dos”, then added and deleted a character to dirty the file, then saved it.

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

Sidebar

Related Questions

I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
I have a program that returns a comma-separated string of numbers after doing some
I have an app that I wrote using C# .NET 4.0 in Visual Studio
I have the following program: ~/test> cat test.cc int main() { int i =
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
let us have a situation in which the following program prints some 10 lines
I have the following layout for my test suite: TestSuite1.cmd: Run my program Check
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have the following code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
I have the following program. However, I can't understand why I have to pass

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.