如何打印WIN32_FIND_DATA结构体中的文件信息

hbxtght 2011-07-09 11:37:44
RT,求助
...全文
50 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
c_losed 2011-07-09
  • 打赏
  • 举报
回复

#define _WIN32_WINNT 0x0501
#include <Windows.h>
#include <stdio.h>
#include <malloc.h>
#include <tchar.h>
#include <wchar.h>
#include <strsafe.h>

#define BUFSIZE MAX_PATH

int _tmain(int argc, TCHAR *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;
LPTSTR DirSpec;
size_t length_of_arg;
INT retval;

DirSpec = (LPTSTR) malloc (BUFSIZE);

if( DirSpec == NULL )
{
printf( "Insufficient memory available\n" );
retval = 1;
goto Cleanup;
}


// Check for the directory to query, specified as
// a command-line parameter; otherwise, print usage.
if(argc != 2)
{
_tprintf(TEXT("Usage: Test <dir>\n"));
retval = 2;
goto Cleanup;
}

// Check that the input is not larger than allowed.
StringCbLength(argv[1], BUFSIZE, &length_of_arg);

if (length_of_arg > (BUFSIZE - 2))
{
_tprintf(TEXT("Input directory is too large.\n"));
retval = 3;
goto Cleanup;
}

_tprintf (TEXT("Target directory is %s.\n"), argv[1]);

// Prepare string for use with FindFile functions. First,
// copy the string to a buffer, then append '\*' to the
// directory name.
StringCbCopyN (DirSpec, BUFSIZE, argv[1], length_of_arg+1);
StringCbCatN (DirSpec, BUFSIZE, TEXT("\\*"), 2*sizeof(TCHAR));

// Find the first file in the directory.
hFind = FindFirstFile(DirSpec, &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)
{
_tprintf (TEXT("Invalid file handle. Error is %u.\n"),
GetLastError());
retval = (-1);
}
else
{
_tprintf (TEXT("First file name is: %s\n"),
FindFileData.cFileName);

// List all the other files in the directory.
while (FindNextFile(hFind, &FindFileData) != 0)
{
_tprintf (TEXT("Next file name is: %s\n"),
FindFileData.cFileName);
}

dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
_tprintf (TEXT("FindNextFile error. Error is %u.\n"),
dwError);
retval = (-1);
goto Cleanup;
}
}
retval = 0;

Cleanup:
free(DirSpec);
return retval;

}

照代码改改就哦了
hbxtght 2011-07-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 c_losed 的回复:]

C/C++ code

#define _WIN32_WINNT 0x0501
#include <Windows.h>
#include <stdio.h>
#include <malloc.h>
#include <tchar.h>
#include <wchar.h>
#include <strsafe.h>

#define BUFSIZE MAX_PATH

