Read complex txt file

Hi,

I have to txt file that I want to read it and arrange the elements into several vectors. The txt file looks like:

type         title         year      number
research     abc de fg?    2005      254874
569214
news         bcd ef gh.    2008      269858

Each column is separated by Tabulator. I create 4 vectors to register elements of each column. However, if the element of first column is not string (like the second row, which is int), I need to put it in the vector of fourth column (number), and repeat the other elements of precedent row (here type, title and year of the first row).
I have following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	vector<string> type;   // 1st column
	vector<string> title;  // 2nd column
	vector<int> year;      // 3rd column
	vector<string> number; // 4th column

	string x;

	ifstream myfile("book1.txt"); // open the txt file
	getline(cin, x);              // take the 1st element of each row
	if(static_cast<int>(x) != x)  // if it is not int
           type.push_back(x);
        else
           type.push_back(*(type.end()-1)); // copy the precedent element
        // how to switch to the second element of each row??
	return 0;
}

My code has some problems. How to take each time one element of each row? How to take into account the change of line in the txt?...

Could anyone please help me to make it work? Thanks a lot!
Last edited on
Read whole line into string. If string contains tabs, it should have four fields? If no tabs, just the number?

Split the long string by the tabs.
Topic archived. No new replies allowed.