zoukankan      html  css  js  c++  java
  • fstream之seekp/seekg/ios::ate/ios::app

    在程序开发中,IO处理无处不在,经常会在代码中遇到特殊的IO处理需求

    1、描述

    需求:如果文件不存在则创建,存在则打开,然后先读取文件的末行,然后在文件末尾写入。

    代码:

    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char **argv)
    {
        fstream fst;
        string strTemp;
        fst.open("test3", ios::in | ios::out);
        //fst.seekp(0, fstream::beg);
        while (!fst.eof())
        {
            getline(fst, strTemp);
            cout << strTemp << endl;
        }
        cout << strTemp;
    
        fst.seekg(0, fstream::end);
        fst << "
    hhfdsfds";
        fst.close();
        return 0;
    }

    因为fstream对象是作为读写同时打开的,在读和写转换之间,会使流失效,这个具体还要去参考《标准C++输入输出流与本地化》这本书。

    2、ios::ate/ios::app的区别

    ios::ate是使文件打开时将文件读取位置移动到文件尾

    ios::app是打开文件并在文件尾部添加数据

    3、seekp/seekg的区别

    seekp是指设置输入流的文件读取位置,对应读取输入流的文件读取位置为tellp

    seekg是指设置输出流的文件插入位置,对应读取输出流的文件插入位置为tellg

  • 相关阅读:
    GridView 内部添加控件
    TreeList获取选中内容
    TreeList简介
    TreeList
    DEV—【GridControl 按钮列无法触发点击事件解决方案】
    dev 多行文本 MemoEdit
    DevExpress控件使用小结
    DEV常用设置
    DEV常用设置
    documentManager1注意事项
  • 原文地址:https://www.cnblogs.com/lit10050528/p/4182277.html
Copyright © 2011-2022 走看看