2.3.7 文件类型(3)
运行结果如图2.7所示。
试一试:根据以上程序,使用fstream数据流读取qq.txt文件中的数据。
前面简单介绍了对文件的读写操作,如果文件已经没有用了,也可以通过程序将文件删除。删除文件需要调用remove函数,remove函数调用成功返回0,否则返回非0值。在调用函数之前,需要引入头文件iomanip。
【例2.8】 使用remove函数删除文件,程序代码如下。(实例位置:光盘\mr\example\第2章\2.8)
代码位置:光盘\mr\example\第2章\2.8\fremove\fremove.cpp
- 01 #include "iostream.h"
- 02 #include "iomanip"
- 03 int main()
- 04 {
- 05 char file[50];
- 06 cout <<"输入文件名:"<<"\n";
- 07 cin >>file;
- 08 if(!remove(file))
- 09 {
- 10 cout <<"文件:"<<file<<"以删除"<<"\n";
- 11 }
- 12 else
- 13 {
- 14 cout <<"文件:"<<file<<"删除失败"<<"\n";
- 15 }
- 16 }
运行结果如图2.8所示。
试一试:在工程文件的目录下,创建一个文件a.txt,然后根据以上程序,删除这个文件。