首页 > 其他 > 详细

C杂谈

时间:2017-03-24 00:10:00      阅读:177      评论:0      收藏:0      [点我收藏+]

最近在做关于C的项目开发,记录一下有关C的操作,比较杂乱

1.利用System进行文件数量统计:

1)  

1 system("dir /b /s /ad d:\\mydir\\*.* | find /c \":\" >d:\\ndirs.txt");

    //读文件d:\\ndirs.txt的内容即d:\\mydir目录下的文件夹数

1  system("dir /b /s /a-d d:\\mydir\\*.* | find /c \":\" >d:\\nfiles.txt");

    //读文件d:\\nfiles.txt的内容即d:\\mydir目录下的文件数

    顺带也写一下读取文件以及将Char类型字符转化为Int类型代码

 1 ifstream in;
 2 string str;
 3 in.open("D:\\nfiles.txt");
 4 if (!in.is_open()){
 5      cout << "Error opening file"; exit(1);
 6  }
 7 else{
 8     std::copy(std::istream_iterator<unsigned char>(in),std::istream_iterator<unsigned char>(), back_inserter(str));
 9 }
10 filecount = atoi(str.c_str());

2)

 1 HANDLE hFind;
 2 WIN32_FIND_DATA dataFind;
 3 BOOL bMoreFiles = TRUE;
 4 
 5 //m_strDir就是你要指定的路径
 6 hFind = FindFirstFile(s2ws(add + "\*.*").c_str(), &dataFind);//找到路径中所有文件
 7 
 8 //遍历路径中所有文件
 9 while (hFind != INVALID_HANDLE_VALUE&&bMoreFiles == TRUE)
10 {                
11     if (dataFind.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)//判断是否是文件
12     {
13         filecount++;
14     }
15     bMoreFiles = FindNextFile(hFind, &dataFind);
16 }
17 FindClose(hFind);

      这种方法个人使用存在BUG,当文件数量大于1时,filevount不会继续增加,后续看到更改方法继续改进。

 

2.string转LPCWSTR(C文件拷贝)

 1 wstring s2ws(const std::string& s)
 2 
 3 {
 4 int len;
 5 int slength = (int)s.length() + 1;
 6 len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
 7 wchar_t* buf = new wchar_t[len];
 8 MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
 9 std::wstring r(buf);
10 delete[] buf;
11 return r;
12 }
13 
14 CopyFile(s2ws(source).c_str(), s2ws(des).c_str(), FALSE);//false代表覆盖,true不覆盖

 

3.C语言遍历文件夹下文件

 1 long hFile = 0;
 2 struct _finddata_t fileInfo;
 3 string pathName, exdName;
 4 
 5 if ((hFile = _findfirst(pathName.assign(path).append("\\*").c_str(), &fileInfo)) == -1) {
 6     return;
 7 }
 8 do {
 9     printf("%s\n", path.c_str());
10     printf("%s\n", fileInfo.name);
11     Mat library_img = imread(path + "/" + fileInfo.name);
12     } while (_findnext(hFile, &fileInfo) == 0);
13 _findclose(hFile);

 

C杂谈

原文:http://www.cnblogs.com/pkjplayer/p/6607783.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!