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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:32:02+00:00 2026-06-11T04:32:02+00:00

I am trying to read 200,000 records from a file and then use tokenizer

  • 0

I am trying to read 200,000 records from a file and then use tokenizer to parse the string and remove the quotes which are around each part. But the running time is very high compared to normally reading a string. It took 25 seconds just to read these records (0.0001 second per record????). Is there any problem with my programming or if not is there a faster way to do this?

int main()
{
    int counter = 0;
    std::string getcontent;
    std::vector<std::string> line;
    std::vector< std::vector<std::string> > lines;

    boost::escaped_list_separator<char> sep( '\\', '*', '"' ) ;
    boost::tokenizer<> tok(getcontent);

    std::ifstream openfile ("test.txt");

    if(openfile.is_open())
    {
        while(!openfile.eof())
        {
            getline(openfile,getcontent);

            // THIS LINE TAKES A LOT OF TIME
            boost::tokenizer<> tok(getcontent); 

            for (boost::tokenizer<>::iterator beg=tok.begin(); beg!=tok.end(); ++beg){
                line.push_back(*beg);
            }

            lines.push_back(line);
            line.clear();
            counter++;
        }
        openfile.close();
    }
    else std::cout << "No such file" << std::endl;

    return 0;
}
  • 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-06-11T04:32:03+00:00Added an answer on June 11, 2026 at 4:32 am

    At least if I’m reading this correctly, I think I’d take a rather more C-like approach. Instead of reading a line, then breaking it up into tokens, and stripping out the characters you don’t want, I’d read a character at a time, and based on the character I read, decide whether to add it to the current token, end the token and add it to the current line, or end the line and add it to the vector of lines:

    #include <vector>
    #include <string>
    #include <stdio.h>
    #include <time.h>
    
    std::vector<std::vector<std::string> > read_tokens(char const *filename) {
        std::vector<std::vector<std::string> > lines;
        FILE *infile= fopen(filename, "r");
    
        int ch;
    
        std::vector<std::string> line;
        std::string token;
    
        while (EOF != (ch = getc(infile))) {
            switch(ch) {
                case '\n':
                    lines.push_back(line);
                    line.clear();
                    token.clear();
                    break;
                case '"':
                    break;
                case '*':
                    line.push_back(token);
                    token.clear();
                    break;
                default:
                    token.push_back(ch);
            }
        }
        return lines;
    }
    
    int main() {
        clock_t start = clock();
        std::vector<std::vector<std::string> > lines = read_tokens("sample_tokens.txt");
        clock_t finish = clock();
        printf("%f seconds\n", double(finish-start)/CLOCKS_PER_SEC);
        return 0;
    }
    

    Doing a quick test with this on a file with a little over 200K copies of the sample you gave in the comment, it’s reading and (apparently) tokenizing the data in ~3.5 second with gcc or ~4.5 seconds with VC++. I’d be a little surprised to see anything get a whole lot faster (at least without faster hardware).

    As an aside, this is handling memory about as you originally did, which (at least in my opinion) is pretty strong evidence that managing memory in the vector probably isn’t a major bottleneck.

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

Sidebar

Related Questions

Im trying to read a column of String values from my DB. Im trying
I am trying to read from a file and encrypt the data using AES
For a project I'm trying to read an int and a string from a
Here is my problem, I'm trying to read in data from a file beginningbalance.dat
I'm trying to use the Net::Twitter::Stream Perl module from CPAN to read the stream
When trying to read a RSA private key from a file using the method
I'm trying to open a file, and read from it.. but I'm having some
I am trying to read the src attribute of the flash file which i
I am trying to read from a file that has comma seperated values like
I'm trying to read a remote binary file (say, image) from internet like this:

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.