Friday, July 31, 2009

Write a program in C++ that emulates the DOS COPY command i.e. it should copy the contents of a character fil?

Write a program in C++ that emulates the DOS COPY command i.e. it should copy the contents of a character file (such as any CPP file) to another file. Invoke the program with two command line arguments – the source file and the destination file – like this





C%26gt;ocopy srcfile.ext destfile.ext





In the program check that the user has typed the correct number of command line arguments, and that the file specified can be opened. Improve on the DOS TYPE command by having the program signal an error if destination file already exist. This will prevent inadvertently writing over a valuable file (Use no replace flag)

Write a program in C++ that emulates the DOS COPY command i.e. it should copy the contents of a character fil?
Hope this will compile smoothly. if not mail me at m_gopi_m@yahoo.co.in





/* program to copy text on one file to another.


can be operated from command line.


*/





#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;stdio.h%26gt;





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


{


int flag=0;


char a;





if(argv[1][0]=='\0')


{


cout%26lt;%26lt;"\n parameters required.";


cout%26lt;%26lt;"\n For help type /?";


}


else if(argv[1][0]=='/')


{


if(argv[1][1]=='?')


{


cout%26lt;%26lt;"\n copys [source] [destination]";


cout%26lt;%26lt;"\n source is the path to the source file";


cout%26lt;%26lt;"\n destination is the path to the destination file";


cout%26lt;%26lt;"\n special arguments none ";


cout%26lt;%26lt;"\n for better results enter the file name and ";


cout%26lt;%26lt;"path to it in 8.3 format";


}


}


else


{





FILE *scr,*des;





des=fopen(argv[2],"r");


scr=fopen(argv[1],"r");





if(des!=NULL)


{


cout%26lt;%26lt;"\n The file already exist. Cannot overwrite.";


flag=1;


fclose(des);


}


else if(scr==NULL)


{


cout%26lt;%26lt;"\n The file is not found or the path is wrong.";


fclose(scr);


flag=1;


}





if(flag==0)


{


scr=fopen(argv[1],"r");


des=fopen(argv[2],"w");





a=fgetc(scr);


while(a!=EOF)


{


fputc(a,des);


a=fgetc(scr);


}





fclose(scr);


fclose(des);





cout%26lt;%26lt;"\n successfully copied...";


}


}


}
Reply:Use File System IO Stream to Read and Write Files.





Since these are only text file it'd be a text stream file as you aren't using any binary file data.





Try DOS Command Console programming with no GUI and start with a simple app.





FOR COPYING FILE (BASIC INPUT/OUTPUT FILE IO):





Simple File Operations Demo:


http://www.codeproject.com/file/alexfile...





File Copy Function Sample:


http://www.programmersheaven.com/downloa...





Simple How To:


http://gcc.gnu.org/onlinedocs/libstdc++/...





Code:


#include %26lt;fstream%26gt;





std::ifstream IN ("input_file");


std::ofstream OUT ("output_file");





A very simple file stream class, C++ File I/O Classes and Functions:


http://www.codersource.net/cpp_file_io.h...





A little more technical method for reading and writing to file:


http://www.gamedev.net/reference/article...





Detailed Method For Reading/Writing Binary File:


http://courses.cs.vt.edu/~cs2604/spring0...
Reply:1) first things first


%26gt;%26gt;command line arguments


in the above program , the name of the code is arg[0],


the source is arg[1]


and destination is arg[2]





2) Learn the file apis


to source file given by arg[0]


and the destination file given by arg[1]





3) to check if a file alreadys exists


%26gt;%26gt;read the destination file and make a counter


until endof of file is reached,


if counter is 0 , it means the file doesnot exist


so go ahead


4) using the file read and file write apis ,read from source


and write to destination





%26gt;%26gt;hey kiddo , dont expect the code but u can expect the


method here
Reply:You can get some help from


http://expert.myitcareer.org/


No comments:

Post a Comment