The standard C library API is provided under the Linux platform to read and write files, and C + + also provides its own means for file stream operation. Although I do not know whether it will be used later, but it is necessary to understand.
Source:
#include <iostream>
#include <fstream>
using namespace std;
int main (void)
{ char filename1[256],filename2[256];
cout<< "Input source file name: ";
cin>>filename1;
cout<< "Input destination file name: ";
cin>>filename2;
Ifstream infile (filename1);
Ofstream outfile (filename2);
char ch;
while (Infile.get (CH))
outfile.put (CH);
Infile.close ();
Outfile.close ();
return 0;
}
In C + + to implement the operation of the file stream, you need to include the header file "include <fstream>". This program needs to copy the contents of a file into another file, for example, we have an existing file called Source.txt, which has the following content:
Hello,se7en:
Remember,hope is a good thing!
Compile run:
root@se7en-lifebook-lh531:~/learn/cpp_program# g++ file.cpp-o file
root@se7en-lifebook-lh531:~/learn/cpp_ program#./file
Input source file name: source.txt
Input destination file name: dest.txt
root@se7en-lifebook-lh531:~/learn/cpp_program# Cat Dest.txt
hello,se7en:
remember,hope is a good thing!
root@se7en-lifebook-lh531:~/learn/cpp_program#