首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++之资料操作,(不以简单而不为,不以难而畏为)

2012-08-02 
c++之文件操作,(不以简单而不为,不以难而畏为)操作相对简单,但不要以为简单而不动手,文件操作流在Java和C

c++之文件操作,(不以简单而不为,不以难而畏为)

操作相对简单,但不要以为简单而不动手,文件操作流在Java和C++都很重要,以下是雏形:(让您溅笑了)

文件写入操作

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
 ofstream file("c:\\hello.txt");
 if(!file){
  cout<<"can't open it"<<endl;
  return -1;
 }
 string s="hello";
 for(int i=0;i<10;++i){
  file<<"hello ";
 }
 file<<endl;

 file.close();
    system("pause");
 return 0;
}

c++之资料操作,(不以简单而不为,不以难而畏为)

c++之资料操作,(不以简单而不为,不以难而畏为)

 

文件输出操作:

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
 ifstream infile("c:\\world.txt");
 if(!infile){
  cout<<"can't open it"<<endl;
  return -1;
 }
 string s;
 while(getline(infile,s)){
  cout<<s<<endl;
 }

  infile.close();
 system("pause");
 return 0;
}

c++之资料操作,(不以简单而不为,不以难而畏为)

c++之资料操作,(不以简单而不为,不以难而畏为)

 

也可以用流迭代器来完成,欢迎看我的《IO流迭代器》,里面有讲解很清楚

 

不以易而不为,不以难而畏为!

热点排行