目录
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的博客-程序员宅基地
C++操作文件行(读取,删除,修改指定行) (****)
C++操作文件行(读取,删除,修改指定行) (****)_ken2232的博客-程序员宅基地
Qt 之QDir文件目录拷贝、创建、删除 (***)
Qt 之QDir文件目录拷贝、创建、删除 (***)_ken2232的博客-程序员宅基地
Qt之Qfile读取文件操作:类介绍
Qt之Qfile读取文件操作:类介绍_qfile读取文件的任意位置_ken2232的博客-程序员宅基地
C++ 读取文件最后一行产生的问题
C++ 读取文件最后一行产生的问题_ken2232的博客-程序员宅基地
================================
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数据替换,其他数据保持不变,这样子就完成了一次对本地文件的修改。
#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>
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;
拼接两个字符串,简单的“+”就可以了,非常方便:
int a = 123;
string b = "id:";
string result = b + to_string(a);
这样你就可以得到:
result = "id:123";
搜索某个字符串中是否含有某个子串或者字符: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
最近要处理一些数据集文件,转换为图像的格式,但是这种文件的格式并不是非常的确定,因此在处理的过程中遇到了种种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
/************************************************************************/
/* 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++更改文件内容_四月sky吖的博客-程序员宅基地
1.使用getline函数依次获取文件行
2.在文件行中查找指定字符串,不包含指定字符串的文件行保存在strFileData字符串中,包含指定字符串的文件行替换为想要的内容 ,再保存在strFileData字符串中
3.最后统一重新写入文件(使用ofstream打开文件默认会将文件清零)
————————————————
版权声明:本文为CSDN博主「杨真平」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43130406/article/details/128242435
工作中经常用到对文件行的操作,下面C++代码实现了通过行号读取指定的行数据,删除指定行数据,对指定行数据进行修改。复制过去可直接使用。
/********************************************************
Copyright (C), 2016-2018,
FileName: main
Author: woniu201
Email: [email protected]
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/vastz/article/details/109291852
ByBarbara Thompson UpdatedMay 20, 2023
https://www.guru99.com/cpp-file-read-write-open.html
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:
The fstream library provides C++ programmers with three classes for working with files. These classes include:
The following image makes it simple to understand:
文章浏览阅读2.5w次,点赞6次,收藏50次。官方解释是,docker 容器是机器上的沙盒进程,它与主机上的所有其他进程隔离。所以容器只是操作系统中被隔离开来的一个进程,所谓的容器化,其实也只是对操作系统进行欺骗的一种语法糖。_docker菜鸟教程
文章浏览阅读5.7k次,点赞3次,收藏14次。该如何避免的,今天小编给大家推荐两个下载Windows系统官方软件的资源网站,可以杜绝软件捆绑等行为。该站提供了丰富的Windows官方技术资源,比较重要的有MSDN技术资源文档库、官方工具和资源、应用程序、开发人员工具(Visual Studio 、SQLServer等等)、系统镜像、设计人员工具等。总的来说,这两个都是非常优秀的Windows系统镜像资源站,提供了丰富的Windows系统镜像资源,并且保证了资源的纯净和安全性,有需要的朋友可以去了解一下。这个非常实用的资源网站的创建者是国内的一个网友。_msdn我告诉你
文章浏览阅读1.2k次。vue2封装对话框el-dialog组件_
文章浏览阅读4.7k次,点赞5次,收藏6次。MFC 文本框换行 标签: it mfc 文本框1.将Multiline属性设置为True2.换行是使用"\r\n" (宽字符串为L"\r\n")3.如果需要编辑并且按Enter键换行,还要将 Want Return 设置为 True4.如果需要垂直滚动条的话将Vertical Scroll属性设置为True,需要水平滚动条的话将Horizontal Scroll属性设_c++ mfc同一框内输入二行怎么换行
文章浏览阅读832次。检查Linux是否是否开启所需端口,默认为6379,若未打开,将其开启:以root用户执行iptables -I INPUT -p tcp --dport 6379 -j ACCEPT如果还是未能解决,修改redis.conf,修改主机地址:bind 192.168.85.**;然后使用该配置文件,重新启动Redis服务./redis-server redis.conf..._redis-server doesn't support auth command or ismisconfigured. try
文章浏览阅读4.9k次。济大数电实验报告_数据选择器及其应用
文章浏览阅读236次。1研究内容消费在生产中占据十分重要的地位,是生产的最终目的和动力,是保持省内经济稳定快速发展的核心要素。预测河南省社会消费品零售总额,是进行宏观经济调控和消费体制改变创新的基础,是河南省内人民对美好的全面和谐社会的追求的要求,保持河南省经济稳定和可持续发展具有重要意义。本文建立灰色预测模型,利用MATLAB软件,预测出2019年~2023年河南省社会消费品零售总额预测值分别为21881...._灰色预测模型用什么软件
文章浏览阅读1.2k次。12.4-在Qt中使用Log4Qt输出Log文件,看这一篇就足够了一、为啥要使用第三方Log库,而不用平台自带的Log库二、Log4j系列库的功能介绍与基本概念三、Log4Qt库的基本介绍四、将Log4qt组装成为一个单独模块五、使用配置文件的方式配置Log4Qt六、使用代码的方式配置Log4Qt七、在Qt工程中引入Log4Qt库模块的方法八、获取示例中的源代码一、为啥要使用第三方Log库,而不用平台自带的Log库首先要说明的是,在平时开发和调试中开发平台自带的“打印输出”已经足够了。但_log4qt
文章浏览阅读786次。全局观思维模型,一个教我们由点到线,由线到面,再由面到体,不断的放大格局去思考问题的思维模型。_计算机中对于全局观的
文章浏览阅读330次。一、CountDownLatch介绍CountDownLatch采用减法计算;是一个同步辅助工具类和CyclicBarrier类功能类似,允许一个或多个线程等待,直到在其他线程中执行的一组操作完成。二、CountDownLatch俩种应用场景: 场景一:所有线程在等待开始信号(startSignal.await()),主流程发出开始信号通知,既执行startSignal.countDown()方法后;所有线程才开始执行;每个线程执行完发出做完信号,既执行do..._countdownluach于cyclicbarrier的用法
文章浏览阅读508次。Prometheus 算是一个全能型选手,原生支持容器监控,当然监控传统应用也不是吃干饭的,所以就是容器和非容器他都支持,所有的监控系统都具备这个流程,_-自动化监控系统prometheus&grafana实战
文章浏览阅读4.7k次。输入关键字,可以通过键盘的搜索按钮完成搜索功能。_react search