c++对txt文件的读取与写入

#include <iostream>  
#include <iomanip>  
#include <fstream>  
  
using namespace std;  
  
int main(){  
char buffer[256];  
ifstream myfile ("c://a.txt");  
ofstream outfile("c://b.txt");  
  
if(!myfile){  
  cout << "Unable to open myfile";  
        exit(1); // terminate with error  
  
}  
if(!outfile){  
    cout << "Unable to open otfile";  
        exit(1); // terminate with error  
  
}  
int a,b;  
int i=0,j=0;  
int data[6][2];  
  while (! myfile.eof() )  
  {  
     myfile.getline (buffer,10);  
    sscanf(buffer,"%d %d",&a,&b);  
    cout<<a<<" "<<b<<endl;  
     data[i][0]=a;  
     data[i][1]=b;  
     i++;  
  }  
myfile.close();  
  for(int k=0;k<i;k++){  
      outfile<<data[k][0] <<" "<<data[k][1]<<endl;  
     cout<<data[k][0] <<" "<<data[k][1]<<endl;  
  }  
  
outfile.close();  
return 0;  
}  
  
  
  
  
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dragonworrior/archive/2009/11/02/4759484.aspx  

 

posted on 2014-11-22 01:00  星空夜夏梦  阅读(580)  评论(0编辑  收藏  举报

导航