我正在尝试列出我的Pc的逻辑驱动器并将结果添加到矢量但我得到这个奇怪的result = ;;;.
我在这里失踪了什么?
std::vector<std::string> directory::getLogicalDrives() {
DWORD mydrives = 100;
char lpBuffer[100];
DWORD drives = GetLogicalDriveStrings(mydrives, lpBuffer);
std::vector<std::string> driveList;
for (int i = 0; i < 100; i++) {
std::string drive(3, '%c' + lpBuffer[0]); // Missing something?
driveList.push_back(drive);
}
return driveList;
}
Run Code Online (Sandbox Code Playgroud)
如文档 中所述,GetLogicalDriveStrings()为您提供以NULL结尾的字符串列表,列表以NULL字符终止.所以只需迭代该列表,例如:
std::vector<std::string> directory::getLogicalDrives() {
std::vector<std::string> driveList;
char szBuffer[105];
DWORD size = GetLogicalDriveStrings(104, szBuffer);
if ((size > 0) && (size <= 104)) {
const char *pstr = szBuffer;
while( *pstr ) {
std::string drive( pstr );
driveList.push_back(drive);
pstr += (drive.length() + 1);
}
}
return driveList;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
151 次 |
| 最近记录: |