C代码使用GetDiskFreeSpaceEx获得磁盘分区大小信息,返回值是以字节数表示,我想用字节数/512,表示成扇区数,为什么不成功?谢谢!

时间:2023-01-11 18:03:21
#include<stdio.h>
#include<windows.h>
#include <stdlib.h>

CHAR buf1[52428800]; //定义全局字符数组变量,大小50M

BOOL GetDiskSpaceInfo(LPCWSTR pszDrive){
DWORD64 qwFreeBytesToCaller, qwTotalBytes, qwFreeBytes;
BOOL bResult;
//使用GetDiskFreeSpaceEx获取磁盘信息并打印结果
bResult = GetDiskFreeSpaceEx(pszDrive,
(PULARGE_INTEGER)&qwFreeBytesToCaller,
(PULARGE_INTEGER)&qwTotalBytes,
(PULARGE_INTEGER)&qwFreeBytes);
if (bResult){
printf("使用GetDiskFreeSpaceEx获取磁盘信息\n");
printf("可获得的空闲空间(字节):\t%I64d\n", qwFreeBytesToCaller);
printf("空闲空间(字节):\t\t%I64d\n", qwFreeBytes);
printf("磁盘总容量(字节):\t\t%I64d\n", qwTotalBytes);
}
return bResult;
}
int main(int argc, PCHAR argv[]){
//GetDiskSpaceInfo(argv[2]);

int i = 0;
int a = 0;
int num_str = 0;
DWORD RSize;

DWORD64 qwFreeBytesToCaller, qwTotalBytes, qwFreeBytes;
BOOL bResult;
//使用GetDiskFreeSpaceEx获取磁盘信息并打印结果
bResult = GetDiskFreeSpaceExA("G:",
(PULARGE_INTEGER)&qwFreeBytesToCaller,
(PULARGE_INTEGER)&qwTotalBytes,
(PULARGE_INTEGER)&qwFreeBytes);

if (bResult){
printf("使用GetDiskFreeSpaceEx获取磁盘信息\n");
printf("可获得的空闲空间(字节):\t%I64d\n", qwFreeBytesToCaller);
printf("空闲空间(字节):\t\t%I64d\n", qwFreeBytes);
printf("磁盘总容量(字节):\t\t%I64d\n", qwTotalBytes);
};
printf("磁盘总容量(扇区):\t\t%I64d\n", qwTotalBytes/512);
        return 0;
}

我这是截选的部分代码,问题出在最后一个printf语句,不知道为什么qwTotalBytes/512不能成功?之前的语句都正确执行了

3 个解决方案

#1


[不成功是什麽意思?輸出不符合預期?還是segment fault?, C代码使用GetDiskFreeSpaceEx获得磁盘分区大小信息,返回值是以字节数表示,我想用字节数/512,表示成扇区数,为什么不成功?谢谢!
就是这样,提示:未找到源, C代码使用GetDiskFreeSpaceEx获得磁盘分区大小信息,返回值是以字节数表示,我想用字节数/512,表示成扇区数,为什么不成功?谢谢!

#ifndef UNICODE
#define UNICODE
#endif // UNICODE

#include<stdio.h>
 #include<windows.h>
 #include <stdlib.h>

 CHAR buf1[52428800]; //定义全局字符数组变量,大小50M

 BOOL GetDiskSpaceInfo(LPCWSTR pszDrive){
 DWORD64 qwFreeBytesToCaller = 0, qwTotalBytes = 0, qwFreeBytes = 0;
 BOOL bResult = FALSE;
 //使用GetDiskFreeSpaceEx获取磁盘信息并打印结果
bResult = GetDiskFreeSpaceEx(pszDrive,
 (PULARGE_INTEGER)&qwFreeBytesToCaller,
 (PULARGE_INTEGER)&qwTotalBytes,
 (PULARGE_INTEGER)&qwFreeBytes);
 if (bResult){
 printf("使用GetDiskFreeSpaceEx获取磁盘信息\n");
 printf("可获得的空闲空间(字节):\t%I64d\n", qwFreeBytesToCaller);
 printf("空闲空间(字节):\t\t%I64d\n", qwFreeBytes);
 printf("磁盘总容量(字节):\t\t%I64d\n", qwTotalBytes);
 }
 return bResult;
 }
 int main(int argc, PCHAR argv[]){
 GetDiskSpaceInfo(L"D:");

 int i = 0;
 int a = 0;
 int num_str = 0;
 DWORD RSize = 0;

 DWORD64 qwFreeBytesToCaller = 0, qwTotalBytes = 0, qwFreeBytes = 0;
 BOOL bResult = FALSE;
 //使用GetDiskFreeSpaceEx获取磁盘信息并打印结果
bResult = GetDiskFreeSpaceExA("C:",
 (PULARGE_INTEGER)&qwFreeBytesToCaller,
 (PULARGE_INTEGER)&qwTotalBytes,
 (PULARGE_INTEGER)&qwFreeBytes);

 if (bResult){
 printf("使用GetDiskFreeSpaceEx获取磁盘信息\n");
 printf("可获得的空闲空间(字节):\t%I64d\n", qwFreeBytesToCaller);
 printf("空闲空间(字节):\t\t%I64d\n", qwFreeBytes);
 printf("磁盘总容量(字节):\t\t%I64d\n", qwTotalBytes);
 };
 printf("磁盘总容量(扇区):\t\t%I64d\n", qwTotalBytes/512);
         return 0;
 }

]

