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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:47:29+00:00 2026-05-22T20:47:29+00:00

I have a file and I need to loop through it assigning an int

  • 0

I have a file and I need to loop through it assigning an int foo, string type, 64/128 bit long. How would I use a stream to parse these lines into the following variables – I want to stick with the stream syntax ( ifs >> foo >> type ) but in this case type would end up being the rest of the line after the 0/52 … and at that point I’d just get a char* and use strtoull and such so why use the stream in the first place… I’m hoping for readable code without horrid performance over char strings / strtok / strtoull

//input file:
0ULL'04001C0180000000000000000EE317BC'
52L'04001C0180000000'
//ouput:
//0 ULL 0x04001C0180000000 0x000000000EE317BC
//52 L 0x04001C0180000000

  ifstream ifs("input.data");
  int foo;
  string type;
  unsigned long long ull[2];
  • 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-22T20:47:30+00:00Added an answer on May 22, 2026 at 8:47 pm

    This is a rather trivial task for a more sophisticated parser such as boost.spirit.

    To solve this using just the standard C++ streams you would need to

    • a) treat ' as whitespace and
    • b) take an extra pass over the string “04001C0180000000000000000EE317BC” which has no separators between the values.

    Borrowing Jerry Coffin’s sample facet code,

    #include <iostream>
    #include <fstream>
    #include <locale>
    #include <vector>
    #include <sstream>
    #include <iomanip>
    struct tick_is_space : std::ctype<char> {
        tick_is_space() : std::ctype<char>(get_table()) {}
        static std::ctype_base::mask const* get_table()
        {
            static std::vector<std::ctype_base::mask>
                   rc(table_size, std::ctype_base::mask());
            rc['\n'] = std::ctype_base::space;
            rc['\''] = std::ctype_base::space;
            return &rc[0];
        }
    };
    
    int main()
    {
        std::ifstream ifs("input.data");
        ifs.imbue(std::locale(std::locale(), new tick_is_space()));
        int foo;
        std::string type, ullstr;
        while( ifs >> foo >> type >> ullstr)
        {
            std::vector<unsigned long long> ull;
            while(ullstr.size() >= 16) // sizeof(unsigned long long)*2
            {
                std::istringstream is(ullstr.substr(0, 16));
                unsigned long long tmp;
                is >> std::hex >> tmp;
                ull.push_back(tmp);
                ullstr.erase(0, 16);
            }
            std::cout << std::dec << foo << " " << type << " "
                      << std::hex << std::showbase;
            for(size_t p=0; p<ull.size(); ++p)
                std::cout << std::setw(16) << std::setfill('0') << ull[p] << ' ';
            std::cout << '\n';
        }
    }
    

    test: https://ideone.com/lRBTq

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

Sidebar

Related Questions

i have the following code ..i need to loop through end of the file
I have a fixed-length data file need to persistence into database. I use a
I need to loop through all controls on ASP.NET webpage. In configuration file I
I have a table of urls which I need to loop through, download each
I have a list of richTextFormat documents that I need to loop through and
I have a config file like this. [rects] rect1=(2,2,10,10) rect2=(12,8,2,10) I need to loop
I have imported a .csv file of 10,000 locations and I need to loop
I have a file I need to rename to that of an existing file.
I have a pdf file.I need to find all the hyperlinks available in that
I have a file format I need to be able to show in explorer

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.