C++简单文件操作

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

int _tmain(int argc, _TCHAR* argv[])
{
    //文件写入操作
    /*int s=0;    
    ofstream fileout("a.doc");
    if(!fileout){
        cout<<"文件打开失败"<<endl;
    }
    while(s<=10){
        fileout<<s<<"+1="<<++s<<endl;
    }
    cout<<"写入成功"<<endl;
    fileout.close();
*/

    char ch;
    ofstream f2;
    f2.open("D:\\data2.txt");//默认ios::out 不存在则创建
    if(!f2){
        cerr<<"错误"<<endl;
        exit(1);
    }
    while((ch=cin.get())!=EOF)//Ctrl+Z 组合 代表文件结束符EOF
    {
        f2<<ch;        
        //f2.put(ch);
    }
    f2.close();
    int a;
    cin>>a;
    return 0;
}
posted @ 2012-03-19 00:19  lance2008  阅读(154)  评论(0编辑  收藏  举报