I have a c++ homework. The homework is asking to convert a c program to c++.
Below is the question:
You are requested to convert the following C function into a C++
function and then embed it into a complete program and test it. Note
that this function copies a binary file of integers and not a text
file. The program must accept the arguments (the file to copy and the
file to be copied to) from the command line.
/* ==================== cpyFile =====================
This function copies the contents of a binary file
of integers to a second file.
Pre fp1 is file pointer to open read file
fp2 is file pointer to open write file
Post file copied
Return 1 is successful or zero if error
*/
int cpyFile (FILE *fp1, FILE *fp2)
{
/* Local Definitions */
int data;
/* Statements */
fseek (fp1, 0, SEEK_END);
if (!ftell (fp1))
{
printf ("\n\acpyFile Error : file empty\n\n");
return 0;
} /* if open error */
if (fseek (fp1, 0, SEEK_SET))
return 0;
if (fseek (fp2, 0, SEEK_SET))
return 0;
while (fread (&data, sizeof (int), 1, fp1))
fwrite (&data, sizeof (int), 1, fp2);
return 1;
} /* cpyFile */
I did my best and managed to convert it, but unfortunately when I’m using it , the file that I get after the copy is empty. Below is my answer:
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc,char* argv[])
{
if(argc!=3)
{cerr<<"invalid number of arguments. must be 3."<<endl;exit(1);}
fstream fp1(argv[1],ios::in);
if(!fp1)+{cerr<<argv[1]<<" could not be opened"<<endl;exit(1);}
fstream fp2(argv[2],ios::out);
if(!fp2)+{cerr<<"file could not be found."<<endl;exit(1);}
int data;
fp1.seekg (0,ios::end);
if (!fp1.tellg ())
{
cout<<"\n\acpyFile Error : file empty\n\n";
return 0;
} /* if open error */
if (fp1.seekg (0, ios::beg))
return 0;
if (fp2.seekg (0, ios::beg))
return 0;
while (fp1.read (reinterpret_cast<char*>(&data), sizeof (int)))
{
fp2.seekp(0);
fp2.write (reinterpret_cast<char*>(&data), sizeof (int));
}
return 1;
}
I did my best and everything is working fine, except that when I copy a binary file, the file that i get is empty and I have no idea why.
You need to open the file in binary mode, as others have said, by doing
Or you can make them
ifstream(in file stream for reading only) andofstream(out file stream, for writing only) and remove theios::inandios::outbecauseifstreamimpliesios::inandofstreamimpliesios::out:You need to do this because if you don’t, the file will be translated when you read from or write to it for things like turning line endings from
\r\nor\rto just\n, etc, which will mess up your binary data which may happen to have those bytes in them.This:
Will always make your code return because
seekgreturns the object you call it on. It’s not the equivalent offseekin this regard becausefseekreturns 0 on success. So you never get to thewhileloop. Take those out of theifstatements so that it looks like this:Or if you have to have the checking, you want to do
Also, this (inside the
while):Is setting the point you are going to write to to the beginning of the file. So you’ll never write anything but at the beginning of the file. Just remove that line completely.
Also, you have aNevermind that, misread due to the unusual brace style.returninside the loop which makes it return on the first iteration. Move thereturn 1;outside the loop so you only return after the loop is finished.