How can i read a file without knowing the size?

Started by
6 comments, last by Zeke 22 years, 12 months ago
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
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
Advertisement
You can use GetFileSize to get the size of a file. See here for its documentation.
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
-Mike
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));
Damn, it seems that this forum does something with those brackets.
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/
Thanks for the info guys
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
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
// CHRIS [win32mfc]

This topic is closed to new replies.

Advertisement