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

  • Home
  • SEARCH
  • 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 7515381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:47:23+00:00 2026-05-30T00:47:23+00:00

I am trying to tokenize a database dump separated by commas. I only need

  • 0

I am trying to tokenize a database dump separated by commas. I only need to read the first word, which will tell me if this is the line I need and then tokenize the line and save each separated string in a vector.

I have had trouble keeping all of the datatypes in order. I use a method of getline:

string line;
    vector<string> tokens;

// Iterate through each line of the file
while( getline( file, line ) )
{
    // Here is where i want to tokenize. strtok however uses a character array and not a string.
}

The thing is, I only want to continue reading and tokenize a line if the first word is what I am after. Here is a sample of a line from the file:

example,1,200,200,220,10,550,550,550,0,100,0,-84,255

So, if I am after the string example, it goes ahead and tokenizes the rest of the line for my use and then stops reading from the file.

Should I be using strtok, stringstream or something else?

Thank you!

  • 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-30T00:47:28+00:00Added an answer on May 30, 2026 at 12:47 am
    #include <iostream>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    void do(ifstream& file) {
        string line;
        string prefix = "example,";
    
        // Get all lines from the file
        while (getline(file,line).good()) {
            // Compare the beginning for your prefix
            if (line.compare(0, prefix.size(), prefix) == 0) {
                // Homemade tokenization
                vector<string> tokens;
                int oldpos = 0;
                int pos;
                while ((pos = line.find(',', oldpos)) != string::npos) {
                    tokens.push_back(line.substr(oldpos, pos-oldpos));
                    oldpos = pos + 1;
                }
                tokens.push_back(line.substr(oldpos)); // don't forget the last bit
                // And here you are!
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to adapt this answer How do I tokenize a string in C++?
I am trying to tokenize a string but I need to know exactly when
Based on this question which was closed rather quickly: Trying to create a program
I am trying to read from a text file and tokenize the input. I
I am trying to read data from a file, tokenize it and sort it,
Hi I'm trying to tokenize a string by loading an entire file into a
I'm trying to create a regex to tokenize a string. An example of the
I am learning Python and trying to figure out an efficient way to tokenize
I am trying to read contents of a file using string tokenizer and store
Trying to get my css / C# functions to look like this: body {

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.