#1


[不成功是什麽意思?輸出不符合預期?還是segment fault?, C代码使用GetDiskFreeSpaceEx获得磁盘分区大小信息,返回值是以字节数表示,我想用字节数/512,表示成扇区数,为什么不成功?谢谢!
就是这样,提示:未找到源, C代码使用GetDiskFreeSpaceEx获得磁盘分区大小信息,返回值是以字节数表示,我想用字节数/512,表示成扇区数,为什么不成功?谢谢!

#ifndef UNICODE
#define UNICODE
#endif // UNICODE

#include<stdio.h>
 #include<windows.h>
 #include <stdlib.h>

 CHAR buf1[52428800]; //定义全局字符数组变量,大小50M

 BOOL GetDiskSpaceInfo(LPCWSTR pszDrive){
 DWORD64 qwFreeBytesToCaller = 0, qwTotalBytes = 0, qwFreeBytes = 0;
 BOOL bResult = FALSE;
 //使用GetDiskFreeSpaceEx获取磁盘信息并打印结果
bResult = GetDiskFreeSpaceEx(pszDrive,
 (PULARGE_INTEGER)&qwFreeBytesToCaller,
 (PULARGE_INTEGER)&qwTotalBytes,
 (PULARGE_INTEGER)&qwFreeBytes);
 if (bResult){
 printf("使用GetDiskFreeSpaceEx获取磁盘信息\n");
 printf("可获得的空闲空间(字节):\t%I64d\n", qwFreeBytesToCaller);
 printf("空闲空间(字节):\t\t%I64d\n", qwFreeBytes);
 printf("磁盘总容量(字节):\t\t%I64d\n", qwTotalBytes);
 }
 return bResult;
 }
 int main(int argc, PCHAR argv[]){
 GetDiskSpaceInfo(L"D:");

 int i = 0;
 int a = 0;
 int num_str = 0;
 DWORD RSize = 0;

 DWORD64 qwFreeBytesToCaller = 0, qwTotalBytes = 0, qwFreeBytes = 0;
 BOOL bResult = FALSE;
 //使用GetDiskFreeSpaceEx获取磁盘信息并打印结果
bResult = GetDiskFreeSpaceExA("C:",
 (PULARGE_INTEGER)&qwFreeBytesToCaller,
 (PULARGE_INTEGER)&qwTotalBytes,
 (PULARGE_INTEGER)&qwFreeBytes);

 if (bResult){
 printf("使用GetDiskFreeSpaceEx获取磁盘信息\n");
 printf("可获得的空闲空间(字节):\t%I64d\n", qwFreeBytesToCaller);
 printf("空闲空间(字节):\t\t%I64d\n", qwFreeBytes);
 printf("磁盘总容量(字节):\t\t%I64d\n", qwTotalBytes);
 };
 printf("磁盘总容量(扇区):\t\t%I64d\n", qwTotalBytes/512);
         return 0;
 }

]