1.实验名称:实验八 文件拷贝
2.实验时间:2008-6-13
3.实验目的:编程用面向对象设计思想分析、设计并实现实验八的内容。
4.实验内容:利用c++文件操作函数实现文件,将text1.txt中的内容拷贝到text2.txt中。
//源代码fcopy.cpp
#include <fstream.h>
void main()
{
char ch;
fstream text1,text2;//define inputs/outputs
text1.open("text1.txt",ios::in|ios::out);
text2.open("text2.txt",ios::in|ios::out);
while(!text1.eof())
{
text1.get(ch);
text2.put(ch);
}
text2.close();
text1.close();
}
/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
//实验结果
//文件text1.txt中的内容如下:
abcdefghijklmnopqrstuvwxyz
//文件text2.txt中的内容如下:
abcdefghijklmnopqrstuvwxyz
5.实验总结:用面向对象的思想和方法实现文件拷贝时,把两个文件看成是文件流类的对象。判断文件结束用到了文件流类中的eof()方法,用这种方法得到的结果多输出了一个字符。