c++:如何遍历目录后获得路径,文件名后,然后将所有获得的二进制文件分别读入,写进txt,文件名不变。
萝 卜哥哥
2024-06-03 22:03:51
最佳回答
/* * 不知道你想做啥 * 计算机里的一切本来就都是二进制了,如果你想打印hex,那你得自己一个一个字节的转换成文本输出 * 要做什么自己在注释部分修改吧 * 要真想做实际的开发,就少用c++ */#include <windows.h>#include <stdio.h>static lpctstr lpcext = text("*.day");//////////// what todo ////////////bool whattodo(lpctstr fullpathname) {handle hfile = createfile(text("l**ts.txt"), open_always, 0, **, open_ex**ting, file_attribute_normal,**); if (hfile == invalid_handle_value) { printf_s ("createfile failed with error %d.\n", getlasterror());return false;} dword dwbyteswritten;bool bsuccess = writefile(hfile, fullpathname, lstrlen(fullpathname),&dwbyteswritten, **); if (!bsuccess) {printf_s ("writefile failed with error %d.\n", getlasterror());return false;}return true;}//////////// what todo ////////////int findfile (lpctstr path){ win32_find_data finddata; handle hhandle = **; tchar filepathname[max_path];lstrcpy(filepathname, path);lstrcat(filepathname, lpcext); hhandle = findfirstfile (filepathname, &finddata); if (hhandle == invalid_handle_value) { return false; } do {if (!lstrcmp(text("."), finddata.cfilename) || !lstrcmp(text(".."), finddata.cfilename)) continue;tchar fullpathname[max_path];lstrcpy(fullpathname, path);lstrcat(fullpathname, finddata.cfilename); if (finddata.dwfileattributes & file_attribute_directory) findfile (fullpathname);else {//////////// todo here ////////////lstrcat(fullpathname, text("\r\n"));if(!whattodo(fullpathname))printf_s("failed\n");//////////// todo here ////////////} } while (findnextfile (hhandle, &finddata)); findclose (hhandle); return true;}void main(){lptstr path = text("r:\\demo\\");if(findfile (path))printf_s("done\n");} 20210311