Write a program that emulates the DOS COPY command. That is, it should copy the
contents of a text file (such as any file) to another file. Invoke the program with two
CPP command-line arguments—the source file and the destination file—like this:
C>ocopy srcfile.cpp destfile.cpp
In the program, check that the user has typed the correct number of command-line arguments, and that the files specified can be opened.

i dont know about command line arguments in OOP ....so please help me out solving this

Recommended Answers

All 11 Replies

i dont know about command line arguments in OOP

There's nothing OOP about it. main() takes two parameters: an integer containing the count of arguments, and an array of pointers to char containing the string value of each argument:

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    for (int i = 0; i < argc; i++)
        cout<< i <<": '"<< argv[i] <<"'\n";
}

That's really all there is to it.

This should help you to get the arguments

[EDIT]
Wow Narue you are quick with the keyboard. Remind me never to get into a typeout with you.
[/EDIT]

i found this code . this just tells the arguments of the CPP file.....
read the question above , then tel me how to open 1 tst file and then another to copy contents

sorry its txt file

In order to do this you will need to open the files and then tranfer the data bettwen them. If you want to do this with OOP you will need to read up on using fstream. the string class will also be helpful

this just tells the arguments of the CPP file.....

I'm not going to do your homework for you. My example was showing you how to use command line arguments. It's your job to use them for simulating MS-DOS's COPY. Or do you have a specific question that doesn't boil down to "please give me the whole thing"?

huh w8 lemme show you what i have done,

#include <fstream>                
#include <iostream>
using namespace std;
#include <process.h>              
int main(int argc, char* argv[] )
{
cout << "\nargc = " << argc << endl;  
for(int j=0; j<argc; j++)             
cout << "Argument " << j << " = " << argv[j] << endl;
if( argc != 1 )
{
cerr << "\nFormat: type filename";
exit(-1);
}

char ch;                    
ifstream infile;             
infile.open( argv[1] );       
if( !infile )                 
{
cerr << "\nCan't open " << argv[1];
exit(-1);
}
while( infile.get(ch) != 0 )  
cout << ch;                

if( argc == 1 )
{ cerr << "\nFormat: ocopy srcfile destfile"; exit(-1); }

infile.open( argv[1] );      
if( !infile )               
{ cerr << "\nCan't open " << argv[1]; exit(-1); }
ofstream outfile;             
outfile.open( argv[2] );      
if( !outfile )             
{ cerr << "\nCan't open " << argv[2]; exit(-1); }
while( infile )           
{
infile.get(ch);         
outfile.put(ch);         
}
return 0;
}

gives an error during run..

Wow, that's pretty horrible. Clearly you know how to open and read a file. You also know how to write to one. It looks to me like you're confused about how to check the command line arguments for validity, as well as the specific steps that this program needs to go through:

  1. Validate arguments
  2. Open files
  3. Copy input to output
  4. Close files

Note that if you're expecting two files, argc should not be less than 3. This makes for easy validation:

if (argc < 3) {
    cerr<<"usage: ocopy srcfile destfile";
}
else {
    // Copy srcfile to destfile
}

[edit]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.