首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

编程练习时的异常(编译连接无有关问题,执行时无任何反应)

2012-09-19 
求助:编程练习时的错误(编译连接无问题,执行时无任何反应)这个程序就是想把卷遍历一遍,输出相应信息。但是

求助:编程练习时的错误(编译连接无问题,执行时无任何反应)
这个程序就是想把卷遍历一遍,输出相应信息。但是编译连接无问题,执行时无任何反应
#include<windows.h>
#include<iostream>
#define buflength 1024
using namespace std;
bool getdriveinfo(PCHAR szDRIVE)
{
UINT DriveType=GetDriveType(szDRIVE);
switch(DriveType)
{
case DRIVE_UNKNOWN:cout<<"The drive type cannot be determined."<<endl; break;
case DRIVE_NO_ROOT_DIR:cout<<"The root path is invalid; for example, there is no volume mounted at the specified path."<<endl;break;
case DRIVE_REMOVABLE:cout<<"The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader."<<endl;break;
case DRIVE_FIXED:cout<<"The drive has fixed media; for example, a hard disk drive or flash drive."<<endl;break;
case DRIVE_REMOTE:cout<<"The drive is a remote (network) drive."<<endl;break;
case DRIVE_CDROM:cout<<"The drive is a CD-ROM drive."<<endl;break;
case DRIVE_RAMDISK:cout<<"The drive is a RAM disk."<<endl;
}
LPSTR lpVolumeNameBuf;
LPDWORD lpVolumeSerialNumber;
LPDWORD llpMaximumComponentLength;
LPDWORD lpFileSystemFlags;
LPSTR lpFileSystemNameBuf;
if(!GetVolumeInformation(szDRIVE,lpVolumeNameBuf,lstrlen(szDRIVE),lpVolumeSerialNumber,llpMaximumComponentLength,lpFileSystemFlags,lpFileSystemNameBuf,buflength))
{
return false;
}
cout<<"VolumeSerialNumber is :"<<lpVolumeSerialNumber<<endl;
cout<<"MaximumComponentLength is:"<<llpMaximumComponentLength<<endl;
cout<<"FileSystemFlags is:"<<lpFileSystemFlags<<endl;
cout<<"FileSystemNameBuf is"<<lpFileSystemNameBuf<<endl;
return true;
}
int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR IpCmdLine,int nCmdShow)
{
CHAR lpBuf[buflength];
PCHAR DRIVE;

ZeroMemory(lpBuf,buflength);
GetLogicalDriveStrings(buflength,lpBuf);
DRIVE=(PCHAR)lpBuf;
while((DRIVE)!="\0x00")
{
if(!getdriveinfo(DRIVE))
{
cout<<"获取信息错误,错误代码:"<<GetLastError()<<endl;
}
DRIVE+=(lstrlen(DRIVE)+1);
}

return 0;
}
谢谢各位了!

[解决办法]
下断点在程序main入口,单步执行,即可知结果。
[解决办法]
把代码书写规范之后,然后单步调试,自己动手,丰衣足食
[解决办法]
怎么看上去是死循环的样子?
这个if里面是不是应该加个break?
if(!getdriveinfo(DRIVE))
[解决办法]
全文都没个注释什么的,代码混乱! 难怪出错不好找。
[解决办法]
单步调试和设断点调试是程序员必须掌握的技能之一。

[解决办法]
出现没反应的情况应该是函数【getdriveinfo】里面的那些指针没有初期化而导致的。
修改后得代码如下:

C/C++ code
#include<windows.h>#include<iostream>#define buflength 1024using namespace std;bool getdriveinfo(PCHAR szDRIVE){    const int INFOBUF_SIZE=256;        printf("%s\n", szDRIVE);       UINT DriveType=GetDriveType(szDRIVE);   switch(DriveType)   {      case DRIVE_UNKNOWN:cout<<"The drive type cannot be determined."<<endl; break;      case DRIVE_NO_ROOT_DIR:cout<<"The root path is invalid; for example, there is no volume mounted at the specified path."<<endl;break;      case DRIVE_REMOVABLE:cout<<"The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader."<<endl;break;      case DRIVE_FIXED:cout<<"The drive has fixed media; for example, a hard disk drive or flash drive."<<endl;break;      case DRIVE_REMOTE:cout<<"The drive is a remote (network) drive."<<endl;break;      case DRIVE_CDROM:cout<<"The drive is a CD-ROM drive."<<endl;break;      case DRIVE_RAMDISK:cout<<"The drive is a RAM disk."<<endl;   }   // 这些指针都需要初始化   TCHAR volumeName[INFOBUF_SIZE];   LPSTR lpVolumeNameBuf = volumeName;   LPDWORD lpVolumeSerialNumber = (LPDWORD)malloc(sizeof(DWORD) * 256);   LPDWORD llpMaximumComponentLength = (LPDWORD)malloc(sizeof(DWORD) * 256);   LPDWORD lpFileSystemFlags = (LPDWORD)malloc(sizeof(DWORD) * 256);   LPSTR lpFileSystemNameBuf = (LPSTR)malloc(sizeof(TCHAR) * 256);      if(!GetVolumeInformation(szDRIVE, lpVolumeNameBuf, INFOBUF_SIZE,lpVolumeSerialNumber,llpMaximumComponentLength,lpFileSystemFlags,lpFileSystemNameBuf,buflength))   {       cout<< "false" << endl;       return false;   }   cout<<"VolumeSerialNumber is :"<<lpVolumeSerialNumber<<endl;   cout<<"MaximumComponentLength is:"<<llpMaximumComponentLength<<endl;   cout<<"FileSystemFlags is:"<<lpFileSystemFlags<<endl;   cout<<"FileSystemNameBuf is:"<<lpFileSystemNameBuf<<endl;   return true;}int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR IpCmdLine,int nCmdShow){   CHAR lpBuf[buflength];   PCHAR DRIVE;   int count = 0;      ZeroMemory(lpBuf,buflength);   GetLogicalDriveStrings(buflength,lpBuf);   DRIVE=(PCHAR)lpBuf;   while((*DRIVE) != 0x00)   {                    if(!getdriveinfo(DRIVE))      {          cout<<"Error occurs when getting drive info\n Error code:"<<GetLastError()<<endl;      }      cout << endl;      DRIVE += (lstrlen(DRIVE)+1);    }    return 0;} 

热点排行