[C] Récupérer espace disque

Récupérer espace disque [C] - C++ - Programmation

Marsh Posté le 19-04-2002 à 15:15:49    

Je me demandais si il y avait un moyen simple de récupérer l'espace disque libre et l'espace disque total en langage C voir meme si c'était possible le pourcentage d'occupation du disque.
Merci d'avance!

Reply

Marsh Posté le 19-04-2002 à 15:15:49   

Reply

Marsh Posté le 19-04-2002 à 21:33:42    

Personne??

Reply

Marsh Posté le 19-04-2002 à 21:52:14    

Cherche du côté de statvfs, qui sert visblement à ça, mais je n'ai pas trouvé de doc dessus.


---------------
« No question is too silly to ask, but, of course, some are too silly to answer. » -- Perl book
Reply

Marsh Posté le 19-04-2002 à 21:58:50    

du MSDN :
 
GetDiskFreeSpace
The GetDiskFreeSpace function retrieves information about the specified disk, including the amount of free space on the disk.  
 
The GetDiskFreeSpace function cannot report volume sizes that are greater than 2 GB. To ensure that your application works with large capacity hard drives, use the GetDiskFreeSpaceEx function.  
 
BOOL GetDiskFreeSpace(
  LPCTSTR lpRootPathName,          // root path
  LPDWORD lpSectorsPerCluster,     // sectors per cluster
  LPDWORD lpBytesPerSector,        // bytes per sector
  LPDWORD lpNumberOfFreeClusters,  // free clusters
  LPDWORD lpTotalNumberOfClusters  // total clusters
);
Parameters
lpRootPathName  
[in] Pointer to a null-terminated string that specifies the root directory of the disk to return information about. If lpRootPathName is NULL, the function uses the root of the current directory. If this parameter is a UNC name, you must follow it with a trailing backslash. For example, you would specify \\MyServer\MyShare as \\MyServer\MyShare\. However, a drive specification such as "C:" must have a trailing backslash.  
Windows 95: The initial release of Windows 95 does not support UNC paths for the lpszRootPathName parameter. To query the free disk space using a UNC path, temporarily map the UNC path to a drive letter, query the free disk space on the drive, then remove the temporary mapping.  
 
Windows 95 OSR2 and later: UNC paths are supported.  
 
lpSectorsPerCluster  
[out] Pointer to a variable for the number of sectors per cluster.  
lpBytesPerSector  
[out] Pointer to a variable for the number of bytes per sector.  
lpNumberOfFreeClusters  
[out] Pointer to a variable for the total number of free clusters on the disk that are available to the user associated with the calling thread.  
Windows 2000 and later: If per-user disk quotas are in use, this value may be less than the total number of free clusters on the disk.  
 
lpTotalNumberOfClusters  
[out] Pointer to a variable for the total number of clusters on the disk that are available to the user associated with the calling thread.  
Windows 2000 and later: If per-user disk quotas are in use, this value may be less than the total number of clusters on the disk.  
 
Return Values
If the function succeeds, the return value is nonzero.
 
If the function fails, the return value is zero. To get extended error information, call GetLastError.  
 
Remarks
The GetDiskFreeSpaceEx function lets you avoid some of the arithmetic required by the GetDiskFreeSpace function.
 
Windows 95 OSR2 and later: The GetDiskFreeSpaceEx function is available beginning with Windows 95 OEM Service Release 2 (OSR2), and you should use it whenever possible. The GetDiskFreeSpaceEx function returns correct values for all volumes, including those that are larger than 2 gigabytes.  
 
Windows 95: For volumes that are larger than 2 gigabytes, the GetDiskFreeSpace function may return misleading values. The function caps the values stored into *lpNumberOfFreeClusters and *lpTotalNumberOfClusters so as to never report volume sizes that are greater than 2 gigabytes. Even on volumes that are smaller than 2 gigabytes, the values stored into *lpSectorsPerCluster, *lpNumberOfFreeClusters, and *lpTotalNumberOfClusters values may be incorrect. That is because the operating system manipulates the values so that computations with them yield the correct volume size.
 
Requirements  
  Windows NT/2000 or later: Requires Windows NT 3.1 or later.
  Windows 95/98/Me: Requires Windows 95 or later.
  Header: Declared in Winbase.h; include Windows.h.
  Library: Use Kernel32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000

Reply

Marsh Posté le 19-04-2002 à 21:59:48    

Et aussi :
 
GetDiskFreeSpaceEx
The GetDiskFreeSpaceEx function retrieves information about the amount of space available on a disk volume: the total amount of space, the total amount of free space, and the total amount of free space available to the user associated with the calling thread.  
 
