C++文件流操作(耦合)

来源:互联网 发布:如何关闭qq游戏端口 编辑:程序博客网 时间:2024/04/30 14:01

// file-stream-coupling.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 //open file "example.dat" for reading and writing
 filebuf buffer;
 ostream output(&buffer);
 istream input(&buffer);
 buffer.open("example.dat", ios::in|ios::out|ios::trunc);
 for(int i=1;i<=4;i++){
  //write one line
  output<<i<<".line"<<endl;
 //print all file contents
  input.seekg(0);
  char c;
  while(input.get(c)){
   cout.put(c);
  }
  cout<<endl;
  input.clear();//clear eofbit and failbit
 }

 system("pause");

 return 0;
}

 

原创粉丝点击