Skip to main content
GameDev.net gamedev.net
🔒 Locked

Determing file size

Started by aperson Feb 15, 2003 at 2:14 PM 3 replies 250+ views
Original Post
aperson
aperson
How do you determin the size of a file using stdio?
Fruny
Fruny
You should directly query the OS for the file size.
* stat() or fstat()
* GetFileSize()
* something else ?

However, using stdio
* fopen() in binary mode.
* fseek() to the end.
* ftell() returns the current file pointer position.




[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on February 15, 2003 3:21:57 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
darookie
darookie
FILE* hFile = fopen("afile", "r");
long lSize = -1;
if (hFile != 0) {
fseek(hFile, 0, SEEK_END);
lSize = ftell(hFile);
fclose(hFile);
}
Fruny
Fruny
quote:

FILE* hFile = fopen("afile", "r");



This will cause newlines to be expanded (on windows) to carriage return + newline, incorrectly reporting one extra byte for every newlines in the file.



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Alpha_ProgDes
Alpha_ProgDes
quote:
Original post by darookie
FILE* hFile = fopen("afile", "rb+");//is that correct?
long lSize = -1; //why not 0?
if (hFile != 0) {
fseek(hFile, 0, SEEK_END);
lSize = ftell(hFile);
fclose(hFile);
}



(i adjusted it, the quote)

Beginner in Game Development?  Read here. And read here.  

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.