jeyejow jeyejow - 1 year ago 319
C Question

C/C++ multiple pointers?

Ok so I've been thinking about this for some time, and i can only come up with one solution.
My problem is the following:
-> Im handeling a USB flash
-> I split its size in 3 parts
-> I have 3 threads, each one with a pointer, and the 3 parts are distributed to the tree threads;

Now this is the part that bugs my head:
If i only have one handle handeling the usb and if, for each thread, i set a pointer using the only handle i have, it will be a reall mess.

Theard 1 sets the pointer to 0;

Thread 2 sets the pointer to 100;

Thread 3 sets the pointer to 500;

The pointer will be, for each thread, at 500 because it will be the last place he will be.

I want to make so that every thread starts in a individual place.
I think the only solution to my problem is to have diferent handles for each thread, but i find that solution pretty bad and unpractic.
I would love if someone knows of a better solution to my problem!
Regardless, thanks!

This is the 2 functions im using (hanlde and the pointer):

Pointer:


DWORD WINAPI SetFilePointer(
_In_ HANDLE hFile,
_In_ LONG lDistanceToMove,
_Inout_opt_ PLONG lpDistanceToMoveHigh,
_In_ DWORD dwMoveMethod
);


Handler:

HANDLE WINAPI CreateFile(
_In_ LPCTSTR lpFileName,
_In_ DWORD dwDesiredAccess,
_In_ DWORD dwShareMode,
_In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
_In_ DWORD dwCreationDisposition,
_In_opt_ HANDLE hTemplateFile
_In_ DWORD dwFlagsAndAttributes,
);

Answer Source

You cannot have multiple file pointers to the same file (or rather, same file object). There are few ways around that:

  • open same file (device in your case) several times, that will get you independent file objects and each will have its own current position.

  • use I/O operations which don't use current position pointers, e.g. async I/O like WriteFileEx

  • (as a variation on the previous idea) use memory-mapped I/O, i.e. MapViewOfFile

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download