Aug 19, 2010 at 5:03am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
char * str1,*str2;
str1=argv[1];
ifstream ifs(str1);
if (!ifs)
{ cerr <<"file name is incorrect" <<endl;
exit(1); }
ofstream ofs1("copy.txt" ,ios_base::out);
ofstream ofs2("copy.htm" ,ios_base::out);
if (!ofs1)
{ cerr <<"error: can not open \"1.txt\" for output\n" ;
exit(1); }
if (!ofs2)
{ cerr <<"error: can not open \"1.txt\" for output\n" ;
exit(1);}
string line;
while (getline(ifs, line))
{
ofs1<<line<<endl;
}
ofs2<<ifs.rdbuf();
ifs.close();
ofs1.close();
ofs2.close();
cout<<"copied two file!" ;
cin.get();
return 0;
}
I want to make a copy.exe same like dos cmd copy.
but as you can see .it doesn't work well.can you give me some advice?
Last edited on Aug 20, 2010 at 5:15am UTC
Aug 19, 2010 at 9:03am UTC
Does it even compile? It seems like you haven't declared ofs, at least. Could you be more specific about your problem besides "it doesn't work well"?
Aug 19, 2010 at 1:53pm UTC
If you want to copy an archive, you better treat it as binary (using read and write), because you can't be sure that exists a \n
What is the purpose of this?
outfile<<infile.rdbuf();
Last edited on Aug 19, 2010 at 1:54pm UTC
Aug 20, 2010 at 1:41am UTC
hello ,friends
I have edit some mistakes.
now.it can compile.
but it can only output Either a copy.htm Or a copy.txt.
never Both!!!
if I cut "ofs2<<ifs.rdbuf(); "of line 19 to line 31
ofs2 will be disabled,i.e copy.txt will be create.
Aug 20, 2010 at 1:58am UTC
What? You mean it isn't creating both files or something?
Aug 20, 2010 at 2:07am UTC
yes,it can only creating one file at a time.
if you exchange the position of code line 19 and line 31.
Aug 20, 2010 at 5:15am UTC
yes ,youare right.thank you.
but.do you notice that the copy.htm? is continous .there is no new line.but the copy.txt is correct.
Last edited on Aug 20, 2010 at 5:19am UTC