读写&复制实例
下面的 C++ 程序以读写模式打开一个文件。
file_wr() 在向文件 test.txt 写入用户输入的信息之后,程序从文件读取信息,并将其输出到屏幕上;
file_copy()将文件test.txt里的数据读取出来后,再写入test_1.txt中。
#include "iostream"
#include "fstream"
using namespace std;
//向文件内部写入数据,并将数据读出
void file_wr(void)
{
char data[100];
//向文件写入数据
ofstream outfile;
outfile.open("test.txt");
cout << "Write to the file" << endl;
cout << "Enter your name:" << endl;
cin.getline(data, 100);
outfile << data << endl;
cout << "Enter your age:" << endl;
cin >> data;
cin.ignore();
outfile << data << endl;
outfile.close();
//从文件读取数据
ifstream infile;
infile.open("test.txt");
cout << "Read from the file" << endl;
infile >> data;
cout << data << endl;
infile >> data;
cout << data << endl;
infile.close();
}
//将数据从一文件复制到另一文件中
void file_copy(void)
{
char data[100];
ifstream infile;
ofstream outfile;
infile.open("test.txt");
outfile.open("test_1.txt");
cout << "copy from test.txt to test_1.txt" << endl;
while (!infile.eof())
{
infile >> data;
cout << data << endl;
outfile << data << endl;
}
infile.close();
outfile.close();
}
//测试上述读写文件,与文件数据复制
int _tmain(int argc, _TCHAR* argv[])
{
file_wr();
file_copy();
return 0;
}
当上面的代码被编译和执行时,它会产生下列输入和输出:
$./a.out
Writing to the file
Enter your name:
John
Enter your age:
20
Reading from the file
John
20
copy from test.txt to test_1.txt
John
20
xiaoke
xiaoke
lkj***9@163.com4年前 (2017-11-02)