程序将自己的源代码写入记事本

这个例子利用了C++文件读写的功能,将main()函数输出到一个记事本中

#include<fstream>
#include<iostream>

using namespace std;

int main()
{
 fstream fin,fout;
 fin.open("main.cpp",ios::in);
 if(!fin)
 {
  cout<<"can't open file main.cpp !";
  return -1;
 }
 fout.open("main.txt",ios::out);
 if(!fout)
 {
  cout<<"can't open file main.txt !";
  return -1;
 }
 char ch;
 while(fin.get(ch))
  fout.put(ch);
 fin.close();
 fout.close();
 return true;
}


图片

你可能感兴趣的:(ios,c,File)