File size in C (Win32)

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

File size in C (Win32)

 
0
  #1
Dec 29th, 2005
Hi, I am trying to get the file size of a .exe file in C, I have tried using the GetFileSize(); function using the CreateFile(); function to get a handle for it, but the value returned is a DWORD and doesnt seem to work when i output it. I want to just retrieve the filesize of a .exe file and store this value in bytes in a long integer, does anyone have any other methods on this or can you help to correct my problem, here is my current code for this part:
  1. HANDLE MF = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
  2. DWORD Size;
  3. GetFileSize(MF,&Size);
  4.  
  5. SendMessage(LogListBox,LB_ADDSTRING,0,(LPARAM)Size);
Any suggestions ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,443
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: File size in C (Win32)

 
0
  #2
Dec 29th, 2005
Look up GetFileSize.
  1. #include <stdio.h>
  2. #include <windows.h>
  3.  
  4. int main(void)
  5. {
  6. const char FilePath[] = "main.exe";
  7. HANDLE MF = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
  8. OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
  9. if ( MF != INVALID_HANDLE_VALUE )
  10. {
  11. DWORD High, Low = GetFileSize(MF, &High);
  12. if ( Low != 0xFFFFFFFF )
  13. {
  14. printf("Size : High = %lu, Low = %lu\n", High, Low);
  15. }
  16. }
  17. return 0;
  18. }
  19.  
  20. /* my output
  21. Size : High = 0, Low = 52736
  22. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,585
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1487
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File size in C (Win32)

 
0
  #3
Dec 29th, 2005
if the file isn't too big, stat() will return its size, among other file info. you don't have to open the file to use it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC