在windows的系统下,采取一下命令将文件夹里面的所有jpg的绝对路径写在tem.set文件里面
dir *.jpg /b/s >tem.set
使用C++读取tem.set,但是windows的换行符号是\r\n,因此需要将其换掉,并添加‘\0’.
#include<iostream>
#include<io.h>
#include<vector>
#include<string>
using namespace std;
void main() {
vector<string> vecRes;
FILE*pfin = fopen("C:\\Users\\23913\\Desktop\\bottle_test_data\\JPEGImages\\tem.set", "rb");
char as8Buf[2048] = { 0 };
while (NULL != fgets(as8Buf, 2048, pfin)) {
if (as8Buf[strlen(as8Buf) - 1] == '\n') {
as8Buf[strlen(as8Buf) - 1] = '\0';
}
if (as8Buf[strlen(as8Buf)-1] == '\r'){
as8Buf[strlen(as8Buf) - 1] = '\0';
}
if (strlen(as8Buf) == 0) {
continue;
}
cout << (as8Buf) << endl;
vecRes.push_back(as8Buf);
}
fclose(pfin);
getchar();
}
可以通过如下的语句获取绝对路径中最后的文件名
int pos = strFullName.rfind("\\");
std::string fn = strFullName.substr(pos + 1, strFullName.length());
string中的rfind是反向查找第一个"\\"这里第一个\是转义字符,也就是找"\"然后将其所处的位置返回,然后在利用string的substr函数将该位置直至最后的子字符串返回。还可以循环调用上述的函数来获取上n层的文件夹的名称,但是需要注意好边界条件的判断,考虑到n的各种取值情况,做好if else的判断。
#include <string>
std::to_string(video_ID) // float转string
float curScore = atof(strScore.c_str()); // string转float
参考:https://www.cnblogs.com/rainsoul/p/6294343.html