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.
#include<iostream>
#include<fstream>
#include<string>
usingnamespace 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 = newint*[N];
for(int i = 0; i < N; i++)
ary[i] = newint[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 < constchar* > (&line), sizeof(line));
outbin.close();
writeTo << line << endl;
writeTo << line << endl;
writeTo.close();
//cout << line << '\n'; // print data from file1
}
//readFrom.close();
return 0;
}