1 批量图片重新命名
代码语言:javascript复制#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
int main()
{
cv::String path="/home/lyy/from_0_to_1_for_slam/homework_1/data/";//待处理图片路径
cv::String dest="/home/lyy/from_0_to_1_for_slam/homework_1/dst/";//保存处理后的图片路径
cv::String savefilename;
vector<cv::String> filenames;
Mat srcImg,dstImg;
cv::glob(path,filenames);//glob 寻找与模式匹配的文件路径
for(int i=0;i<filenames.size(); i)
{
srcImg=cv::imread(filenames[i]);
srcImg.copyTo(dstImg);
if(i<10)
{
savefilename=dest "000" to_string(i) ".png";
cv::imwrite(savefilename,dstImg);
}
if(i>=10&&i<99)
{
savefilename=dest "00" to_string(i) ".png";
cv::imwrite(savefilename,dstImg);
}
if(i>=100&&i<999)
{
savefilename=dest "0" to_string(i) ".png";
cv::imwrite(savefilename,dstImg);
}
if(i>=1000&&i<9999)
{
savefilename=dest to_string(i) ".png";
cv::imwrite(savefilename,dstImg);
}
}
return 0;
}
2. 查找文件中批量数据
代码语言:javascript复制#include <fstream>
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
vector<pair<int,string>> data;
void read_txt(string input_file)
{
ifstream file_open;
file_open.open(input_file.c_str());
if(!file_open.is_open())
{
cout<<"cannot open file"<<endl;
exit(1);
}
std::string file_line;
int num;
int people;
string city;
string note;
bool not_first_line=false;
bool first_line=true;
while(std::getline(file_open,file_line) && !file_line.empty())
{
//第一行为标题的情况下,且各列之间是空格分隔符
if(first_line)
{
not_first_line=true;
first_line=false;
}
else if(not_first_line)
{
std::istringstream data_(file_line);
data_>>num>>people>>city>>note;
cout<<num<<" "<<people<<" "<<city<<" "<<note<<endl;;
pair<int,string> _data=std::make_pair(people,city);
data.push_back(_data);
}
}
file_open.close();
for(int i=0;i<data.size();i )
{
if(data[i].first > 600)
cout<<"人流量大于600的城市:"<<data[i].second<<endl;
}
}
int main()
{
string input="./1.txt";
read_txt(input);
return 0;
}
3. 在文件首行添加列名称
代码语言:javascript复制#include <iostream>
#include <fstream>
using namespace std;
//在第一行插入各列的名字
int main()
{
ifstream ifs("1.txt");
ofstream ofs("output.txt");
//ofs<<"x1ty1tx2ty2tth"<<endl;
ofs<<"序号t人流量t城市t其他t"<<endl;
char str[1024];
while(ifs.getline(str, 1024)){
ofs<<str<<endl;
}
ifs.close();
ofs.close();
return 0;
}
4. 修改文件中特定变量
代码语言:javascript复制#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;
struct Data
{
char name[20];
int people;
};
int main()
{
FILE *fp=fopen("./2.txt","r ");
Data data={"宁波",200};//修改目标
char name[100];
int people;
while(fscanf(fp,"%s%d",name,&people)!=EOF)
{
if(strcmp(name,data.name)==0 && people==data.people)//找到目标
{
fseek(fp,-3,SEEK_CUR);//回滚指针到200前
cout<<"Please input new people"<<endl;
cin>>people;
fprintf(fp,"%d",people);//覆盖写入
break;
}
}
fclose(fp);
return 0;
}
博主:菜鸟程序员
初衷:学习资料,程序设计,视觉算法,求职经验,工作心得