2d Array and File Handling

Background:

I'm attempting to read through a 2d array and find values that coincide with row numbers and column numbers.

Question:

How do I read values off a file and acquire, for example, 1 and 4 vs. 14?

Here's what I have so far...

All constructive criticism is welcome.

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
      int arrayOfNum[5][5] = {
        {34,21,32,41,25},
        {14,42,43,14,31},
        {54,45,52,42,23},
        {33,15,51,31,35},
        {21,52,33,13,23}};

    
    ofstream arrayFile;
    arrayFile.open("arrays.txt");
    

        if (arrayFile.is_open()) {
            cout << "File opened successfully..." << endl;
        }
    
    for (int i = 0; i <= 4; i++) {
        
        arrayFile << endl;
        
        for (int j = 0; j <= 4; j++) {
            
            arrayFile << arrayOfNum[j][i] << ' ';
        }

    }
What is the format of the data of the file you are trying to read in? You can read in one character at a time.

It looks like you've opened a file for output only in the code posted.


There are some examples and explanations on file handling in C++ here.
http://www.cplusplus.com/doc/tutorial/files/
http://www.umich.edu/~eecs381/handouts/filestreams.pdf
Well wont it read as a string? Ideally, I want to assign a variable of class int to parts of the txt file I read from

Oh wow, thank you!

How would I read one character at a time?

Using substr from the string library I'm assuming or is there a better way?
Last edited on
Topic archived. No new replies allowed.