Archived

This topic is now archived and is closed to further replies.

Zeke

How can i read a file without knowing the size?

Recommended Posts

I need to open and then read a file in my mfc dialog based app. However fread() and ReadFile() both need me to specify how big the file is ("Maximum number of items to be read" for fread() and "Number of bytes to be read from the file" for ReadFile()). However the files i need the app to read may be different sizes. Can anyone offer any help as to this problem? Thanks very much Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face

Share this post


Link to post
Share on other sites
Umm, neither fread nor ReadFile require you to know how big the file is. The number you give them is how many bytes you want to read and this could be smaller (or bigger even) then the file size.

Both api''s will return the number of bytes they actually read. If this value is smaller than the number of bytes you asked for you''re probably at the end of the file.

-Mike

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Or you can read with fgetc(FILE *); until feof(FILE *);
(feof returns nonzero if file end is reached)

int i=0;

do {
data=fgetc(f);
i++;
} while(!feof(f));

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Damn, it seems that this forum does something with those brackets.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster

Or you can read with fgetc(FILE *); until feof(FILE *);
(feof returns nonzero if file end is reached)

int i=0;

do {
data=fgetc(f);
i++;
} while(!feof(f));


Do you have any idea how slow that''d be for a large file? Just use a buffer with fread, and pull in some-odd byte chunks from the file until one of them doesn''t return a item number equal to the amount of items (you could set the item to one for bytes) that you wanted to read. That''s the easiest method for the "body" portion of the file if you have no idea how big anything is.



"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/

Share this post


Link to post
Share on other sites
Here''s a function I wrote to determine if a file exists, and (if it does exist) return the file size.

  

// Returns 0xFFFFFFFF if file does NOT exist, otherwise value

// returned is the file size.


DWORD CM_FileExistsGetSize(TCHAR* pszFileName)
{
BOOL bExist = FALSE;
DWORD dwFileSize = 0xFFFFFFFF;
DWORD dwFileSizeHigh;

HANDLE hFile;

if (NULL != pszFileName)
{
// Use the preferred Win32 API call and not the older OpenFile.

hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);

if (hFile != INVALID_HANDLE_VALUE)
{
// OK, Get File Size

dwFileSize = ::GetFileSize(hFile, &dwFileSizeHigh); // Returns dwFileSize 0xFFFFFFFF on error

CloseHandle(hFile);
bExist = TRUE;
}
}

if( (bExist == FALSE) || (dwFileSize == 0xFFFFFFFF) )
return(0xFFFFFFFF);

return(dwFileSize);
}



Cheers!
// CHRIS

Share this post


Link to post
Share on other sites