944,162 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 23358
  • C RSS
Dec 29th, 2005
0

File size in C (Win32)

Expand Post »
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 ?
Similar Threads
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Dec 29th, 2005
0

Re: File size in C (Win32)

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. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 29th, 2005
0

Re: File size in C (Win32)

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Hook Alert
Next Thread in C Forum Timeline: having a problem with this simple program am going crazy





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC