why can't I output two files?

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
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"?
You don't have an include for exit (http://cplusplus.com/reference/clibrary/cstdlib/exit/),
you declare the output files ofs1 and ofs2 (without a check) but call "outfile" (which is not declared) as your output file.
It can't compile...
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
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.
What? You mean it isn't creating both files or something?
yes,it can only creating one file at a time.

if you exchange the position of code line 19 and line 31.
f you have writing permission both files should be created by the constructors (empty)

1
2
3
ofs2<<ifs.rdbuf(); //I think you're moving the pointer
// ifs.tellg() != 0
ifs.seekg(0, ios::beg); //going back to the start of the file 
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
Topic archived. No new replies allowed.