zoukankan      html  css  js  c++  java
  • C++ 文件操作之修改文件某一行数据

    C++ 文件操作之修改文件某一行数据

    void ModifyLineData(char* fileName, int lineNum, char* lineData)
    {
      ifstream in;
      in.open(fileName);
      string strFileData = "";
      int line = 1;
      char tmpLineData[1024] = {0};
      while(in.getline(tmpLineData, sizeof(tmpLineData)))
      {
        if (line == lineNum)
        {
          strFileData += string(lineData);
          strFileData += "\n";
        }
        else
        {
          strFileData += string(tmpLineData);
          strFileData += "\n";
        }
        line++;
      }
      in.close();
      //写入文件
      ofstream out;
      out.open(fileName);
      out.flush();
      out<<strFileData;
      out.close();
    }
    void main()
    {
      ModifyLineData("./123456.txt", 2, "国服老虎在线收徒!");
    }

  • 相关阅读:
    poj 3422 Kaka's Matrix Travels
    poj 1815 Friendship
    poj 1966 Cable TV Network
    黑暗
    【bzoj2741】[FOTILE模拟赛] L
    整数拆分
    LCIS
    原题的旅行
    【codeforces gym】Increasing Costs
    【noip模拟】D(==)
  • 原文地址:https://www.cnblogs.com/xiaohai123/p/13663026.html
Copyright © 2011-2022 走看看