C++文件操作

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include<fstream>
#include<sstream>
#include<bitset> 
#include<string>
#include<vector>

#include<exception>

#include<cassert>
using namespace std;


int main(int argc, char *argv[])
{
    //bitset<32> bitvec(0xffff);
    /*{
    string str;
    getline(cin,str);
    bitset<32> bitvec1(str);
    int pp=bitvec1.count();
    cout<<pp<<endl;
    unsigned long val=bitvec1.to_ulong();
    cout<<val<<endl;
    cout<<bitvec1<<endl;
*/

     
  /* const int i=0, *p=i;
   cout<<p<<endl;
    */
  /*  char str[]={'s','h','e'};
    //char *str="she";
    size_t len=strlen(str);
    string ss=str;
    //char s[]="";
  cout<<ss<<endl;
    //cout<<strcpy(s,str)<<endl;
    
    */
 /*   const size_t len=5;
    int arr[]={1,2,3,2,1};
    vector<int> v(arr,arr+len);
   */
 /*  unsigned char bits=0xff;
   cout<<(bits>>5)<<endl;
   cout<<sizeof(bits)<<endl;
   */
   //int *a=new int[2]();
   
  /* int a[]={1,2,3};
   
   cout<<typeid(int).name<<endl; 
   //delete []a;
    */
 /*   double d=3.14;
    void *p=&d;
    double *dp=static_cast<double*>(p);
     
  // delete p;
  //   p=0;
   //  delete dp;
    dp=0;
   */
/*  try
  {
         int *p=new int[1000000]; 
         }
  catch(exception &e)
  {
                  cout<<e.what()<<endl;
                  }
     
*/
   
  /* int i;
   cin>>i;
   assert(i!=0);
   cout<<i<<endl; 
   
    */
    //写流 
    ofstream fs("d:\\demo.txt");
    if(!fs)
    //流未打开,返回值1 
    return 1;
    //往流中写入数据 
    fs<<setw(20)<<"name:   "<<"frank"<<endl;
    fs<<setw(20)<<"tel:   "<<"188"<<endl; 
    //关闭流 
    fs.close();
    
    //读流 
/*
读取文件流----字符串中(所有内容),-----字符串流----输出每个字符串 
*/ 
   // ifstream fi("d:\\demo.txt");
   //创建文件读入流 
    ifstream fi;
    //流打开文件 
    fi.open("d:\\demo.txt");
    //创建字符串 
    string s,word;
    //把读取的写流写到字符串s中 
   while( getline(fi,s)) 
    {
          //cout<<s<<endl;
          //创建字符串读入流,并把字符串s搞进字符串流str中 
          istringstream str(s); 
          //读取字符串中的每个字符,以空格分开的 
          while(str>>word)
         { cout<<word<<endl;}
}
//关闭流 
    fi.close(); 
    system("PAUSE");
    return EXIT_SUCCESS;
}

 

posted @ 2013-05-23 17:03  夜雨阑珊  阅读(237)  评论(0编辑  收藏  举报