.txt to matrix

Hello everyone, I made a code to convert .dat to .txt, and after put the .txt created in other 2 matrices (distances_1 and distances_2). The problem is that the only last element(1023,319) of the matrices are not read properly, the last element have a value of 400000 but still being show as zero. All the other elements are corrects, only the last is not read. Could Someone take a look in the code and show me my mistake?

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  const int SIZEX = 1024;
const int SIZEY = 320;
const char* const InputFile = "1024x320.dat";
const char* const OutputFile = "C:/Users/Home/OneDrive/Desktop/output.txt";


int main() {
    std::vector<std::vector<float>> matrix(SIZEX, std::vector<float>(SIZEY, 0));
         std::ifstream in(InputFile, std::ios::binary);
    if (!in) {
        std::cout << "Error opening input file\n";
        return 1;
    }

    for (int y = 0; y < SIZEY; ++y) {      
        for (int x = 0; x < SIZEX; ++x) {  
           if (!in.read(reinterpret_cast<char*>(&matrix[x][y]), sizeof matrix[0][0])) {
               std::cout << "Error reading file!\n";
               return 1;
           }

        }
    }

    std::ofstream out(OutputFile);
    if (!out) {
        std::cout << "Error opening output file\n";
        return 1;
    }
    for (int y = 0; y < SIZEY; y++)
    {
        for (int x = 0; x < SIZEX; x++)
            out << matrix[x][y] << " ";
        out << '\n';
    }
    
    
    int y, x;
    std::vector<std::vector<float>> distances_1(SIZEX, std::vector<float>(SIZEY, 0));
    std::ifstream in1(OutputFile);

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

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

    in1.close();

    std::vector<std::vector<float>> distances_2(SIZEX, std::vector<float>(SIZEY, 0));
    std::ifstream in2(OutputFile);

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

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

    
    int i = 1023;
    int j = 319;
    std::cout << "\n\nDistances_1: ";
    std::cout <<  "\n1023,319: " << distances_1[1023][319];

    std::cout << "\n\nDistances_2: ";
    std::cout << "\n1023,319: " << distances_2[1023][319];

    
    return 0;
}


The .txt file created is a 1024x320, each element is separated by spaces.
Last edited on
Why are you starting another post?
Last edited on
Hello dutch, this is another question.
What does this print?

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

const int SIZEX = 1024, SIZEY = 320;
const char* const InputFile = "1024x320.dat";

int main() {
    std::vector<std::vector<float>> matrix(SIZEX, std::vector<float>(SIZEY));
    std::ifstream in(InputFile, in.binary);
    if (!in) {
        std::cout << "Error opening input file\n";
        return 1;
    }

    for (int y = 0; y < SIZEY; ++y) {      
        for (int x = 0; x < SIZEX; ++x) {  
           if (!in.read(reinterpret_cast<char*>(&matrix[x][y]), sizeof matrix[0][0])) {
               std::cout << "Error reading file!\n";
               return 1;
           }
        }
    }

    std::cout << matrix[SIZEX-1][SIZEY-1] << '\n';
}

prints the last term of the generated matrix from .dat
When I get that matrix and convert it into a .txt file, and define vectors to hold the values of each element, and print std::cout << distances_1[SIZEX-1][SIZEY-1] only the last element is wrong. All the others are right, only the last is sohwing zero instead its value.
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
int y, x;
    std::vector<std::vector<float>> distances_1(SIZEX, std::vector<float>(SIZEY, 0));
    std::ifstream in1(OutputFile);

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

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

    in1.close();

    

    
    int i = 1023;
    int j = 319;
    std::cout << "\n\nDistances_1: ";
    std::cout <<  "\n1023,319: " << distances_1[1023][319];

   

    
    return 0;
}
prints the last term of the generated matrix from .dat

So you are saying that you ran the code I posted and that it did NOT print zero?
Last edited on
I made a change to run std::ifstream in(InputFile, std::ios::binary and do not print zero. Was printed 400000.
You're missing out.close() after writing the original text output file.
In your original post above it would go on line 36.
Closing the file will "flush" the buffer, writing the last few numbers out.
Last edited on
To certify that, I also convert distance_1 matrix into a .txt, and looking there the last element was written as zero, nevertheless all the other was wirtten correctly. The last element was correct until I convert to a matrix variable.
I believe my last post has the answer.
PERFECT! Was that the solution!
Thank you so much for your patience
I am really grateful
It can be hard to spot a little error like that. That's why it's usually best to open a file within a scope and let it automatically close at the end of the scope:

1
2
3
4
5
6
7
8
9
10
11
12
    {
        std::ofstream out(OutputFile);
        if (!out) {
            std::cout << "Error opening output file\n";
            return 1;
        }
        for (int y = 0; y < SizeY; y++) {
            for (int x = 0; x < SizeX; x++)
                out << matrix[x][y] << ' ';
            out << '\n';
        }
    } // out will automatically close here 

Or, better yet, use functions and let the function scope do the work:

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
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>

using FVector = std::vector<float>;
using Matrix = std::vector<FVector>;

const int SizeX = 1024, SizeY = 320;
const char* const InputFile = "1024x320.dat";
const char* const OutputFile = "C:/Users/Home/OneDrive/Desktop/output.txt";

auto readBinaryFile(const std::string& filename) {
    std::ifstream in(filename, in.binary);
    if (!in) {
        std::cout << "Error opening input file: " << filename << '\n';
        exit(1);
    }
    Matrix v(SizeX, FVector(SizeY));
    for (int y = 0; y < SizeY; ++y)
        for (int x = 0; x < SizeX; ++x)
            if (!in.read(reinterpret_cast<char*>(&v[x][y]), sizeof v[0][0])) {
                std::cout << "Error reading file!\n";
                exit(1);
            }
    return v;
}

void writeFile(const std::string& filename, const Matrix& v) {
    std::ofstream out(filename);
    if (!out) {
        std::cout << "Error opening output file\n";
        exit(1);
    }
    for (int y = 0; y < SizeY; ++y) {
        for (int x = 0; x < SizeX; ++x)
            out << v[x][y] << ' ';
        out << '\n';
    }
}

auto readFile(const std::string& filename) {
    std::ifstream in(filename);
    if (!in) {
        std::cerr << "Cannot open file: " << filename << '\n';
        exit(1);
    }
    Matrix v(SizeX, FVector(SizeY));
    for (int y = 0; y < SizeY; ++y)
        for (int x = 0; x < SizeX; ++x)
            if (!(in >> v[x][y])) {
                std::cerr << "Missing value(s) in file.\n";
                exit(1);
            }
    return v;
}

int main() {
    auto matrix = readBinaryFile(InputFile);
    writeFile(OutputFile, matrix);
    auto distances = readFile(OutputFile);
    std::cout << "Last element: " << distances[SizeX-1][SizeY-1] << '\n';
}

Yes, that is true. I will get this habit, define functions is very good. Thank you so much
Topic archived. No new replies allowed.