Trying to convert my app from file streams to file handles!

Started by
6 comments, last by Kwizatz 18 years, 11 months ago
Hi I've managed to convert most of my program from using the c FILE to using proper Windows handles. I've converted my fread to ReadFile and so on. Now part of my program requires the use of ftell() so my program knows where it is in the file. What is the Windows equivalent of this function? I've not been able to find it. The other command equivalent I'm looking for is the fseek command so I can skip past parts of a file that I'm not interested in. Any ideas?? Cheers, Steve
Advertisement
it seems your taking a step away from portability, why not use std::fstream instead? Unless you are sure you will only need to work on Win32 based systems.

Doesn't readfile just read the file into memory allocated by the user? In that case I think that it is up to you to keep track of that. Check out MSDN and search for ReadFile for more info

hth
moe.ron
moe.ron
It's somewhat unintuitively done by using SetFilePointer() with a 0 byte offset to the current position. But searching for "obtaining file pointer position" immediately lead me to this result:
MSDN: Positioning a File Pointer

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Why the heck would you want to do a thing like this?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Slight off topic, but don't convert standard ansi C IO to Windows crap. If you'd had look at Windows CRT sources you would see that these functions (fopen, fread, etc.) use core win ones (ReadFile, etc.). Basically, fread calls ReadFile to perform IO.
Quote:Original post by Kiput
Slight off topic, but don't convert standard ansi C IO to Windows crap. If you'd had look at Windows CRT sources you would see that these functions (fopen, fread, etc.) use core win ones (ReadFile, etc.). Basically, fread calls ReadFile to perform IO.

Kiput, I think you mean ReadFile calls the fread function, no?? ReadFile is not a core function.

Thanks for the replies. Look, I would love to stick with the standard i/o functions but I've found no way of discovering a file's date/time stamp using these functions. Maybe I've just not looked hard enough. The only function I found which allows you to do this requires windows file handles, hence the switch to file handles. The other is an old dos function. If someone know of a better way to find the date/time stamp using standard i/o functions I'd love to hear it. :)

Steve
Quote:Original post by Steve-B
Kiput, I think you mean ReadFile calls the fread function, no?? ReadFile is not a core function.
No, ReadFile() is implemented in Kernel32.dll. fread() is part of the CRT (so it's linked into your app usually), and just wraps around ReadFile() in a portable way. fread() also does buffering for performance reasons (4k buffers in my implementation [VS.NET 2003]). If you have the CRT source code installed as part of visual studio, look in fread.c.

Quote:Original post by Steve-B
Thanks for the replies. Look, I would love to stick with the standard i/o functions but I've found no way of discovering a file's date/time stamp using these functions. Maybe I've just not looked hard enough. The only function I found which allows you to do this requires windows file handles, hence the switch to file handles. The other is an old dos function. If someone know of a better way to find the date/time stamp using standard i/o functions I'd love to hear it. :)
I don't know of any functions offhand. The reason it's difficult to find information is that it's probably going to be platform/compiler specific. a file stream doesn't have to be a file on disk. stdin is a FILE*, but it's not a file on disk, so how would you get its creation time? Also, I think you can wrap sockets in a FILE* under unix systems.
You could take a look at the Run-Time Library Reference in particular _stat

This topic is closed to new replies.

Advertisement