目录
6、字符串的一些其他操作:(****)
6.5、字符串搜索: (**)
C++操作文件行(读取,删除,修改指定行)(****)
c++修改文件某行内容 (**)
c++读取整个txt文件三种方式
C++ File Handling: How to Open, Write, Read, Close Files in C++
c++如何修改文件中指定内容
C++修改指定字符串所在行内容
C++如何修改文件中的特定行 (***)
C++ 读、写、改、删除本地文件内容(***)
----------------------------------
参考:
Qt:Qfile与QTextStream读写文本文件 + Qt QFile /readLine()(****)
Qt:Qfile与QTextStream读写文本文件 + Qt QFile /readLine()(****)_ken2232的博客-CSDN博客
C++操作文件行(读取,删除,修改指定行) (****)
C++操作文件行(读取,删除,修改指定行) (****)_ken2232的博客-CSDN博客
Qt 之QDir文件目录拷贝、创建、删除 (***)
Qt 之QDir文件目录拷贝、创建、删除 (***)_ken2232的博客-CSDN博客
Qt之Qfile读取文件操作:类介绍
Qt之Qfile读取文件操作:类介绍_qfile读取文件的任意位置_ken2232的博客-CSDN博客
C++ 读取文件最后一行产生的问题
C++ 读取文件最后一行产生的问题_ken2232的博客-CSDN博客
================================
C++ 读、写、改、删除本地文件内容 (***)
3、修改本地文件:
例如我现在有一个本地文件,我想修改其中的某一行内容,我应该怎么做?在C++中,似乎没有对文本的行替换的功能,但是我们可以通过一个长字符串来解决这个问题:
思路如下:
新建一个空字符串,将文本文件一行行读取,不需要修改的就按原来的顺序保存到字符串中,
等读到需要修改的行的时候,将新的行保存到字符串中,原来的数据舍弃。
然后继续保存后面的内容,直到整个文本读取完成。
然后将文本清空,将新字符串保存进去。这样子就完成了某一行的修改。
代码如下:
void local_file_process::ModifyLoaclFile(vector<string> msg)
{
ifstream fin;
fin.open(file_path.c_str());
string linestr;
string strFileData = "";//暂存新的数据的地方
int file_line = 0;
int line = 1;
int line_2 = 1;
//查找需要修改的id是哪一个
while (getline(fin, linestr))
{
file_line ++;
if(linestr == msg[1])
break;
line++;
}
fin.close();
//如果id存在,这里应相等
if(file_line != line)
{//如果说目前的库位信息中没有这个id信息,新增一个新的库位信息
AddLoaclStorage(msg);
return;
}
ifstream in;
in.open(file_path.c_str());
//将需要修改的四行内容保存为新的msg。其他的行不变,暂存到strFileData
while (getline(in, linestr))
{
if(line_2 == line-1)
{
strFileData += msg[0];
strFileData += "\n";
}
else if(line_2 == line)
{
strFileData += msg[1];
strFileData += "\n";
}
else if(line_2 == line+1)
{
strFileData += msg[2];
strFileData += "\n";
}
else if(line_2 == line+2)
{
strFileData += msg[3];
strFileData += "\n";
}
else
{
strFileData += linestr;
strFileData += "\n";
}
line_2++;
}
in.close();
ofstream out;
out.open(file_path.c_str());
out.flush();//清空file内容
out<<strFileData;//写入修改后的数据
out.close();
}
上述代码实现的功能是:传入一个含有四个参数的容器:type、id、使用数量、总数量。首先根据传入的id判断当前需要修改的位置。如果文件中没有对应的数据则新增一条字段。如果有就开始操作文件,找到这四行代码所在的位置,用新的msg数据替换,其他数据保持不变,这样子就完成了一次对本地文件的修改。
5、涉及到的头文件:
#include "ros/ros.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdio>
#include "std_msgs/String.h"
#include <boost/algorithm/string.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
6、字符串的一些其他操作:(****)
6.1、字符串分割:
boost 库中提供了对字符串按照某个字符分割的函数:
string linestr;
vector<string> temp_sig;
boost::split(temp_sig, linestr, boost::is_any_of( ":" ), boost::token_compress_on );
第一个参数是分割后的字符串存储位置,一个vector类型容器;
第二个参数是需要被分割的字符串,string类型;
第三个参数是按照什么字符分割。
6.2、字符串转数字:
string temp = "123"
int numb = atoi(temp.c_str());
//或者
int numb = stoi(temp.c_str());
stoi()函数将字符串作为参数并返回其值。 atoi()函数将字符数组或字符串文字作为参数并返回其值。atoi()是旧的C样式函数。在C ++ 11中添加了stoi()。
stoi()最多可以包含三个参数,第二个参数用于起始索引,第三个参数用于输入数字的基数。
int stoi(const string&str , size_t* index = 0,int base = 10);
类似地,为了将String转换为Double,可以使用atof()。上面的函数返回转换后的整数作为int值。如果无法执行有效的转换,它将返回零。
6.3、数字转字符串:
int numb = 123;
string temp = to_string(numb);
to_string函数可以实现简单的将数字转字符串的操作。另外,itoa()也可以实现这个功能,但是没有to_string好用。
int n = 100;
char str2[10];
//字符串比较麻烦,所以转字符串三个参数,我是这么记得(手动滑稽)
itoa(n,str2,10); //第一个参数为整数,第二个为字符串(char*),第三个为进制
cout << str2 << endl;
6.4、字符串拼接:
拼接两个字符串,简单的“+”就可以了,非常方便:
int a = 123;
string b = "id:";
string result = b + to_string(a);
这样你就可以得到:
result = "id:123";
6.5、字符串搜索:
搜索某个字符串中是否含有某个子串或者字符:find函数,我们一般可以跟if之类的判断语句连用,实现某些条件:
string a = "today is a good day";
if(a.find("good")==string::npos)
{
//如果没有该字符
}
else
{
//如果有该字符
}
————————————————
版权声明:本文为CSDN博主「一叶执念」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/YiYeZhiNian/article/details/128394165
C++如何修改文件中的特定行 (***)
最近要处理一些数据集文件,转换为图像的格式,但是这种文件的格式并不是非常的确定,因此在处理的过程中遇到了种种bug,其中有一个bug的原因就是文件末尾的回车少了,我需要把这种文件的这些行分别加上回车。于是就有了这篇博客。下面来详细记录一下问题的解决方法。
一、问题描述
我需要处理的文件片段如下:
就是把含有<trace id="i">的这些行中,让数据单独占一行,让<trace id="i">和</trace>单独占一行。
。。。。。。。。。。省略。。。。。。。。。。。。。。。。。。。。。。
二、解决方案
感觉上面的需求很简单,实际上实现起来还挺麻烦的,首先我想找到这一行,然后找到<trace id="i">然后在他后面加回车符,然后写会文件,但是这样做会写在文件的末尾,C语言中的fseek函数虽然可以定位文件中的指针,让他在特定位置进行读写操作,但是又比较麻烦(实际上是我C语言几乎忘光了)。
出于懒得原因,我想到了一种简单的方法,
就是首先我按行读取原文件,判断该行是不是我要操作的,如果不是,则把他逐行写入一个新的临时文件中去,
如果是我要操作的行,那么我就先把<trace id="i">写入新的文件,然后写入换行符,再写入数据,在写入换行符,再写入</trace>。
最后写完临时文件以后,再读取临时文件,重新写入到原文件中,最终删除临时文件。
虽然这样表述着比较麻烦,但是这是一种懒人的做法,效率也不会很高,但是实现起来很便捷。
代码如下(Windows下实现):
void readAndWrite(const string& filepath)
{
fstream file(filepath);
string line;
int n, count = 0;
//create a temp file
ofstream outfile("1\\tmp.inkml", ios::out | ios::trunc);
//read the original file
while (!file.eof())
{
getline(file, line);//read one line
//judge the line just read is the line to process
if (strstr(line.c_str(), "<trace") && (strstr(line.c_str(), "id =") ||
strstr(line.c_str(), "id=")) && !strstr(line.c_str(), "traceGroup"))
{
//find the end of <trace id="i">
const char* t = strstr(line.c_str(), ">") + 1;
int i = 0;
//write <trace id="i"> to the temp file
while (line[i] != '>' && i<line.length())
{
outfile << line[i];
i++;
}
outfile << '>';
//write \n
outfile << endl;
int j = 0;
//write the data
while (*(t + j) != '<' && j<strlen(t))
{
outfile << *(t + j);
j++;
}
//write \n
outfile << endl;
//write </trace>
outfile << t + j << endl;
}
else
outfile << line << endl;;
}
outfile.close();
file.close();
ofstream outfile1(filepath, ios::out | ios::trunc);
fstream file1("1\\tmp.inkml");
//write the temp file to the original file
while (!file1.eof())
{
getline(file1, line);
outfile1 << line << endl;
}
outfile1.close();
file1.close();
//delete the temp file
system("del 1\\tmp.inkml");
}
————————————————
版权声明:本文为CSDN博主「lhanchao」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lhanchao/article/details/53064515
c++修改文件某行内容
/************************************************************************/
/* char*tostr 字符串转化str类型
输入:char * 字符串地址
无输出
返回值: str类型的字符串
*/
/************************************************************************/
string charToStr(char * contentChar)
{
string tempStr;
for (int i=0;contentChar[i]!='\0';i++)
{
tempStr+=contentChar[i];
}
return tempStr;
}
/************************************************************************/
/* 修改文件某行内容
输入:文件名 fileName 行号 lineNum ,修改的内容 content
输出:文件名 fileName
无返回值
tip:1,lineNum从第一行开始 2.content需要加上换行符
*/
/************************************************************************/
void modifyContentInFile(char *fileName,int lineNum,char *content)
{
ifstream in;
char line[1024]={'\0'};
in.open(fileName);
int i=0;
string tempStr;
while(in.getline(line,sizeof(line)))
{
i++;
if(lineNum!=i)
{
tempStr+=charToStr(line);
}
else
{
tempStr+=charToStr(content);
}
tempStr+='\n';
}
in.close();
ofstream out;
out.open(fileName);
out.flush();
out<<tempStr;
out.close();
}
c++如何修改文件中指定内容
c++如何修改文件中指定内容_c++更改文件内容_四月sky吖的博客-CSDN博客
C++修改指定字符串所在行内容
1.使用getline函数依次获取文件行
2.在文件行中查找指定字符串,不包含指定字符串的文件行保存在strFileData字符串中,包含指定字符串的文件行替换为想要的内容 ,再保存在strFileData字符串中
3.最后统一重新写入文件(使用ofstream打开文件默认会将文件清零)
————————————————
版权声明:本文为CSDN博主「杨真平」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43130406/article/details/128242435
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++读取整个txt文件三种方式
https://blog.csdn.net/vastz/article/details/109291852
C++ File Handling: How to Open, Write, Read, Close Files in C++
ByBarbara Thompson UpdatedMay 20, 2023
https://www.guru99.com/cpp-file-read-write-open.html
What is file handling in C++?
Files store data permanently in a storage device. With file handling, the output from a program can be stored in a file. Various operations can be performed on the data while in the file.
A stream is an abstraction of a device where input/output operations are performed. You can represent a stream as either a destination or a source of characters of indefinite length. This will be determined by their usage. C++ provides you with a library that comes with methods for file handling. Let us discuss it.
In this c++ tutorial, you will learn:
- What is file handling in C++?
- The fstream Library
- How to Open Files
- How to Close Files
- How to Write to Files
- How to Read from Files
The fstream Library
The fstream library provides C++ programmers with three classes for working with files. These classes include:
- ofstream– This class represents an output stream. It’s used for creating files and writing information to files.
- ifstream– This class represents an input stream. It’s used for reading information from data files.
- fstream– This class generally represents a file stream. It comes with ofstream/ifstream capabilities. This means it’s capable of creating files, writing to files, reading from data files.
The following image makes it simple to understand: