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

 

工作中经常用到对文件行的操作,下面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();
}

欢迎加群交流:C/C++开发交流

扫码关注公众号每天获取技术干货!扫码关注公众号每天获取技术干货!扫码关注公众号每天获取技术干货!

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农code之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值