answersLogoWhite

0


Best Answer

#include<iostream>

#include<fstream>

#include<algorithm>

#include<string>

int copy_file (const std::string& src_name, const std::string& dst_name)

{

// Ensure source exists for input.

if (!std::ifstream (src_name).good())

{

std::cerr << "The system cannot find the file specified.\n" << std::endl;

return -1;

}

// If destination exists (for input), ask to overwrite.

if (std::ifstream (dst_name).good())

{

std::string yn;

while (!yn.size())

{

std::cerr << "The destination file exists. Overwrite? [Y/N]";

std::getline (std::cin, yn);

if (yn.size())

{

switch (yn[0])

{

case ('Y'): case ('y'): continue;

case ('N'): case ('n'): return -1;

default: yn.clear();

}

}

}

}

// Try opening or creating the destination.

std::ofstream dst (dst_name, std::ios_base::binary);

if (!dst.good())

{

std::cerr << "The destination file could not be created or opened for writing.\n" << std::endl;

return -1;

}

// Definitely good to go.

std::ifstream src (src_name, std::ios_base::binary);

std::cout << "Copying " << src_name << " to " << dst_name << std::endl;

dst << src.rdbuf();

std::cout << "\t1 file copied.\n" << std::endl;

return 0;

// Note: both files close when they fall from scope...

}

int main (int argc, char* argv[])

{

switch (argc)

{

case (3):

return copy_file (argv[1], argv[2]);

case (2):

if (std::string(argv[1])!="/?")

break;

// strip the path and extension from the command

std::string command (argv[0]);

size_t last_backslash = command.rfind ('\\') + 1;

size_t last_period = command.rfind ('.');

std::string exe = command.substr (last_backslash, last_period-last_backslash);

std::transform (exe.begin(), exe.end(), exe.begin(), ::toupper);

std::cerr << "Copies a file to another location.\n\n"

<< exe << " source destination\n\n"

"source\t\tSpecifies the file to be copied.\n"

"destination\tSpecifies the filename for the new file.\n" << std::endl;

return 0;

}

std::cerr << "The syntax of the command is incorrect.\n" << std::endl;

return -1;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<dos.h>

#include<sys\stat.h>

#include<io.h>

int main(int argc,char * argv[])

{

int fp,ft,buffsize=10;

char *buff=(char *)malloc(sizeof(char)*10);

char ch;

long count=0;

int cur1,cur2;

long ret1,ret2;

clrscr();

if(argc!=3)

{

puts("wrong syntax:use the below\n");

puts("copy file1 file2\n");

puts("copy source destination");

return(1);

}

if((fp=open(argv[1],O_RDONLY|O_BINARY,S_IREAD))==-1)

{

printf("can't open the file : %s\n", argv[1]);

perror("Error no:");

return(1);

}

if((ft=open(argv[2],O_RDWR|O_CREAT|O_BINARY,S_IREAD|S_IWRITE))==-1)

{

printf("can't open the file : %s\n", argv[2]);

perror("Error no:");

close(fp);

return(1);

}

while(read(fp,buff,buffsize)==buffsize)

{

if(write(ft,buff,buffsize)!=buffsize)

{

printf("cant write to file 1\n");

perror("Error no:");

close(fp);

close(ft);

return(1);

}

count++;

}

if ( lseek(fp,0L,SEEK_END)==-1)

{

printf("lseek failed 2");

close(ft);

close(fp);

return(1);

}

cur1=tell(fp);

if(cur1 -1)

{

printf("tell failed 4");

close(ft);

close(fp);

return(1);

}

ret1=read(fp,buff,cur1-cur2);

if(ret1==-1)

{

printf("read failed 6");

close(fp);

close(ft);

return(1);

}

ret2=write(ft,buff,cur1-cur2);

if(ret2==-1)

{

printf("write failed 7");

close(fp);

close(ft);

return(1);

}

close(fp);

close(ft);

printf("\n Copying file is successfull");

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

contact c teacher.....m an eng teacher

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Program to copy one file into another using command line arguments?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you pass command line arguments using turbo in c compiler?

In the Options menu the Arguments command.


How do you write c file arguments?

Using parameters argc and argv, and library functions fopen, fprintf, fclose


Please someone explain you command line argument in C?

Command Line Arguments---- I am trying to explain each word one by oneCommand ------perform specific taskex. When CMD is typed in run window and then press ok button then open a black screen that is called command prompt.Command Line ------on command prompt where command is supplied that is called command line.using command prompt dos commands are executed and java program is also executed using command prompt.for executing java program command is supplied with given syntaxjava java_class_name argument1 argument2.........Java--- is a commandjava_class_name ---name of java file which you want to executeargument1 argument2 --- are the values passing to java application from out sidethese argument1 argument2 are stored in string array argument of main method


How do you capture the output of C program in to another C program?

HIYou can first include the 1st program in ur 2nd program using # include and then whatever be the output from frst it can be used in second program.pankajThat's what popen is good for. Read the manual.


Write a programme to perform binary operations on integer argument The arguments and operators should be accepted using command line parameters?

There are many different operators, which are you referring to?

Related questions

How do you pass command line arguments using turbo in c compiler?

In the Options menu the Arguments command.


What command provides on screen information about the program your using?

KAIMAN


What is the purpose of using echo command?

The echo command echoes out any of the command line arguments given to it. It is commonly used in shell scripts to echo what portions of the shell script are doing.


How do am pass arguments in public static void main?

By using command line arguments we can pass values to the static void main method at the time of running the Java class. For example: if Class name is A,then to run this class and accepts command line then run this by using below line java A &lt;argument1&gt; &lt;argument2&gt; ....


How do you write c file arguments?

Using parameters argc and argv, and library functions fopen, fprintf, fclose


What is Computer Command Ride?

You probably mean "Command Line." The command line is a way of interacting with the computer by typing text based commands, rather than using a Graphical User Interface (GUI). Instead of using a mouse to double click the icon of a program, someone using the command line might simply enter the name of the program, and have it run.


What is the command to run your programexe using the command prompt?

Run program.exeorthe name of the program, with or without .exe (if the command prompt is in the same directory/folder of the executable)


How to you use clear screen in turbo C?

by using "clrscr" command at starting of the program


How do you write a program using command line arguments?

#include&lt;stdio.h&gt; int main( int argc, char *argv[]) { FILE *p; if(!argv[1]) { printf("can't open the file"); } else p=fopen("venki.txt","r" ); return 0; }


What does control C do?

Depends on which program you are using it with: - In a GUI it generally acts as the copy function - In a command-line it will kill the currently running program


Please someone explain you command line argument in C?

Command Line Arguments---- I am trying to explain each word one by oneCommand ------perform specific taskex. When CMD is typed in run window and then press ok button then open a black screen that is called command prompt.Command Line ------on command prompt where command is supplied that is called command line.using command prompt dos commands are executed and java program is also executed using command prompt.for executing java program command is supplied with given syntaxjava java_class_name argument1 argument2.........Java--- is a commandjava_class_name ---name of java file which you want to executeargument1 argument2 --- are the values passing to java application from out sidethese argument1 argument2 are stored in string array argument of main method


How do you execute a Java program?

You can run a Java application from the command line using "java &lt;name of the class&gt;"