欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

C++简单文件操作

发布时间:2025/7/25 c/c++ 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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;
}

转载于:https://www.cnblogs.com/clc2008/archive/2012/03/19/2405386.html

总结

以上是生活随笔为你收集整理的C++简单文件操作的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。