Convert .txt file into a 2D float matrix?

hello, I am a beginner i C++. I have been passing for some troubles here, my code gets the warning of exceeds/analysis stacksize, the error C6262 in visual studio. How to reduce the memory usage? My code is correct? Please do not hesitate in tell me my mistakes! I am aiming to put the data from the .txt into a variable matrix to do posterior calculus with it.


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
  #include <fstream>
#include <iostream>

int main()
{
    int x, y;
    float distances_1[1024][1024];
    std::ifstream in1("C:/Users/Home/OneDrive/Desktop/output.txt");

    if (!in1) {
        std::cout << "Cannot open file.\n";
        return 0;
    }

    for (y = 0; y < 1024; y++) {
        for (x = 0; x < 1024; x++) {
            in1 >> distances_1[x][y];
        }
    }

    in1.close();

    float distances_2[1024][1024];
    std::ifstream in2("C:/Users/Home/OneDrive/Desktop/output.txt");

    if (!in2) {
        std::cout << "Cannot open file.\n";
        return 0;
    }

    for (y = 0; y < 1024; y++) {
        for (x = 0; x < 1024; x++) {
            in2 >> distances_2[x][y];
        }
    }
    in2.close();

    // float a[1023][1023];
    //a[1023][1023] = distances_1[1023][1023] / distances_2[1023][1023];
     //  std::cout << a[1023][1023];
    return 0;
}
From: https://docs.microsoft.com/en-us/cpp/code-quality/c6262?view=msvc-160

To correct the problem behind this warning, you can either move some data to the heap or to other dynamic memory.

An easy -- and safe -- way to store the data on the heap is using a std::vector. std::vector stores its data on the heap automatically when using the default allocator.

1. Add #include <vector> to your includes.

2. Change the 2D array definitions at lines 7 & 23 to:
std::vector<std::vector<float>> distances_1(1024, std::vector<float>(1024, 0)); std::vector<std::vector<float>> distances_2(1024, std::vector<float>(1024, 0));

Your 2D containers are zero initialized now. The rest of the code should need no change.

Last edited on
Very interesting, it is for sure a better way to storage a matrix. Thank you so much Furry Guy.

Unfortunately a strange behavior appears when I was checking the values of the elements.

1
2
3
4
5
6
7
8
9
10
11
12
   
    /std::vector<std::vector<float>> result(size, std::vector<float>(size, 0));

    int i = 0;
    int j=0;
    std::cout << distances_1[i][j]; // just to check if the values was read correctly to perform the division below.

    //result[i][j] = distances_1[i][j] / distances_2[i][j];
    //std::cout <<"Result: "<< result[i][j];
    
    return 0;
}


The std::cout<<distances_1[i][j] only show me the correct value for [0,0], other elements always are printed as zero for some reason.
How many values are actually stored in your file? To fill a 1024 by 1024 2D container requires your file has at least 1,048,576 values.
I think that I found the reason. I tried with other .txt file where the elements a separated by spaces instead of commas, and works correctly. Apparently just do not work with elements separated by commas.
CSV files can cause problems with C++ input streams when using operator>>.

BTW, convention when dealing with 2D container loops has the row loop first, then the column loop second. You have the column loop first, row loop second.

If your input file was:
1 2 3
4 5 6
7 8 9

The 2d container would hold:
1 4 7
2 5 8
3 6 9

Even if your file isn't formatted, no carriage returns, you will have a issue that doesn't keep the data in the same order as in the file.
Did not know that. I am analyzing what I can do.
Thank you Furry Guy.
Last edited on
Topic archived. No new replies allowed.