Read a .txt file into arrays

Hello,

So I´m new in C++ programming and I´m trying to read a .txt file into arrays.

My file contains 4 columns separated with space. The first column contains words (just one word per line) and the last three contain numbers. The file has unknown number of lines.

I know how to read a file in c++ but I need help to read the columns into 4 separated arrays.

Does anyone here have a tip to do that ?

thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    string thefilename;
    ifstream infile;

    cout << " Write the file path : " << endl;
    cin >> thefilename;
    cout << endl;

    infile.open(thefilename.c_str());
    if ( infile.fail() ) {
        cout << " Cannot open file: " << thefilename << endl;
        exit(1);
    }


    infile.close();
    return 0;
}




So the file contains information about number of students at a university.

The 1 column contains information about the department, 2 column contains the year, 3 contains number of men and the 4 column contains number of women.

The program needs to give the user a menu to choose from.
If the user chooses "0: total " then the program gives number of student each per year that the file contains independent of what the student was studying.

and if the user chooses for example "1: business" than the programprogram gives number of students per each year at the business program
Last edited on
The first column contains words

That statement is not clear. Can the first column contain more than one word? If so, the problem is a little harder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <string>
#include <iostream>
#include <fstream>
using namespace std;


const int MAX_ROWS = 100;

int main()
{   string thefilename;
    ifstream infile;
    string col1[MAX_ROWS];
    int col2[MAX_ROWS], col3[MAX_ROWS], col4[MAX_ROWS];
    int count = 0;
    
    cout << " Write the file path : " << endl;
    cin >> thefilename;
    cout << endl;

    infile.open(thefilename.c_str());
    if ( infile.fail() ) 
    {   cout << " Cannot open file: " << thefilename << endl;
        exit(1);
    }
    
    while (infile >> col1[count] >> col2[count] >> col3[count] >> col4[count] && count < MAX_ROWS)
    {   //  Read succeded
        //  Do something with the columns
        count++;
    }

    infile.close();
    return 0;
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
The first column only contains one word per line.

And I need to read the columns into separate arrays. col1[]
And I need to read the columns into separate arrays. col1[]

The code I posted above reads the columns into separate arrays (col1,col2,col3,col4).
Thank you!

So the file contains information about number of students at a university.

The 1 column contains information about the department, 2 column contains the year, 3 contains number of men and the 4 column contains number of women.

The program needs to give the user a menu to choose from.
If the user chooses "0: total " then the program gives number of student each per year that the file contains independent of what the student was studying.

and if the user chooses for example "1: business" than the programprogram gives number of students per each year at the business program
Topic archived. No new replies allowed.