int _tmain……
[/Quote]不能用printf打印吗?
遍历并搜索枚举当前Windows Mobile系统可用SD卡的源代码,更多资源请访问http://www.59186618.com 在WMSD卡是重要的存储设备相当于PC系统的硬盘。用户有大量数据和程序保存在其。SD卡一般以目录形式出现在根目录下,用户可以方便的访问比如模拟器上一般为\Storage Card目录。但是在真实系统SD卡对应的目录名不是固定的。比如我见过\SDMMC或者\存储卡等等。 甚至在模拟器上如果在设置SD卡共享目录前已经存在\Storage Card目录则系统会将SD卡目录自动设置为\Storage Card2。因此我们的程序不能固定使用\Storage Card作为SD卡的目录名。WM有一组特殊的API可以解决这个问题。 头文件:projects.h Lib库: note_prj.lib FindFirstFlashCard():获取第一个可用的SD卡信息,使用WIN32_FIND_DATA结构体保存这些信息。 返回查找句柄。 FindNextFlashCard():使用查找句柄和WIN32_FIND_DATA查找下一个可用的SD卡信息,返回BOOL类型的查找结果。 FindClose():关闭查找句柄。 示例程序枚举所有可用SD卡信息,并用弹出对话框显示SD卡目录名。 BOOL bContinue = TRUE; // If TRUE, continue // searching // If FALSE, stop searching. HANDLE hFlashCard; // Search handle for storage // cards WIN32_FIND_DATA *lpwfdFlashCard; // Structure for storing // card information. lpwfdFlashCard = (WIN32_FIND_DATA *) LocalAlloc (LPTR, 10 * sizeof (WIN32_FIND_DATA)); if (lpwfdFlashCard != NULL) // Failed allocate memory return; { hFlashCard = FindFirstFile(TEXT("\\*"), lpwfdFlashCard); //hFlashCard = FindFirstFlashCard (lpwfdFlashCard); } if (hFlashCard == INVALID_HANDLE_VALUE) { LocalFree (lpwfdFlashCard); // Free the memory. return; } while (bContinue) { //判断代码 if ((lpwfdFlashCard->dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_TEMPORARY)) == (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_TEMPORARY)) { csPath = lpwfdFlashCard->cFileName; // This Folder is an mounted device (e.g. flash card) } // Search for the next storage card. bContinue = FindNextFile(hFlashCard, lpwfdFlashCard); //bContinue = FindNextFlashCard (hFlashCard, lpwfdFlashCard); } FindClose (hFlashCard); // Close the search handle. LocalFree (lpwfdFlashCard); // Free the memory. 遍历并搜索枚举当前Windows Mobile系统可用SD卡的源代码,更多资源请访问http://www.59186618.com
代码静态分析工具PC-LINT安装配置 PC-Lint是C/C++软件代码静态分析工具,你可以把它看作是一种更加严格的编译器。它不仅可以检查出一般的语法错误,还可以检查出那些虽然符合语法要求但不易发现的潜在错误。 C语言的灵活性带来了代码效率的提升,但相应带来了代码编写的随意性,另外C编译器不进行强制类型检查,也带来了代码编写的隐患。PCLint识别并报告C语言的编程陷阱和格式缺陷的发生。它进行程序的全局分析,能识别没有被适当检验的数组下标,报告未被初始化的变量,警告使用空指针,冗余的代码,等等。软件除错是软件项目开发成本和延误的主要因素。PClint能够帮你在程序动态测试之前发现编码错误。这样消除错误的成本更低。 使用PC-Lint在代码走读和单元测试之前进行检查,可以提前发现程序隐藏错误,提高代码质量,节省测试时间。并提供编码规则检查,规范软件人员的编码行为。 由于PC-LINT对于一般程序员来说可能比较陌生,有好多人安装了也不知道怎样配置和使用。 下面我就根据自己的安装和配置心得对PC-Lint的安装、配置及使用进行下详细说明.本人主要介绍了将PC-Lint集成到VC++6.0和SourceInsight的方法和步骤。 (一)Windows下C/C++开发工具,VC6使用较为普遍,因此这里先讲下VC6.0环境集成pclint的步骤. 首先, 当然要下载软件,正版软件要200多$呢,买不起!所以只好网上找免费的拉。从http://www.61ic.com/down/othe/pclint.rar处可以下载到一个8.0版本的pclint. 1.将pclint.rar解压至c:\, 这样lint文件就位与c:\pclint(安装目录)下了。 2.将c:\pclint\lnt 下的3个文件lib-w32.lnt,env-vc6.lnt,co-msc60.lnt拷贝至c:\pclint下, 再在安装目录下创建std.lnt和options.lnt两个文件,其std.lnt的内容如下 // contents of std.lnt c:\pclint\co-msc60.lnt c:\pclint\lib-w32.lnt c:\pclint\options.lnt -si4 -sp4 -i"D:\Program Files;D:\Program Files\Microsoft Visual Studio\VC98\Include" //end 其-i后面的路径名为VC的安装路径和VC Include 文件路径,根据自己的修改便可。 options.lnt 内容可为空,为定制内容,以后需要时再添加。 准备工作做完了,下一步就是要将pclint集成到VC6去,先配置lint使之能对单个C或C++文件进行检查。 1.打开VC6,tools--->customize-->tools 新建一个名为pclint的项,在下面填入 command: C:\pclint\lint-nt.exe arguments: -u c:\pclint\std.lnt c:\pclint\env-vc6.lnt "$(FilePath)" Use Output Window 打上勾 close 完成。 这个在你VC窗口tools菜单下应该多了一个pclint选项,可以用它来运行lint程序,对你的c/c++代码进行静态检查了。 现在就可以用个小程序测试一下pclint了 //test1.cpp #include class X { int *p; public: X() { p = new int[20]; } void init() { memset( p, 20, 'a' ); } ~X() { delete p; } }; 编译这个文件,看下你的编译器给你多少警告,再运行下lint, 可以自己对比一下。 我的机器上,VC产生0 errors 0 warnings, 而lint程序产生了如下8条警告信息,有些还是很有用处的提示,这里就不一一分析了. test.cpp(12): error 783: (Info -- Line does not end with new-line) test.cpp(7): error 1732: (Info -- new in constructor for class 'X' which has no assignment operator) test.cpp(7): error 1733: (Info -- new in constructor for class 'X' which has no copy constru

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