为什么这个程序不起作用? (将文件内容复制到另一个的程序)

Why is this program not working? (Program to copy contents of file to another)

抱歉这个菜鸟问题,我是 C++ 的新手。我正在尝试编写一个程序来逐行将一个文件复制到另一个文件。这不会抛出任何错误,它 运行 但它不会创建目标文件。

让我知道错误在哪里..

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[]) {
  ifstream fin;
  ofstream fout;
  char line[255];

  if (argc < 3) {
    cout << "ERROR - Incorrect number of arguments" << endl;
  } else {
    if (std::ifstream(argv[1])) {
      if (std::ifstream(argv[2])) {
        cout << "ERROR - Destination file already exists" << endl;
      } else {
        fin.open(argv[1], ios::in);
        fin.open(argv[2], ios::out);

        while(fin >> line) {
          cout << line << endl;
          fout << line << endl;
        }

        fin.close();
        fout.close();
      }
    } else {
      cout << "ERROR - Source file does not exist" << endl;
    }

  }

  return 0;  
}

更新:我把它改成

后就可以用了
ifstream fin(argv[1]);
ofstream fout(argv[2]);

但它的复制很奇怪。它不是复制整行,它是这样复制的:

#include
<iostream>
#include
<fstream>
using
namespace
std;
int
main(int
argc,
char
*argv[])
{
string
line;
if
(argc
<
3)
{
cout
<<
"ERROR
-
Incorrect
number
of
arguments"
<<
endl;
}
else
{
if
(std::ifstream(argv[1]))
{
if
(std::ifstream(argv[2]))
{
cout
<<
"ERROR
-
Destination
file
already
exists"
<<
endl;
}
else
{
ifstream
fin(argv[1]);
ofstream
fout(argv[2]);
while(fin
>>
line)
{
cout
<<
line
<<
endl;
fout
<<
line
<<
endl;
}
fin.close();
fout.close();
}
}
else
{
cout
<<
"ERROR
-
Source
file
does
not
exist"
<<
endl;
}
}
return
0;
}

如何让它复制包括空格,并将整行视为整行?

更新:开始工作了:

while(getline(fin, line)) {
  cout << line << endl;
  fout << line << endl;
}

fin.open(argv[2], ios::out);更改为fout.open(argv[2], ios::out);