【发布时间】:2013-09-02 12:09:50
【问题描述】:
我想搜索可能存在于任何驱动器中的文件,例如 C:\、D:\ 等。使用 GetLogicalDriveStrings 我可以获得驱动器列表,但是当我为输出添加任何额外内容时,我在输出提示中收到null。这是我的代码:
#include "StdAfx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
// Buffer length
DWORD mydrives = 100;
// Buffer for drive string storage
char lpBuffer[100];
const char *extFile = "text.ext";
// You may want to try the wmain() version
int main(void)
{
DWORD test;
int i;
test = GetLogicalDriveStrings(mydrives, (LPWSTR)lpBuffer);
if(test != 0)
{
printf("GetLogicalDriveStrings() return value: %d, Error (if any): %d \n", test, GetLastError());
printf("The logical drives of this machine are:\n");
// Check up to 100 drives...
for(i = 0; i<100; i++)
printf("%c%s", lpBuffer[i],extFile);
printf("\n");
}
else
printf("GetLogicalDriveStrings() is failed lor!!! Error code: %d\n", GetLastError());
_getch();
return 0;
}
我希望上面的输出为C:\text.ext D:\text.ext ...,而我只得到text.ext。我正在使用Microsoft Visual C++ 2010 Express
【问题讨论】:
-
您将“text.ext”分配给
extFile100 次,然后打印一个由lpBuffer[100](超出缓冲区末尾)和名为datFile的字符串组成的字符串在您的示例中其他任何地方都没有出现。请在发布帮助之前更仔细地检查您的代码。
标签: c++ char msdn const-char