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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:48:36+00:00 2026-06-14T13:48:36+00:00

I need to insert data in a search binary tree reading a text file.

  • 0

I need to insert data in a search binary tree reading a text file. It works when I use spaces between the data, but it doesn’t work if I use commas like I want to.
For example I would want my text file to look like this:

New-York,c3,Luke
London,c5,Nathan
Toronto,c1,Jacob
  ...

My text file right now look like something like this:

New-York c3 Luke
London c5 Nathan
Toronto c1 Jacob
  ...

I’d also want my program not to think a space means it needs to look at the next data, only with commas.

This is how my code looks like:

void fillTree( BinarySearchTree *b)
{
    ifstream file;
    file.open("data.txt");
    string city;
    string tag;
    string name;
    Person p;

    if(!file) {
        cout<<"Error. " << endl;
    }

    while(file >> city >> tag >> name)
    {
        p.setCity(city);
        p.setTag(tag);
        p.setName(name);
        cout << p.getCity() << " " << p.getTag() << " " << p.getName() << endl;
        (*b).insert(p);
    }
    file.close();
}

What would I need to change in my code so I could use commmas instead of spaces? I feel like it would look more neat in the text file with commas instead of spaces.
I’m pretty sure I’d need to edit something in this block of code, but if it could be somewhere else please let me know.

  • 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-14T13:48:37+00:00Added an answer on June 14, 2026 at 1:48 pm

    use getline ( file, value, ',' ); to read string until coma occurs.

    string value;
    while(file.good())
    {
        getline ( file, value, ',' );
        p.setCity(value);
        getline ( file, value, ',' );
        p.setTag(value);
        getline ( file, value, ',' );
        p.setName(value);
        cout << p.getCity() << " " << p.getTag() << " " << p.getName() << endl;
        (*b).insert(p);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to insert, delete and update data using a xml file. I have
I need to insert input field data into MySQL. But the thing is that
Text file (or CSV) is: Data:,,,,,\n (but with 100 ,s) In C or C++
I basically need a data structure that works just like a Set , but
Need help to figure out why the following code for basic Binary Search Tree
I am writing a Generic Binary Search Tree. I need to compare two generic
Hi need insert data in table if the record is not already exits Ex:
I need to insert data from table1 into table2. However, I would like to
I need to insert data into a column in the database. Which hook should
I need to insert/update data by using link or image I need a code

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.