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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:42:09+00:00 2026-05-10T20:42:09+00:00

another request sorry.. Right now I am reading the tokens in one by one

  • 0

another request sorry.. Right now I am reading the tokens in one by one and it works, but I want to know when there is a new line..

if my file contains

Hey Bob Now 

should give me

Hey Bob [NEW LINE] NOW 

Is there a way to do this without using getline?

  • 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. 2026-05-10T20:42:09+00:00Added an answer on May 10, 2026 at 8:42 pm

    Yes the operator>> when used with string read ‘white space’ separated words. A ‘White space’ includes space tab and new line characters.

    If you want to read a line at a time use std::getline()
    The line can then be tokenized separately with a string stream.

    std::string   line; while(std::getline(std::cin,line)) {      // If you then want to tokenize the line use a string stream:      std::stringstream lineStream(line);     std::string token;     while(lineStream >> token)     {         std::cout << 'Token(' << token << ')\n';     }      std::cout << 'New Line Detected\n'; } 

    Small addition:

    Without using getline()

    So you really want to be able to detect a newline. This means that newline becomes another type of token. So lets assume that you have words separated by ‘white spaces’ as tokens and newline as its own token.

    Then you can create a Token type.
    Then all you have to do is write the stream operators for a token:

    #include <iostream> #include <fstream>  class Token {     private:         friend std::ostream& operator<<(std::ostream&,Token const&);         friend std::istream& operator>>(std::istream&,Token&);         std::string     value; }; std::istream& operator>>(std::istream& str,Token& data) {     // Check to make sure the stream is OK.     if (!str)     {   return str;     }      char    x;     // Drop leading space     do     {         x = str.get();     }     while(str && isspace(x) && (x != '\n'));      // If the stream is done. exit now.     if (!str)     {         return str;     }      // We have skipped all white space up to the     // start of the first token. We can now modify data.     data.value  ='';      // If the token is a '\n' We are finished.     if (x == '\n')     {   data.value  = '\n';         return str;     }      // Otherwise read the next token in.     str.unget();     str >> data.value;      return str; } std::ostream& operator<<(std::ostream& str,Token const& data) {     return str << data.value; }   int main() {     std::ifstream   f('PLOP');     Token   x;      while(f >> x)     {         std::cout << 'Token(' << x << ')\n';     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 58k
  • Answers 58k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I'm partial to zsh (it's like a blend of ksh… May 11, 2026 at 8:55 am
  • added an answer Read Effective Ways to Introduce Agile into the Workplace and… May 11, 2026 at 8:55 am
  • added an answer You can use many-to-one inside your component to reference Entity2:… May 11, 2026 at 8:55 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.