BOOL GetDiskFreeSpaceEx(
  LPCTSTR lpDirectoryName,                 // directory name
  PULARGE_INTEGER lpFreeBytesAvailable,    // bytes available to caller
  PULARGE_INTEGER lpTotalNumberOfBytes,    // bytes on disk
  PULARGE_INTEGER lpTotalNumberOfFreeBytes // free bytes on disk
);
Parameters
lpDirectoryName  
[in] Pointer to a null-terminated string that specifies a directory on the disk of interest. This string can be a UNC name. If this parameter is a UNC name, you must follow it with an additional backslash. For example, you would specify \\MyServer\MyShare as \\MyServer\MyShare\.  
If lpDirectoryName is NULL, the GetDiskFreeSpaceEx function retrieves information about the disk that contains the current directory.  
 
Note that lpDirectoryName does not have to specify the root directory on a disk. The function accepts any directory on the disk.  
 
lpFreeBytesAvailable  
[out] Pointer to a variable that receives the total number of free bytes on the disk that are available to the user associated with the calling thread.  
Windows 2000 and later: If per-user quotas are in use, this value may be less than the total number of free bytes on the disk.  
 
lpTotalNumberOfBytes  
[out] Pointer to a variable that receives the total number of bytes on the disk that are available to the user associated with the calling thread.  
Windows 2000 and later: If per-user quotas are in use, this value may be less than the total number of bytes on the disk.  
 
lpTotalNumberOfFreeBytes  
[out] Pointer to a variable that receives the total number of free bytes on the disk.  
This parameter can be NULL.  
 
Return Values
If the function succeeds, the return value is nonzero.  
 
If the function fails, the return value is zero. To get extended error information, call GetLastError.  
 
Remarks
Note that the values obtained by this function are of type ULARGE_INTEGER. Be careful not to truncate these values to 32 bits.
 
Windows 95 OSR2 and later: The GetDiskFreeSpaceEx function is available beginning with Windows 95 OEM Service Release 2 (OSR2). To determine whether GetDiskFreeSpaceEx is available, call GetModuleHandle to get the handle to Kernel32.dll. Then you can call GetProcAddress.
 
The following code fragment shows one way to do this:
 
pGetDiskFreeSpaceEx = GetProcAddress( GetModuleHandle("kernel32.dll" ),
                         "GetDiskFreeSpaceExA" );
 
if (pGetDiskFreeSpaceEx)
{
   fResult = pGetDiskFreeSpaceEx (pszDrive,
                (PULARGE_INTEGER)&i64FreeBytesToCaller,
                (PULARGE_INTEGER)&i64TotalBytes,
                (PULARGE_INTEGER)&i64FreeBytes);
 
// Process GetDiskFreeSpaceEx results.
}
 
else  
{
   fResult = GetDiskFreeSpace (pszDrive,  
                &dwSectPerClust,  
                &dwBytesPerSect,
                &dwFreeClusters,  
                &dwTotalClusters)
 
// Process GetDiskFreeSpace results.
 
}
It is not necessary to call LoadLibrary on Kernel32.dll because it is already loaded into every process address space.
 
Requirements  
  Windows NT/2000 or later: Requires Windows NT 4.0 or later.
  Windows 95/98/Me: Requires Windows 95 OSR2 or later.
  Header: Declared in Winbase.h; include Windows.h.
  Library: Use Kernel32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.

Reply

Marsh Posté le 19-04-2002 à 23:01:53    

Heu moi c pour linux

Reply

Marsh Posté le 19-04-2002 à 23:36:18    

A ben c pas marqué  :heink:

Reply

Marsh Posté le 19-04-2002 à 23:55:50    

scuse moi merci pour ta réponse c'est quand même très sympa de ta part d'autant que c'était bien détaillé, enfin c'est de ma faute j'ai pas été clair

Reply

Marsh Posté le 20-04-2002 à 01:12:17    

Sous Unix c'est bien statvfs. La fonction n'est pas documentée sous Linux mais on trouve la doc pour d'autres Unix :http://www.chemie.fu-berlin.de/cgi-bin/man/sgi_irix?statvfs+2

Code :
  1. #include <sys/types.h>
  2. #include <sys/statvfs.h>
  3. struct statvfs toto;
  4. char *titi;
  5. int espacelibre, espacetotal;
  6. titi="/mnt/mapartition"
  7. if(statvfs (titi, &toto))
  8.   {
  9.    perror(titi);
  10.    exit(-1);
  11.   }
  12. espacelibre=toto.f_bsize*toto.f_bfree;
  13. espacetotal=toto.f_bsize*toto.f_blocks;


Pour récupérer les noms des différentes partitions, tu peux regarder dans /proc/mounts.


---------------
« No question is too silly to ask, but, of course, some are too silly to answer. » -- Perl book
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed