Char error reading string from text file into a vector array C++

Hi all i have a file i'm trying to read into a vector array.
I've checked a few other post which got me as far as i did.
I keep running into an error where it's not allowing me to insert my string using the put_back() function. I keep getting a char error. The file i'm importing contains numbers and i'm trying to read it in as a string type to avoid loosing leading zeros.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <vector>
    using std::vector;
    using namespace std;
    string outPutFileName;
    vector<vector<string> > array2D;
    #define HEIGHT 32
    #define WIDTH 9

    int main() {
    string x;
    string line;
    string filename;
    ifstream infile;
    
    
    infile.open("file.txt");
    

    if (infile.fail()) {
        cerr << " The file you are trying to access cannot be found or opened";
        exit(1);
    }
    
    array2D.resize(HEIGHT);
    for (int i = 0; i < HEIGHT; ++i) {
        array2D[i].resize(WIDTH);
    }
        while (getline(infile, line)) {
            istringstream streamA(line);
            
            while (streamA >> x) {
                for (int row = 0; row < HEIGHT; row++) {
                    for (int col= 0; col < WIDTH; col++) {
                        array2D[row][col].push_back(x);
                        col++;
                    }
                    row++;
                }
            }
        }
        
        for (int i = 0; i <HEIGHT; i++) {
            for (int j = 0; j <WIDTH; j++) {
                cout << array2D[i][j] << " ";
            }
            cout << endl;
        }
        return 0;
    }
On line 38 you try to push_back(...) to a string. Since you did already the correct resize(...) you can actually write it like so:
array2D[row][col] = x;
Thanks for the reply, when i try that assignment my array gets loaded with zero's?
A friend mentioned that it's taking only the last value from the file and i should read into x every loop iteration, rather than reading once before the loop. Still stuck there. Not sure where to put the read?
Yes, the loops are wrong there. One way would be:
1
2
3
4
5
6
7
8
9
10
        while (getline(infile, line)) {
            istringstream streamA(line);
            
            col = 0;
            while (streamA >> x) {
                        array2D[row][col] = x;
                        col++; // Note: This might go out of bounds
            }
            row++; // Note: This might go out of bounds
        }


Brilliant that did the job

here is the revised code.
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using std::vector;
using namespace std;
string outPutFileName;
vector<vector<string> > array2D;
#define HEIGHT 32
#define WIDTH 9

int main() {
    string x;
    string line;
    string filename;
    ifstream infile;
    
    
    infile.open("file.txt");
    
    //error check
    if (infile.fail()) {
        cerr << " The file you are trying to access cannot be found or opened";
        exit(1);
    }
    array2D.resize(HEIGHT);
    for (int i = 0; i < HEIGHT; ++i) {
        array2D[i].resize(WIDTH);
    }
    int row;
    while (getline(infile, line)) {
        istringstream streamA(line);
        
        int col = 0;
        while (streamA >> x) {
            array2D[row][col] = x;
            col++; // Note: This might go out of bounds
        }
        row++; // Note: This might go out of bounds
    }
    
        for (int i = 0; i <HEIGHT; i++) {
            for (int j = 0; j <WIDTH; j++) {
                cout << array2D[i][j] << " ";
            }
            cout << endl;
        }

        return 0;
    }
    
Last edited on
Topic archived. No new replies allowed.