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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:16:15+00:00 2026-05-19T23:16:15+00:00

Good morning to everybody, :) I have writen a code in C++ that reads

  • 0

Good morning to everybody, 🙂

I have writen a code in C++ that reads information from a txt file. It takes the information in the first row saving it in a string and then I want to work with this info. I want to read this string and when it finds a “|” character it must jump to a new line. Its something quite easy but I’m having problems when executing and I’ve been trying to find the problem for hours and I haven’t suceed. 🙁 I attached the code.

Thanks in advance for your help.

#include <string>
#include <fstream>
#include <vector>
#include <iostream>

using namespace std;

int main()
{

    ifstream ifs( "C:\\a\\text.txt" );
    string temp;

    getline( ifs, temp ); 
    cout<<temp<<endl;

    string * pch;
    pch = strtok (temp,"|");

    while (pch != NULL)
    {
        printf ("%s\n",pch);
        pch = strtok (NULL, "|");
    }

    system("pause");
    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-05-19T23:16:15+00:00Added an answer on May 19, 2026 at 11:16 pm

    strtok works with char*, not string*. That’s probably why you’re experiencing issues.

    Since you’re implementing this with c++, I suggest you use the string functions instead of strtok:

    int main()
    {
    
        ifstream ifs( "C:\\a\\text.txt" );
        string temp;
    
        getline( ifs, temp ); 
        cout<<temp<<endl;
    
        size_t tokenPos = temp.find("|");   
    
        while (tokenPos != string::npos)
        {
            cout << temp.substr(0, tokenPos) << endl;
            temp.erase(0, tokenPos+1);
            tokenPos = temp.find("|");  
        }
    
        system("pause");
        return 0;
    
    }
    

    To store your text in the values you described in your comment, you’d do the following:

    int main() 
    { 
        ifstream ifs( "C:\\a\\text.txt" ); 
    
        int id; 
        int type; 
        int columns; 
        string temp; 
    
        getline( ifs, temp ); 
        cout<<temp<<endl; 
    
        size_t tokenPos = temp.find("|");
        while (tokenPos != string::npos) 
        { 
            int i=0; 
            tokenPos = temp.find("|"); 
            cout << temp.substr(0, tokenPos) << endl; 
    
            if(i==0)
            { 
                id = atoi(temp.substr(0, tokenPos).c_str()); 
            }
            else if(i==1) 
            { 
                type = atoi(temp.substr(0, tokenPos).c_str()); 
            } 
            else if(i==2) 
            { 
                columns = atoi(temp.substr(0, tokenPos).c_str()); 
            } 
    
            ++i; 
            temp.erase(0, tokenPos+1); 
        } 
    
        cout << "ID: " << id << ", Type: " << type << ", Columns: " << columns << endl; 
        system("pause"); 
        return 0; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good Morning, I have copied this code from somewere <script type=text/javascript> $(function() { setInterval(rotateImages(),
Good Morning All, I have an Objective C question that is coming from JavaScript.
Good morning gang. I have a jQuery Datepicker object that is prepoulated using a
Good morning. I have a modal JQuery dialog that on Open calls and loads
Good Morning, I have a SharePoint site that I've been trying to fix up
Good morning everybody. I have a question connected with controls and event handling. Lets
Good morning, I have to use the properties from DeviceNetworkInformation in my application. (http://msdn.microsoft.com/en-us/library/microsoft.phone.net.networkinformation.devicenetworkinformation(v=vs.92).aspx)
Good morning folks, I'm just that beginner in json, I have a formated data
Good morning. I have an XML file which contains lists of warning and errors
Good morning, I have a .Net 2.0 runtime DLL that I am trying to

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.