代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/********************************************************
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 ();
}
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/woniu211111/article/details/82257636