C++操作文件行(读取,删除,修改指定行)

180it 2020-10-13 AM 1084℃ 0条

工作中经常用到对文件行的操作,下面C++代码实现了通过行号读取指定的行数据,删除指定行数据,对指定行数据进行修改。复制过去可直接使用。

/********************************************************
Copyright (C),  2016-2018,
FileName:        main
Author:            woniu201
Email:             wangpengfei.201@163.com
Created:           2018/08/31
Description:    文件操作:读取指定行,删除指定行,修改指定行
********************************************************/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
 
/************************************
@ Brief:        读取指定行数据
@ Author:        woniu201 
@ Created:        2018/08/31
@ Return:            
************************************/
void ReadLineData(char* fileName, int lineNum, char* data)
{
    ifstream in;
    in.open(fileName);
 
    int line = 1;
    while (in.getline(data, 1024))
    {
        if (lineNum == line)
        {
            break;
        }
        line ++ ;
    }
 
    in.close();
}
 
/************************************
@ Brief:        字符串转string类型
@ Author:        woniu201 
@ Created:        2018/08/31
@ Return:            
************************************/
string CharToStr(char * contentChar)
{
    string tempStr;
    for (int i=0;contentChar[i]!='\0';i++)
    {
        tempStr+=contentChar[i];
    }
    return tempStr;
}
 
 
/************************************
@ Brief:        删除指定行
@ Author:        woniu201 
@ Created:        2018/08/31
@ Return:           
************************************/
void DelLineData(char* fileName, int lineNum)
{
    ifstream in;
    in.open(fileName);
    
    string strFileData = "";
    int line = 1;
    char lineData[1024] = {0};
    while(in.getline(lineData, sizeof(lineData)))
    {
        if (line == lineNum)
        {
            strFileData += "\n";
        }
        else
        {
            strFileData += CharToStr(lineData);
            strFileData += "\n";
        }
        line++;
    }
    in.close();
 
    //写入文件
    ofstream out;
    out.open(fileName);
    out.flush();
    out<<strFileData;
    out.close();
}
 
/************************************
@ Brief:        修改行数据
@ Author:        woniu201 
@ Created:        2018/08/31
@ Return:            
************************************/
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 += CharToStr(lineData);
            strFileData += "\n";
        }
        else
        {
            strFileData += CharToStr(tmpLineData);
            strFileData += "\n";
        }
        line++;
    }
    in.close();
 
    //写入文件
    ofstream out;
    out.open(fileName);
    out.flush();
    out<<strFileData;
    out.close();
}
 
int main()
{
    char lineData[1024] = {0};
    ReadLineData("D:\\project\\cpp\\2010\\jsondemo\\jsondemo\\1.json", 21, lineData);
    cout << lineData << endl;
 
    DelLineData("D:\\project\\cpp\\2010\\jsondemo\\jsondemo\\1.json", 10);
 
    ModifyLineData("D:\\project\\cpp\\2010\\jsondemo\\jsondemo\\1.json", 10, "aaaaaaaaaaaaaa");
    getchar();
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

C++操作文件行(读取,删除,修改指定行)