need help converting c to c++ (simple error but cant fix)

dear cpp community.
its my first post here. hope i could get some answers . thank you.

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 im 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. thanks again.
[code] "Please use code tags" [/code]
$ cp cpyFile.{c,cpp}
1
2
3
4
while (fp1.read (reinterpret_cast<char*>(&data), sizeof (int))){
  fp2.seekp(0); // <---
  fp2.write (reinterpret_cast<char*>(&data), sizeof (int));
}

Also, limit the privileges. Use ifstream for read and ofstream for write.

By the way, you didn't do the function.
Topic archived. No new replies allowed.