博客
关于我
C++研发 核心篇 第五讲 【文件操作】
阅读量:67 次
发布时间:2019-02-25

本文共 2564 字,大约阅读时间需要 8 分钟。

5 ????

??????????????????????????????????????????????????????????????????C++????????????? <fstream>?

???????????

  • ???????????ASCII???????????
  • ??????????????????????????????????????
  • ????????

    ????????????

  • ofstream??????????
  • ifstream??????????
  • fstream??????????
  • 5.1 ??????

    5.1.1 ???

    ?????????

  • ??????#include <fstream>?
  • ??????ofstream ofs;?
  • ?????ofs.open("????", ????);?
  • ?????ofs << "?????";?
  • ?????ofs.close();?
  • ???????????????????????

    ???? ??
    ios::in ?????
    ios::out ?????
    ios::ate ?????????
    ios::app ???????
    ios::trunc ?????????????????
    ios::binary ???????????

    ???

    #include 
    #include
    void test01() { ofstream ofs("test.txt", ios::out); ofs << "?????" << endl; ofs << "????" << endl; ofs << "???18" << endl; ofs.close();}int main() { test01(); system("pause"); return 0;}

    5.1.2 ???

    ?????????

  • ??????#include <fstream>?
  • ??????ifstream ifs;?
  • ?????ifs.open("????", ????);?
  • ??????????????????
  • ?????ifs.close();?
  • ???

    #include 
    #include
    void test01() { ifstream ifs("test.txt", ios::in); if (!ifs.is_open()) { cout << "??????" << endl; return; } // ???????????? char buf[1024] = {0}; while (ifs >> buf) { cout << buf << endl; } // ???? char buf[1024] = {0}; while (ifs.getline(buf, sizeof(buf))) { cout << buf << endl; } // ????????? string buf; while (getline(ifs, buf)) { cout << buf << endl; } char c; while ((c = ifs.get()) != EOF) { cout << c; } ifs.close();}int main() { test01(); system("pause"); return 0;}

    5.2 ???????

    5.2.1 ???

    ??????????????????write???????????

    ostream& write(const char* buffer, int len);

    ???

    #include 
    #include
    struct Person { char m_Name[64]; int m_Age;};void test01() { ofstream ofs("person.txt", ios::out | ios::binary); Person p = {"??", 18}; ofs.write((const char*)&p, sizeof(p)); ofs.close();}int main() { test01(); system("pause"); return 0;}

    5.2.2 ???

    ??????????????????read???????????

    istream& read(char* buffer, int len);

    ???

    #include 
    #include
    struct Person { char m_Name[64]; int m_Age;};void test01() { ifstream ifs("person.txt", ios::in | ios::binary); if (!ifs.is_open()) { cout << "??????" << endl; return; } Person p; ifs.read((char*)&p, sizeof(p)); cout << "??? " << p.m_Name << " ??? " << p.m_Age << endl; ifs.close();}int main() { test01(); system("pause"); return 0;}

    ?????????????????????????????????

    转载地址:http://bke.baihongyu.com/

    你可能感兴趣的文章
    OpenStack创建虚拟机实例实战
    查看>>
    OpenStack安装部署实战
    查看>>
    OpenStack实践系列⑨云硬盘服务Cinder
    查看>>
    OpenStack架构
    查看>>
    OpenStack版本升级与故障排查实战
    查看>>
    Openstack的HA解决方案【替换原有的dashboard】
    查看>>
    OpenStack的基本概念与架构详解
    查看>>
    Openstack的视频学习
    查看>>
    OpenStack自动化安装部署实战(附OpenStack实验环境)
    查看>>
    openstack虚拟机迁移live-migration中libvirt配置
    查看>>
    OpenStack项目管理实战
    查看>>
    OpenStreetMap初探(一)——了解OpenStreetMap
    查看>>
    openSUSE 13.1 Milestone 2 发布
    查看>>
    openSUSE推出独立 GUI 包管理工具:YQPkg,简化了整个软件包管理流程
    查看>>
    OpenVP共用账号 一个账号多台电脑登录
    查看>>
    OpenVSwtich(OVS)Vlan间路由实战 附实验环境
    查看>>
    Openwrt LuCI模块练习详细步骤
    查看>>
    openwrt_git_pull命令提示merger冲突时如何解决?
    查看>>
    OpenWrt包管理软件opkg的使用(极路由)
    查看>>
    OpenWrt固件编译刷机完全总结
    查看>>