当前位置:主页 > 软件编程 > C代码 >

C++操作文件进行读取、删除、修改指定行

时间:2021-07-10 09:33:56 | 栏目:C代码 | 点击:

代码如下:

/********************************************************
Copyright (C), 2016-2018,
FileName: main
Author:     woniu201
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();
}

总结

您可能感兴趣的文章:

相关文章