Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7034321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:10:17+00:00 2026-05-28T01:10:17+00:00

I have a c++ homework. The homework is asking to convert a c program

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:10:18+00:00Added an answer on May 28, 2026 at 1:10 am

    You need to open the file in binary mode, as others have said, by doing

    fstream fp1(argv[1], ios::in | ios::binary); // combine ios::in with ios::binary
    
    fstream fp2(argv[2], ios::out | ios::binary); // combine ios::out with ios::binary
    

    Or you can make them ifstream (in file stream for reading only) and ofstream (out file stream, for writing only) and remove the ios::in and ios::out because ifstream implies ios::in and ofstream implies ios::out:

    ifstream fp1(argv[1], ios::binary);
    
    ofstream fp2(argv[2], ios::binary);
    

    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\n or \r to just \n, etc, which will mess up your binary data which may happen to have those bytes in them.

    This:

    if (fp1.seekg (0, ios::beg))
        return 0;
    
    if (fp2.seekg (0, ios::beg))
        return 0;
    

    Will always make your code return because seekg returns the object you call it on. It’s not the equivalent of fseek in this regard because fseek returns 0 on success. So you never get to the while loop. Take those out of the if statements so that it looks like this:

    fp1.seekg(0, ios::beg);
    fp2.seekg(0, ios::beg);
    

    Or if you have to have the checking, you want to do

    if (!fp1.seekg (0, ios::beg)) // notice the added !
        return 0;
    
    if (!fp2.seekg (0, ios::beg)) // notice the added !
        return 0;
    

    Also, this (inside the while):

    fp2.seekp(0);
    

    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 a return inside the loop which makes it return on the first iteration. Move the return 1; outside the loop so you only return after the loop is finished. Nevermind that, misread due to the unusual brace style.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on my compiler homework and I have the following question: Consider the
I have a homework assignment, and i am finished other then one question (see
Okay, so I have another question on a prolog homework problem I am struggling
I have a homework question regarding operator precedence in Fortran. In order to understand
This is homework...I'm not asking for answers, I just have a bug I'm not
I have this homework that I gotta finish, but then something about it is
Hey guys, I have a homework question that's been frustrating me to no end!
I beg pardon for not doing homework on this issue and directly asking question
I have a homework problem for my algorithms class asking me to calculate the
I have been pondering about my homework question for a while. I welcome (and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.