最近在做关于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);

最新文章

  1. insert、update select from
  2. ORACLE实验一-三
  3. JSP动作元素之include
  4. jQuery和AngularJS的区别小分析
  5. JavaScript获取一段html片段中a标签的href值
  6. [转]反向ajax项目
  7. call,apply,bind函数
  8. Webservice初接触
  9. ACM2050
  10. hdu 4778 Gems Fight! 博弈+状态dp+搜索
  11. PHP Java
  12. CSS——图片替换方法比较
  13. 循环单词 java
  14. tensorflow安装篇
  15. golang 日期时间处理
  16. 潭州课堂25班:Ph201805201 第十五课 迭代器,生成器 (课堂笔记)
  17. block-chain
  18. [2016北京集训测试赛17]crash的游戏-[组合数+斯特林数+拉格朗日插值]
  19. vue子组件的自定义事件
  20. hadoop配置文件的参数含义说明

热门文章

  1. Leetcode题解(4):L216/Combination Sum III
  2. vmware克隆一台机器后修改etho
  3. vim 参数文件配置
  4. Oracle 一行拆分为多行
  5. JAVA虚拟机、Dalvik虚拟机和ART虚拟机简要对照
  6. oracle 的sys 和 system 账号
  7. select version();desc mysql.user;
  8. Android笔记之manifestPlaceholders
  9. BAPI_PO_CREATE1 创建PO ch_memory_complete = ‘x&#39;,导致hold on 解决方案,
  10. Android Touch事件分发