Jump to content
  • Advertisement

Archived

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

elis-cool

Obtaining file length...

This topic is 5851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, im trying to get the file length but its not working...
  
bool CApp::OpenProjectHTML(char* filename)
{
	ifstream File(filename, ios::binary);
	if(!File)
	{
		File.close();
		return false;
	}

	// Get File length

        
        // if I use this then it returns the number 2, the file is

        //obvously alot longer than this...

	/*
	File.seekg(ios::end);
	int iSize = File.tellg();
	File.seekg(ios::beg);
	*/

        // if I use this then it just goes into an infinite loop...
	/*
	int iSize = -1;
	while(!File.eof()){File.seekg(++iSize);}
        File.seekg(ios::beg);
	*/

	delete[] pszProjectHTML;
	pszProjectHTML = new char[iSize];
	File.read(pszProjectHTML, iSize);
	File.close();
	return true;
}
  
CEO Platoon Studios

Share this post


Link to post
Share on other sites
Advertisement
Try this:


  

unsigned long fileSize(ifstream& inputFile)
{

streampos fileCurrentPosition;

unsigned long fileLength;


// save current file pointer in case we were in the middle

// of operating on the file before this function call


fileCurrentPosition = inputFile.tellg();

inputFile.seekg(0, ios::end);

fileLength = (unsigned long) inputFile.tellg();

inputFile.seekg(fileCurrentPosition);

return fileLength;

}




-G|aD-

Share this post


Link to post
Share on other sites
I believe that asking where the file pointer is isn''t guaranteed to do the job. If you want to do it correctly, you open the file, you read every character, and increment a counter as you do so.

Share this post


Link to post
Share on other sites
Here's a brand new function for you. I've been using it for years, and it is extremely accurate. Tells you the exact byte count of the file.

LONG GetFileSize(const char *Path)				// returns the size of the unopened file
{
HANDLE FileHandle,MapHandle;
LONG Size;
OFSTRUCT FileBuffer;
// Open the file
FileHandle = (HANDLE) OpenFile(Path, &FileBuffer, OF_READ);
if(FileHandle == (HANDLE) HFILE_ERROR) return 0;
MapHandle = CreateFileMapping(FileHandle, 0, PAGE_READONLY, 0,0,0);
if(!MapHandle)
{
CloseHandle(FileHandle);
return 0;
}
Size = GetFileSize(FileHandle,NULL);
CloseHandle(MapHandle);
CloseHandle(FileHandle);
return Size;
}


[edited by - Jiia on September 14, 2002 12:29:26 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

  
#include <io.h>

int FileSize(FILE* f)
{
return _filelength(_fileno(f));
}

Share this post


Link to post
Share on other sites
DrPizza, doH!

Jiia, that''s another way to do it, but you don''t need to map the file in order to get the file size. Also he wants to use file streams.

Anon, nice, but he needs it for file streams.

Hope this helps!


-G|aD-

Share this post


Link to post
Share on other sites
quote:
Original post by Gladiator
Try this:



    

unsigned long fileSize(ifstream& inputFile)
{

streampos fileCurrentPosition;

unsigned long fileLength;


// save current file pointer in case we were in the middle

// of operating on the file before this function call


fileCurrentPosition = inputFile.tellg();

inputFile.seekg(0, ios::end);

fileLength = (unsigned long) inputFile.tellg();

inputFile.seekg(fileCurrentPosition);

return fileLength;

}




-G|aD-
Thanks man, I tryed the File.seekg(0, ios::end); and it worked, I dont see why that did but File.seekg(ios::end); didnt... oh well

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!