File I/O (HELP)!!

I have a inpu.dat binary file. I have to read this and assign the elements in it to a matrix. How can I do this? Thanks
What is the format of the inpu.dat binary file?
Do you know how to read a single number out of it?
Last edited on
Are you sure it's binary (not readable in a text editor)?
Are the numbers integers or floating point?
What size?
How many?
Is there initial data about the matrix size?

What is the output of this program (prints first 96 bytes in hex):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <iomanip>

int main() {
    unsigned char buf[96];

    std::ifstream in("input.dat", std::ios::binary);
    in.read((char*)buf, sizeof buf); // try to fill buf
    int cnt = in.gcount(); // number of bytes actually read

    using std::cout;
    cout << std::hex << std::setfill('0');
    for (int i = 0; i < cnt; ++i) {
        cout << std::setw(2) << unsigned(buf[i]) << ' ';
        if      (i % 16 ==  7) cout << ' ';
        else if (i % 16 == 15) cout << '\n';
    }
    cout << '\n';
}

Last edited on
Side note,

sizeof buf

How come the name "buf' doesn't refer to a char*, and thus give you the size of the pointer itself (like 8 on 64-bit system)?
Last edited on
A C-style array type contains its dimensional information which sizeof can interpret (in bytes/chars).
Once passed to a function it "decays" to a pointer and the dimensional information is lost, so sizeof just yields the size of the pointer in that case.

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
#include <iostream>
using std::cout;

void f(int a[100]) { // The '100' here is meaningless.
    cout << "f: " << sizeof a << '\n';  // just prints ptr size
    // There's no way to know that the array has a size of 100
    // unless you pass the size in or define a global size.
}

// You can retain dimensional information with a reference.
// But then it can't be called with a different size array.
void g(int (&a)[100]) {
    cout << "g: " << sizeof a << '\n';  // prints 400 (for 32-bit int)
}

void h(int a[10][10]) { // The first '10' is meaningless.
    cout << "h: " << sizeof a << ' '               // just pointer size
         << sizeof a[0] / sizeof a[0][0] << '\n';  // prints 10
    // Note that only the first dimension of array info is lost.
}

int main() {
    int a[100];
    cout << "a: " << sizeof a << ' ' << sizeof a / sizeof a[0] << '\n';
    f(a);
    g(a);
    int a2[90];
    f(a2); // okay
    //g(a2); // won't compile
    
    int b[10][10];
    int TotalBytes = sizeof b;
    int TotalElements = TotalBytes / sizeof b[0][0];
    int Rows = TotalBytes / sizeof b[0];
    int Cols = TotalElements / Rows;
    cout << "Total bytes: " << TotalBytes << '\n';
    cout << "Total elements: " << TotalElements << '\n';
    cout << "Rows: " << Rows << "  Cols: " << Cols << '\n';
    h(b);
}

Last edited on
Topic archived. No new replies allowed.