Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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.
Posted 18-Feb-13 20:55pm
Edited 18-Feb-13 21:04pm
v2
Comments
enhzflep at 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 at 19-Feb-13 3:08am
   
Windows x86.
I'm just experimenting to get a better grasp.
enhzflep at 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'. command.com 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 );
  Permalink  
v2

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

  Print Answers RSS
0 CPallini 365
1 Sergey Alexandrovich Kryukov 277
2 OriginalGriff 165
3 George Jonsson 159
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,860
2 CPallini 5,125
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100