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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:51:23+00:00 2026-05-19T13:51:23+00:00

For my homework project, I had to read in a book and then parse

  • 0

For my homework project, I had to read in a book and then parse it word for word, listing occurrences of each word and the respective line it was found at. I’m done with the project now and looking to optimize my code by reading other people’s use of the STL (we are supposed to rely heavily on the STL for this assignment).

This is not regarding the homework itself, but two attempts I made to read in each word that resulted in the stream eating my newlines. Without them, I couldn’t keep track of which line I’m on in the document I’m reading, therefore eliminating the use of such solutions.

Attempt One:

string word;
stringstream ss;
ss << document; // Document was read from file and stored in a "string document"
while(ss)
{
    ss >> word;
    // Work with the word.
    // Oops! No newlines anymore! :(
}

Attempt Two:

ifstream ifs(filename.c_str());
if(ifs.is_open)
{
    typedef istream_iterator<string> string_input;
    vector<string> words;
    copy(string_input(ifs), strin_input(), back_inserter(words));

    // Oops! The newlines are gone here too! :(
}

My current solution isn’t as pretty as I want it to be, and I want some more STL magic in it (just for the sake of learning some neat STL tricks and getting even more comfortable with it)

Current solution:

Read the file:

std::ostringstream os;
ss << ifstream(filename.c_str()).rdbuf();
return ss.str();

And split by line and word:

size_t oldNewLine = 0;
size_t newLine = document_.find('\n', oldNewLine);
while(newLine != string::npos)
{
    string documentLine = document_.substr(oldNewLine, (newLine - oldNewLine));
    vector<string> words = Utility::split(documentLine);

   // Work with each individual word
   // Yay! I can keep track of the line number now! :)

   oldNewLine = newLine + 1; // Curse you, off by one error!
   newLine = document_.find('\n', oldNewLine);
}

The reading of the file is short, succinct and very readable imo, but the splitting of each line and word is tedious. I wish I could just immediately read each word from file, preserving the newline. There has to be a short, sweet way to do this!

So, how to do it?

  • 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-19T13:51:24+00:00Added an answer on May 19, 2026 at 1:51 pm

    You can parse the words line by line:

    std::string line;
    vector<string> words;
    
    while(std::getline(ifs, line))
    {
        std::stringstream linestream(line);
    
        copy(std::istream_iterator<std::string>(linestream),
             std::istream_iterator<std::string>(),
             std::back_inserter(words));
    
        ++lineCount;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For homework, I was given the following 8 code fragments to analyze and give
(This is no homework and no work issue. It's just my personal interest/occupation and
For a homework assignment I was given a Card class that has enumerated types
I'm working through some homework and a question on a previous exam paper asks
I'm doing a basic homework assignment which looks like this: While input <> -1
Disclaimer: This is for a homework assignment, but the question is not regarding the
So I've decided to try to solve my physics homework by writing some python
I've been working through a recent Computer Science homework involving recursion and big-O notation.
This one is a case of not doing your homework.:-) Apart from dynamic loading
I'm writing some excel-like C++ console app for homework. My app should be able

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.