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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:52:34+00:00 2026-05-25T16:52:34+00:00

I have a function (legacy) that reads the first few lines of a file

  • 0

I have a function (legacy) that reads the first few lines of a file to determine its type, then closes and reopens the file so it can re-read the entire file using the correct interpreter. The gist is:

void readFile(const char *filename) {
  ifstream is(filename);
  Filetype ft = determineFileType(is);
  is.close();
  is.open(filename);
  parseFile(is, ft);
}

I needed a similar function that can work on an already-opened stream. I created a new function that takes an ostream & instead of a char *filename — basically this:

void readFile(istream &is) {
  std::ios::streampos pos = is.tellg();
  Filetype ft = determineFileType(is);
  is.seekg(pos);
  parseFile(is, ft);
}

It seems to work when the istream is actually a stringstream or an fstream but I wonder if I’m just getting lucky. I also did a small test on seekg-ing std::cin and it worked, which surprised me.

So my question: what kinds of streams are you allowed to use seekg on? When will it fail? Spec references would be great — I looked through and the stuff on seekg, pubseekpos, seekpos, seekoff weren’t helpful at all.

I’d like to reimplement the original function in terms of the new (as below), but I just don’t know if that’s safe.

void readFile(const char *filename) {
  ifstream is(filename);
  readFile(is);
  is.close();
}
  • 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-25T16:52:35+00:00Added an answer on May 25, 2026 at 4:52 pm

    The only real answer one can give is that it works where it works. In
    the case of std::stringbuf, it should work everywhere. In the case of
    std::filebuf, whether it works or not depends on the system; it will
    generally work if the filebuf is opened on an actual file, but will
    usually fail (perhaps silently, if the system doesn’t report an error)
    for many other types of input: from a keyboard, or a named pipe, for
    example.

    A more robust solution would be to cache the initial input, and re-read it from the cache.

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

Sidebar

Related Questions

I have the following function from some legacy code that I am maintaining. long
So I have a light box that's dynamically loaded from a common.js file. (Legacy
I have a legacy function that looks like this: int Random() const { return
I have the following legacy VB6 function that I want to call from C#.
I have a lot of legacy code that uses a function pointer as an
I have function getCartItems in cart.js and I want to call that function in
I have function Start() that is fired on ready. When I click on .ExampleClick
I have function some_func_1 which will create an object of type some_type and will
Inside my Controller i have function that runs after user clicks on item, which
I have some legacy code that provides a list of the available COM ports

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.