博客
关于我
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/

    你可能感兴趣的文章
    oracle tirger_在Oracle中,临时表和全局临时表有什么区别?
    查看>>
    Oracle Validated Configurations 安装使用 说明
    查看>>
    oracle where 条件的执行顺序分析1
    查看>>
    oracle 中的 CONCAT,substring ,MINUS 用法
    查看>>
    Oracle 中的 decode
    查看>>
    oracle 中表一对多取多方的最新的一条数据
    查看>>
    oracle 使用 PL/SQL Developer创建表并插入单条、多条数据
    查看>>
    oracle 使用leading, use_nl, rownum调优
    查看>>
    oracle 修改字段类型方法
    查看>>
    Oracle 修改数据库表数据提交之后进行回滚
    查看>>
    UML-总结
    查看>>
    oracle 内存参数示意图
    查看>>
    Oracle 写存储过程的一个模板还有一些基本的知识点
    查看>>
    UML- 配置图(部署图)
    查看>>
    oracle 切割字符串加引号_使用Clean() 去掉由函数自动生成的字符串中的双引号...
    查看>>
    Oracle 创建 DBLink 的方法
    查看>>
    oracle 创建job
    查看>>
    oracle 创建一个用户,只能访问指定的对象
    查看>>
    oracle 创建双向备份,Materialized View 物化视图实现 Oracle 表双向同步
    查看>>
    oracle 创建字段自增长——两种实现方式汇总
    查看>>