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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:58:48+00:00 2026-05-23T17:58:48+00:00

I’m trying to read in lines from a std::istream but the input may contain

  • 0

I’m trying to read in lines from a std::istream but the input may contain '\r' and/or '\n', so std::getline is no use.

Sorry to shout but this seems to need emphasis…

The input may contain either newline type or both.

Is there a standard way to do this? At the moment I’m trying

char c;
while (in >> c && '\n' != c && '\r' != c)
    out .push_back (c);

…but this skips over whitespace. D’oh! std::noskipws — more fiddling required and now it’s misehaving.

Surely there must be a better way?!?

  • 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-23T17:58:49+00:00Added an answer on May 23, 2026 at 5:58 pm

    OK, here’s one way to do it. Basically I’ve made an implementation of std::getline which accepts a predicate instead of a character. This gets you 2/3’s of the way there:

    template <class Ch, class Tr, class A, class Pred>
    std::basic_istream<Ch, Tr> &getline(std::basic_istream<Ch, Tr> &is, std::basic_string<Ch, Tr, A>& str, Pred p) {
    
        typename std::string::size_type nread = 0;      
        if(typename std::istream::sentry(is, true)) {
            std::streambuf *sbuf = is.rdbuf();
            str.clear();
    
            while (nread < str.max_size()) {
                int c1 = sbuf->sbumpc();
                if (Tr::eq_int_type(c1, Tr::eof())) {
                    is.setstate(std::istream::eofbit);
                    break;
                } else {
                    ++nread;
                    const Ch ch = Tr::to_char_type(c1);
                    if (!p(ch)) {
                        str.push_back(ch);
                    } else {
                        break;
                    }
                }
            }
        }
    
        if (nread == 0 || nread >= str.max_size()) {
            is.setstate(std::istream::failbit);
        }
    
        return is;
    }
    

    with a functor similar to this:

    struct is_newline {
        bool operator()(char ch) const {
            return ch == '\n' || ch == '\r';
        }
    };
    

    Now, the only thing left is to determine if you ended on a '\r' or not…, if you did, then if the next character is a '\n', just consume it and ignore it.

    EDIT: So to put this all into a functional solution, here’s an example:

    #include <string>
    #include <sstream>
    #include <iostream>
    
    namespace util {
    
        struct is_newline { 
            bool operator()(char ch) {
                ch_ = ch;
                return ch_ == '\n' || ch_ == '\r';
            }
    
            char ch_;
        };
    
        template <class Ch, class Tr, class A, class Pred>
            std::basic_istream<Ch, Tr> &getline(std::basic_istream<Ch, Tr> &is, std::basic_string<Ch, Tr, A>& str, Pred &p) {
    
            typename std::string::size_type nread = 0;
    
            if(typename std::istream::sentry(is, true)) {
                std::streambuf *const sbuf = is.rdbuf();
                    str.clear();
    
                while (nread < str.max_size()) {
                    int c1 = sbuf->sbumpc();
                    if (Tr::eq_int_type(c1, Tr::eof())) {
                        is.setstate(std::istream::eofbit);
                        break;
                    } else {
                        ++nread;
                        const Ch ch = Tr::to_char_type(c1);
                        if (!p(ch)) {
                            str.push_back(ch);
                        } else {
                            break;
                        }
                    }
                }
            }
    
            if (nread == 0 || nread >= str.max_size()) {
                is.setstate(std::istream::failbit);
            }
    
            return is;
        }
    }
    
    int main() {
    
        std::stringstream ss("this\ris a\ntest\r\nyay");
        std::string       item;
        util::is_newline  is_newline;
    
        while(util::getline(ss, item, is_newline)) {
            if(is_newline.ch_ == '\r' && ss.peek() == '\n') {
                ss.ignore(1);
            }
    
            std::cout << '[' << item << ']' << std::endl;
        }
    }
    

    I’ve made a couple minor changes to my original example. The Pred p parameter is now a reference so that the predicate can store some data (specifically the last char tested). And likewise I made the predicate operator() non-const so it can store that character.

    The in main, I have a string in a std::stringstream which has all 3 versions of line breaks. I use my util::getline, and if the predicate object says that the last char was a '\r', then I peek() ahead and ignore 1 character if it happens to be '\n'.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

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.