C++读取文件

在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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值