Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I want to create a function that creates handles for reading and writing a file and returns those handles to me. Is it possible to do that? If so, could you provide a syntax for function prototype e.g. HANDLE* function()? Thanks.
Updated 18-Feb-13 21:04pm
enhzflep 19-Feb-13 3:05am    
Platform? Architecture? Windows/Linux x86/x64/Arm?
What's wrong with using good old fopen?

fopen is a part of the stdlib and is implemented in different ways for each platform/arch - it's also available anywhere a C compiler is!
YourAverageCoder 19-Feb-13 3:08am    
Windows x86.
I'm just experimenting to get a better grasp.
enhzflep 19-Feb-13 3:43am    
Okay, well to better understand the size of the question you're asking and the implications it has, consider the following points.

1) You have to read the File Allocation Table or FAT (not sure what its called under the NTFS, EXTFS or any of the other myriad of file systems)
2) Once done, you'll have a list of the sectors on disk that hold the data contained in the file.
3) You then have to implement a system that will allow a pointer to reference a specific byte offset in the file - an offset which will be in any of the given sectors.
4) You then have to buffer any sector that's changed so that you can blast the entire sector back to disk. (Disk writes happen on a per-sector basis, not a per-byte basis)
5) You have to allow for increases in file-length, which will then necessitate the use of another sector - you also have to update the FAT with the new sector.
6) Also consider that you need to handle operations which shorten the length of the file, such that the FAT is updated to reflect the sectors which are no longer consumed by the file's data.

It's probably a larger task than dissecting an elephant with a steak-knife, to be honest!

Probably the place I would look for a (more accurate and far, far) better insight than I've provided would be to look at the source code for either (a) boot-loaders or (b) operating systems.
Perhps the easiest place would be to find some source-code for a free 'Dos like' op-sys.
Grub (linux bootloader) may be a good place to investigate, too.

After the bios reads the first sector of the disk into a specific memory location, there will be code that then reads the FAT, before identifying the sectors (including head nums, clusters, and sides) occupied by the file(s) to be loaded. I forget the names of the files you'd have to search the boot-code for, they're typically hidden files and the only ones copied to the disk(ette) when it is made into a 'boot-disk'. is one of them, I think one of the others may be called msdos.sys and io.sys - but I've not tried anything remotely similar to this for nearly 20 years, so you'll forgive me (I hope!) if he memory banks have no longer retained the information in a crystal-clear fashion..

Oh, and to do so under WinXp or anything newer than Windows 3.1 or so, you'll have to write Kernel Mode code, since you can't just access an arbitrary disk head/sector in the protected environment that almost all modern OSs utilize.

Good luck, and have fun.. - I really was quite serious about the steak-knife and elephant quip btw.

EDIT: Ha! Looks like I seriously mis-judged the question's intent. C'est la vie.

1 solution

I would use a caller pre-allocated array, e.g.
bool CreateMyFiles(HANDLE handle[], INT count);

or (better!?), with STL:
bool CreateMyFiles( vector <handle> & handle );
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900