I have a text file which has data in it in the following format:

string string char int double string

Each field is separated by a tab character. The last string of every line contains multiple words. The file can have up to 50 lines (I am guessing if I can do it for 2 lines I can do it for any number of line). The last line of the file has a keyword announcing the end of the file.

I searched the internet, my notes, and textbook for the past 8 hours to figure this out, but it seems like I'm really dumb (at the moment, at least). It must be something relatively easy, but I can't figure it out.

I have to read the data from the text file in such a manner that I can list them later in different forms (e.g., maybe I want to show all the strings in the first and second column, maybe only the string in the second column and the double, etc.).

#include <iostream>
#include <fstream>
#include <string>
#include <vector> 
using namespace std;

int main ()
{
   
    vector<string> lines; //vector to hold all the lines
    string line; //string to hold intermediary line
    
    ifstream myfile ("example.txt"); 
    if (myfile.is_open()) //if the file is open
    {
        while ( getline (myfile,line ).good () ) 
        {
            getline (myfile,line); //get one line from the file
            
			lines.push_back(line);
             /*	checking vector size. not what is expected. 
                only every other line is saved. grrrrr!!!!
             */  
                cout << "vector size: " << lines.size()<< endl; 
        }
             /* this for loop is here to test if the vector has saved all the
		lines from the while above. IT HASN'T. ONLY LINES 2,4,6,8....
		ARE SAVED. I DO NOT KNOW WHY!
	     */
	        for (int i=0; i<lines.size() ;i++ )
                {
		   cout << lines[i] << endl;
                }
		myfile.close(); //closing the file
    }
    else cout << "Unable to open file"; //if the file is not open output
    
    return 0;
}

If you can give me a BIG HINT on how to do this I would greatly appreciate it. (I probably need more than a hint considering the amount of time I've spent on this and the results, or the lack of, that I came with. Oh, and most of the code above is from the forums. I think I understand the general idea, but, as they say: easy to learn, hard to master.

Thanks a lot!

Recommended Answers

All 3 Replies

since you have a known format with with data separated by white spaces, we can create an object to hold each line from the text file:

struct account
{
    //string string char int double string
    string first_name;
    string last_name;
    char m_f;
    int age;
    double balance;
    string address;
};

Now you can load a text file into a data structure of your choice:

vector<account> account_list;

account temp;

while(infile)
{
     infile >> temp.first_name;
     infile >> temp.last_name;
     infile >> temp.m_f;
     infile >> temp.age;
     infile >> temp.balance;
     //since we need to include the white spaces with the address data...
     getline(infile, temp.address);  

     //load the data structure
     account_list.push_back(temp);
}

Here is a simple function to display the data:

void display(vector<account> account_list)
{
     cout << "\n\tFirst\tLast\tGender\tAge\tBalance\tAddress"
          << "\n\t--------------------------------------------------------";

     for(int i=0, size=account_list.size(); i<size; i++)
     {
          cout << account_list[i].first_name << '\t';
          cout << account_list[i].last_name << '\t';
          cout << account_list[i].m_f << '\t';
          cout << account_list[i].age << '\t';
          cout << account_list[i].balance << '\t';
          cout << account_list[i].address << '\t';
          cout << endl;
     }
}

since you have a known format with with data separated by white spaces, we can create an object to hold each line from the text file:

struct account
{
    //string string char int double string
    string first_name;
    string last_name;
    char m_f;
    int age;
    double balance;
    string address;
};

Now you can load a text file into a data structure of your choice:

vector<account> account_list;

account temp;

while(infile)
{
     infile >> temp.first_name;
     infile >> temp.last_name;
     infile >> temp.m_f;
     infile >> temp.age;
     infile >> temp.balance;
     //since we need to include the white spaces with the address data...
     getline(infile, temp.address);  

     //load the data structure
     account_list.push_back(temp);
}

Here is a simple function to display the data:

void display(vector<account> account_list)
{
     cout << "\n\tFirst\tLast\tGender\tAge\tBalance\tAddress"
          << "\n\t--------------------------------------------------------";

     for(int i=0, size=account_list.size(); i<size; i++)
     {
          cout << account_list[i].first_name << '\t';
          cout << account_list[i].last_name << '\t';
          cout << account_list[i].m_f << '\t';
          cout << account_list[i].age << '\t';
          cout << account_list[i].balance << '\t';
          cout << account_list[i].address << '\t';
          cout << endl;
     }
}

Thanks for the answer, Clinton, but I am not allowed to use struct in the assignment, since we haven't covered it in class. I apologize for not mentioning it in my initial post. I will edit it now.

commented: Do not edit initial posts. It makes the replys look stupid. +17

then just create individual arrays instead of using the struct:

string first_name[100];
string last_name[100];
char m_f[100];
int age[100];
double balance[100];
string address[100];

follow the same general protocol. load each array element with a piece of data from the text file.

it's just that easy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.