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  

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。