Covert hexadecimal to binary using dynamic array

I have to create this program that reads in data from one file, places it in a 2D dynamically allocated array, and writes it in binary to another file. So far I think I have the correct code to read the file and put the information in the array, but I'm not sure how to convert it to binary and write it to another file. Sample input and output is shown. Any help would be appreciated.

Input: Output:
8 2
0x0 0x0 00000000
0x1 0x8 00011000
0x3 0xc 00111100
0x7 0xe 01111110
0x7 0xE 01111110
0x3 0xC 00111100
0x1 0x8 00011000
0x0 0x0 00000000


Here's my 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
56
57
58
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
        string line;                    //line of text to be displayed
        int N = 3;
        int M = 3;
        int count = 0;
        int nCols, nRows;

        ifstream readFrom("file1.dat");         // open file to read from
        readFrom >> nRows >> nCols;             // read in number of rows and columns
        if(readFrom.is_open()) {
                getline(readFrom, line);
                        while(getline(readFrom, line))
                        {

                                int** ary = new int*[N];
                                for(int i = 0; i < N; i++)
                                        ary[i] = new int[M];
                                for(int i = 0; i < N; i++)
                                        for(int j = 0; j < M; j++)
                                                ary[i][j] = i;

                                for(int i = 0; i < N; i++)
                                        for(int j = 0; j < M; j++)
                                                cout << ary[i][j] << "\n";
                                for(int i = 0; i < N; i++)
                                        delete [] ary[i];
                                delete [] ary;

                }
                // write data to another file

                        //outFile.open("file1.dat", ios::out | ios::binary);
                        ofstream writeTo("anotherFile.dat");
                        ofstream outbin("anotherFile.dat");
                        outbin.write( reinterpret_cast < const char* > (&line), sizeof(line));
                        outbin.close();
                        writeTo << line << endl;
                        writeTo << line << endl;
                        writeTo.close();

                        //cout << line << '\n';         // print data from file1
                }
                //readFrom.close();





    return 0;

}
Topic archived. No new replies allowed.