C杂谈
2024-08-30 08:00:38
最近在做关于C的项目开发,记录一下有关C的操作,比较杂乱
1.利用System进行文件数量统计:
1)
system("dir /b /s /ad d:\\mydir\\*.* | find /c \":\" >d:\\ndirs.txt");
//读文件d:\\ndirs.txt的内容即d:\\mydir目录下的文件夹数
system("dir /b /s /a-d d:\\mydir\\*.* | find /c \":\" >d:\\nfiles.txt");
//读文件d:\\nfiles.txt的内容即d:\\mydir目录下的文件数
顺带也写一下读取文件以及将Char类型字符转化为Int类型代码
ifstream in;
string str;
in.open("D:\\nfiles.txt");
if (!in.is_open()){
cout << "Error opening file"; exit();
}
else{
std::copy(std::istream_iterator<unsigned char>(in),std::istream_iterator<unsigned char>(), back_inserter(str));
}
filecount = atoi(str.c_str());
2)
HANDLE hFind;
WIN32_FIND_DATA dataFind;
BOOL bMoreFiles = TRUE; //m_strDir就是你要指定的路径
hFind = FindFirstFile(s2ws(add + "\*.*").c_str(), &dataFind);//找到路径中所有文件 //遍历路径中所有文件
while (hFind != INVALID_HANDLE_VALUE&&bMoreFiles == TRUE)
{
if (dataFind.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)//判断是否是文件
{
filecount++;
}
bMoreFiles = FindNextFile(hFind, &dataFind);
}
FindClose(hFind);
这种方法个人使用存在BUG,当文件数量大于1时,filevount不会继续增加,后续看到更改方法继续改进。
2.string转LPCWSTR(C文件拷贝)
wstring s2ws(const std::string& s) {
int len;
int slength = (int)s.length() + ;
len = MultiByteToWideChar(CP_ACP, , s.c_str(), slength, , );
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, , s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
} CopyFile(s2ws(source).c_str(), s2ws(des).c_str(), FALSE);//false代表覆盖,true不覆盖
3.C语言遍历文件夹下文件
long hFile = ;
struct _finddata_t fileInfo;
string pathName, exdName; if ((hFile = _findfirst(pathName.assign(path).append("\\*").c_str(), &fileInfo)) == -) {
return;
7 }
8 do {
printf("%s\n", path.c_str());
printf("%s\n", fileInfo.name);
Mat library_img = imread(path + "/" + fileInfo.name);
} while (_findnext(hFile, &fileInfo) == );
13 _findclose(hFile);
最新文章
- insert、update select from
- ORACLE实验一-三
- JSP动作元素之include
- jQuery和AngularJS的区别小分析
- JavaScript获取一段html片段中a标签的href值
- [转]反向ajax项目
- call,apply,bind函数
- Webservice初接触
- ACM2050
- hdu 4778 Gems Fight! 博弈+状态dp+搜索
- PHP Java
- CSS——图片替换方法比较
- 循环单词 java
- tensorflow安装篇
- golang 日期时间处理
- 潭州课堂25班:Ph201805201 第十五课 迭代器,生成器 (课堂笔记)
- block-chain
- [2016北京集训测试赛17]crash的游戏-[组合数+斯特林数+拉格朗日插值]
- vue子组件的自定义事件
- hadoop配置文件的参数含义说明
热门文章
- Leetcode题解(4):L216/Combination Sum III
- vmware克隆一台机器后修改etho
- vim 参数文件配置
- Oracle 一行拆分为多行
- JAVA虚拟机、Dalvik虚拟机和ART虚拟机简要对照
- oracle 的sys 和 system 账号
- select version();desc mysql.user;
- Android笔记之manifestPlaceholders
- BAPI_PO_CREATE1 创建PO ch_memory_complete = ‘x&#39;,导致hold on 解决方案,
- Android Touch事件分发