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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:37:11+00:00 2026-05-19T12:37:11+00:00

I have a file full of lines in this format: 1 – 2: 3

  • 0

I have a file full of lines in this format:

1 - 2: 3

I want to only load numbers using C++ streams. Whats the most elegant way to do it? I only thought about cin.get() and checikng each char if it is number or not.

  • 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-19T12:37:12+00:00Added an answer on May 19, 2026 at 12:37 pm

    You can use a locale to change what things are read from the file as it is being read. That is, you will filter out all non-numeric values:

    struct numeric_only: std::ctype<char> 
    {
        numeric_only(): std::ctype<char>(get_table()) {}
    
        static std::ctype_base::mask const* get_table()
        {
            static std::vector<std::ctype_base::mask> 
                rc(std::ctype<char>::table_size,std::ctype_base::space);
    
            std::fill(&rc['0'], &rc[':'], std::ctype_base::digit);
            return &rc[0];
        }
    };
    
    std::fstream myFile("foo.txt");
    myfile.imbue(std::locale(std::locale(), new numeric_only()));
    

    Then when you read your file, it’ll convert all non digits to spaces while leaving you only the numbers. After that, you can simply use your normal conversions to transform what is being read into ints.

    std::vector<int> intFromFile;
    std::istream_iterator<int> myFileIter(myFile);
    std::istream_iterator<int> eos;
    std::copy(myFileIter, eos, std::back_inserter(intFromFile));
    

    Response to the comments below:

    Here is what I did to get it to work

    int main(int args, char** argv){
        std::fstream blah;
        blah.open("foo.txt", std::fstream::in);
        if(!blah.is_open()){
            std::cout << "no file";
            return 0;
        }
        blah.imbue(std::locale(std::locale(), new numeric_only()));
    
        std::vector<int> intFromFile;
        std::istream_iterator<int> myFileIter(blah);
        std::istream_iterator<int> eos;
        std::copy(myFileIter, eos, std::back_inserter(intFromFile));
    
       return 0;
    }
    

    And this put only the ints into the vector, nothing more, nothing less. The reason it wasn’t working before was two fold:

    1. I was filling up to ‘9’ but not ‘9’ itself. I’ve changed the fill to ‘:’
    2. Numbers larger than what an int can hold are a problem. I’d suggest using longs.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following file full of lines similar to this: line = 'Weclome
I have a large file that is full of text lines that I want
I have a file full of URLs in a weird format, characters separated by
I have file in json format of world-countries I want to change it to
I have a php file with around 100mb which is full of arrays (only
I have a file full of ascii data. How would I append a string
I have an XML file full of bookmarks from Google Bookmarks. (File: http://gist.github.com/324844 )
I have a text file of full of sentences and I am trying to
I have file with lines like so: 1 17 A G R:560:500:60:10.71%:1.6329E-19 Pass:1.0:276:0:57:0:1E0 15
Suppose that we have file like this: sometext11 sometext12 sometext13 sometext21 sometext22 sometext23 Texts

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.