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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:16:35+00:00 2026-05-26T03:16:35+00:00

How would you read a string if it was no longer than 20 characters

  • 0

How would you read a string if it was no longer than 20 characters and could contain spaces in C++? For example I have this kind of file:

3
Namama               4 5 12
Cool                 4 2 34
Not So Cool          4 2 45

I want to write it in the most basic way possible. And I would prefer using only C++ (no C thingies) in this case. This means I want to store it in std::string.

I could use istream::get or istream::getline but then I have to avoid the new line characters. This is problematic (and the code I’m writing is going to be shown to beginners).

What are your solutions for this problem?

Edit:

As asked I’m going to tell you what I have tried but I don’t think it’s going to be any good. I am not a C++ beginner and usually I would have used something like istream::get and char array but removing new line characters might seem too techy for some people.

So… Either reading char array or std::string using istream::operator>> fails because it stops reading when they see space character (and I may have to read several words). This means the following code fails:

char name[21];
std::cin >> name;

or…

std::string name;
std::cin >> name;

Another thing is that new line characters differ from system to system and actually Windows use two of them by default and I have tried using istream::ignore with 2 as an argument on Windows and that was the only way to ignore the new line and I came to conclusion that it’s because Windows use two characters for a new line mark. That means it wouldn’t work on Linux and it would have to be more complex… Again – bad for beginners.

  • 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-26T03:16:35+00:00Added an answer on May 26, 2026 at 3:16 am

    If you want to read exactly 20 characters,

    for(int i=0; i<3; ++i) {
        std::string result;
        int one, two, three;
    
        result.resize(20);
        if (!std::cin.read(&result[0], 20)) throw std::runtime_error("reading buffer failed!");
    
        if (!std::cin >> one >> two >> three) throw std::runtime_error("reading numbers failed!");
        if (!std::cin.ignore(1000, '\n')) throw std::runtime_error("ignore failed!");
    }
    

    If you don’t want exactly 20 characters, how do you know when you’ve reached the end of the string?

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

Sidebar

Related Questions

I have a longer text saved in a String. I would like to print
I have read string ńîôč˙ from file by using code page windows-1251, instead of
In java i would read the whole file in to byte array and do
I would like to read a text file and input its contents into an
I would like to read a DICOM file in C#. I don't want to
I would like to read the last 1 megabyte of a MP3 file and
I've read many different articles/threads on this, but have yet to determine the best
lets say I have a String like this [{ name : Ronald , firstname
I have some code which works after replacing read-string with load-string. It is good
Thanks to this excellent tutorial , I know how to read a string (in

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.