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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:37:06+00:00 2026-06-14T19:37:06+00:00

I have an input stream with the following lines: # <int> <int> <some_data_type> <some_data_type>

  • 0

I have an input stream with the following lines:

# <int> <int>
<some_data_type> <some_data_type> <some_data_type> ..... <some_data_type>
<some_data_type_1> <some_data_type_2> <some_data_type_3> <some_data_type_1> <some_data_type_2> <some_data_type_3> .... <some_data_type_1> <some_data_type_2> <some_data_type_3> 

In the above stream all three lines are different and have to be parsed differently. Currently,I am using a reading method as follows:

void reader( std::istream & is, DataStructure & d ){
  std::string line;
  getline(is,line);
  std::stringstream s(line);
  //parse line 1

  getline(is,line);
  std::stringstream line2(line);
  //parse line 2

  getline(is,line);
  std::stringstream line3(line);
  //parse line 3

 }

Now the idea is not to make use of std::stringstream at all, as a line can arbitarily large and we donot want to load everything into memory twice. So, it would be better if it was possible to read from the input stream directly into the user given datastructure d.

An idea is to make use of std::istream_iterator but unfortunately the different lines have different parsing needs. For example, in the last line, three elements from the stream together constitute a single data element.

The only idea that seems plausible to me at this moment is to handle the stream buffer directly. It would be great if anyone could recommend a better way of doing this.

NOTE: Cannot make use of a tertiary data structure like std::stringstream. It is essential to read from the stream directly into the user provided data structure.

EDIT: Please note we are only allowed a single pass over the file.

  • 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-14T19:37:07+00:00Added an answer on June 14, 2026 at 7:37 pm

    Now the idea is not to make use of std::stringstream at all, as a line
    can arbitarily large and we donot want to load everything into memory
    twice. So, it would be better if it was possible to read from the
    input stream directly into the user given datastructure d.

    Olaf explained the extraction operator above but then we have a new requirement:

    This will only work for the first line, where it is known there is a
    fixed number of elements.

    and

    (2) Unfortunately, I have no discriminator beyond my knowledge that each instance of the data
    structure needs to be instantiated with information stored in three
    different lines. All three lines have different lengths and different
    data elements. Also, I cannot change the format.

    plus

    (3) All information is treated as unsigned integer.

    Now the next issue is that we don’t know what the data structure actually is, so given what has come before it appears to be dynamic in some fashion. Because we can treat the data as unsigned int then we can use the extraction operator possibly, but read into a dynamic member:

    vector<unsigned int> myUInts;
    ...
    inFile >> currentUInt;
    myUInts.push_back(currentUInt);
    

    But then the issue of where to stop comes into play. Is it at the end of the first line, the third? If you need to read an arbitrary number of unsigned ints, whilst still checking for a new line then you will need to process white space as well:

    inFile.unsetf(ios_base::skipws);
    

    How you actually handle that is beyond what I can say at the moment without some clearer requirements. But I would guess it will be in the form:

     inFile >> myMember;
     char next = infile.peek()
     //skip whitespace and check for new line
     //Repeat until data structure filled, and repeat for each data structure.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an input stream, and I want to map to output lines. For
so i have the following code opening an input stream and collecting the information
I have been testing the performance of reading server socket input stream. I found
So a FILE stream can have both input and output buffers. You can adjust
I have the following data as input (sorted by first column): foo 1 2
I have a method search that I want to feed an input stream and
I have the following piece of code in a try/catch block InputStream inputstream =
I have input text: <div><label wicket:for=name>Name</label><input type=text wicket:id=name /></div> Now I need to add
I have input.txt and I want to search for a string and extract a
i have input xml as <content> <date> <day>14</day> <month>06</month> <year>2012</year> </date> </content> want it

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.