
在C++中,文件的输入与输出非常重要,现在介绍如何简单把变量数据写入到磁盘的文件,使用了ofstream这个类。
使用两个头文件:
#include
#include
//利用ofstream类的构造函数创建一个文件输出流对象来打开文件
ofstream fout( "d:\\mytest.txt" );
// 输出到磁盘文件
fout << "Learning C++ is very useful."<< endl;
//关闭文件输出流
cout << "文件能打开" < 屏幕ofstream能磁盘打开文件 成功的字符串写入了d盘的文件夹中了 代码如下,方便粘贴: #include #include using namespace std; int main() { //利用ofstream类的构造函数创建一个文件输出流对象来打开文件 ofstream fout( "d:\\mytest.txt" ); if ( ! fout) { cout << "文件不能打开" < } else { // 输出到磁盘文件 fout << "Learning C++ is very useful."<< endl; //关闭文件输出流 cout << "文件能打开" < } fout.close(); }
