Read file and input into vector

Hey everyone! I am pretty new to C++ (just started this month) and will appreciate if someone can help me here.

I have a text file with the following fixed contents (only two lines, nothing more):

1
2
43 65 123 13 41
83 67 22


I need it to turn into a vector array where I can apply the name.size() to find out the length of the array and also to make use of the integers inside for other purposes.

The result should be:

1
2
3
4
5
Array 1: 43 65 123 13 41
Array 2: 83 67 22

Array 1 Length: 5
Array 2 Length: 3


I have successfully read the text file and added the content using .push_back(). However when I cout the vector .size(), it returns 2 and I realised that it consider each line as an element in the array.

What is the approach to tackling this?

Thanks!

Edit:

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
vector <string> readFile(const string& fileName)
{
    ifstream source;
    source.open(filename);
    vector <string> lines;
    string line;

    while(getline(source, line)
    {
        lines.push_back(line);
    } return lines;
}

int main(int argc, char ** argv)
{
    string inputFile(argv[1]);
    vector <string> fileData = readFile(inputFile);
    
    // Check vector
    for(auto i : fileData)
        cout << i << endl;

    // Check vector length
    cout << fileData.size() << endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

std::vector<int> load_vector() {
    std::vector<int> v;
    std::string line;
    if (std::getline(std::cin, line)) {
        std::istringstream iss(line);
        int n;
        while (iss >> n)
            v.push_back(n);
    }
    return v;
}

int main() {
    auto a = load_vector();
    auto b = load_vector();
    std::cout << a.size() << ' ' << b.size() << '\n';
}

Hey there! Thanks for the reply.

I am not sure how to implement your code into mine. I have edited my original post with the code. The read in .dat file is something I took from another place to try this.

Can you advise on how to implement your code into my existing one?
I guess I don't understand what you're trying to do.
I thought you wanted each line in a separate vector of ints.
But instead you are reading the lines into a single vector of strings.
Hey there! The code I posted is my original code which is not what I wanted.

I need each line to be a in separate vector of ints. Its just that I do not understand how to implement your code into mine.
Maybe something like this:

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

vector<int> load_vector(istream& in) {
    vector<int> v;
    string line;
    if (getline(in, line)) {
        istringstream iss(line);
        int n;
        while (iss >> n)
            v.push_back(n);
    }
    return v;
}

int main(int argc, char** argv) {
    if (argc != 2) {
        cerr << "Usage: myprog FILENAME\n";
        return 1;
    }

    ifstream fin(argv[1]);
    auto a = load_vector(fin);
    auto b = load_vector(fin);
    fin.close();

    cout << a.size() << ' ' << b.size() << '\n';
}

Topic archived. No new replies allowed.