返回驱动器号列表的程序
Program that returns a list of drive letters
在我的计算机示例中,所需的输出应该是:"C: E: F: H: N:" .我知道这是可能的,但最简单的方法是什么?Pottering in QueryDosDevice 输出
#ifndef UNICODE
#define UNICODE
#endif
#include <Windows.h>
#include <fstream>
#include <iostream>
const int REPORT_LENGTH = 5000;
int main(void)
{
TCHAR targetPath[REPORT_LENGTH];
std::ofstream oFile;
oFile.open("dos device query.txt");
QueryDosDevice(NULL,targetPath,REPORT_LENGTH);
for(int i=0; i<REPORT_LENGTH;i++)
if (targetPath[i]==' ')(targetPath[i]='n');
for(int i=0; i<REPORT_LENGTH; i++)
oFile<<static_cast<char>(targetPath[i]);
oFile.close();
return 0;
}
这将是对时间和资源的巨大浪费。此外,函数GetLogicalDriveStrings背叛了我很多。
#include <Windows.h>
int main()
{
TCHAR buffer[50];
GetLogicalDriveStrings(50,buffer);
MessageBox(0,buffer,"Drives in the system",MB_OK);
return 0;
}
它只显示"C:\"伏柳胺。
带有 GetLogicalDrives
的示例,尽管不是连接到字符串(作为练习留给 OP 和读者;)(:
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
int __cdecl _tmain(int argc, _TCHAR *argv[])
{
// Get the bit mask of drive letters
DWORD drives = ::GetLogicalDrives();
// Go through all possible letters from a to z
for(int i = 0; i < 26; i++)
{
// Check if the respective bit is set
if(drives & (1 << i))
{
// ... and if so, print it
_tprintf(TEXT("Drive %c: existsn"), _T('A') + i);
}
}
return 0;
}
GetLogicalDriveStrings()
是要走的路,你只需要正确使用。 您假设它返回一个包含所有驱动器字符串的字符串,但事实并非如此。 它返回一个字符串数组,每个驱动器一个,因此您必须遍历数组:
#include <windows.h>
int main()
{
TCHAR buffer[(4*26)+1] = {0};
GetLogicalDriveStrings(sizeof(buffer) / sizeof(TCHAR), buffer);
for (LPTSTR lpDrive = buffer; *lpDrive != 0; lpDrive += 4)
MessageBox(NULL, lpDrive, "Drive in the system", MB_OK);
return 0;
}
相关文章:
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 处理程序的模块列表中有一个错误的模块"WebSocketModule"
- 程序输入名称和高度并显示它。我不知道如何显示列表中最高人的姓名和身高
- 在C++初始化程序列表中使用Integer Literals
- 在后台记录 Eclipse IDE 调用的应用程序列表
- 类的头文件中的C++初始化程序列表
- 显示应用程序列表,如Win7中的alt选项卡
- 初始化程序列表
- 如何获取与某个扩展相关联的程序列表
- 避免对自定义类使用初始化程序列表
- 初始化程序列表和赋值重载(运算符=)
- 引用包装的初始化程序列表
- c++初始化程序列表
- 将STL容器内容与初始化程序列表进行比较
- 初始化程序列表作为容器不起作用
- 初始化程序列表作为数组的函数参数传递
- 使用大括号的统一初始化被误认为初始化程序列表
- 初始化程序列表不正常
- 初始化程序列表与初始化方法
- Visual Studio 2012 - 为什么统一初始化程序列表功能在 VS2012 中不起作用 C++.