Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Createfile and Sharing

Posted on 1997-04-16
1
Medium Priority
?
828 Views
Last Modified: 2011-10-03
I need to be able to reopen a file that already has handle open. I used Creatfile to open a text file and read it no problem....but I need to be able to reopen the file for writing without dropping the handling. if I drop the handle another function that is polling the file will grab it. I've tried to initially open the file with the FILE_SHARE_WRITE
attribute but this won't work. Is there anyway to Erase the file and move the file pointer back to the beginning without dropping the handle?
Here's a bit of the code that won't work

short int WriteFile(HANDLE *FHandle,char *buffer,CString                        ,*Filename,unsigned int *towrite)


      HANDLE newhandle;
      DWORD BytesWritten,faccess,fcreat,foattrib,fsmode;
      LPDWORD BWriteptr;
      BWriteptr=&BytesWritten;
      BOOL WriteSuccess,Closep,Closec;
      faccess=GENERIC_WRITE;
        fcreat=TRUNCATE_EXISTING;          
        foattrib=FILE_ATTRIBUTE_NORMAL;
        fsmode=FILE_SHARE_WRITE;
      //CloseHandle(*FHandle); in order for this to work I                                 have to close thishandle
      newhandle = CreateFile(*Filename,
                  faccess,//GENERIC_WRITE                                fsmode,//FILE_SHARE_WRITE,
                                   NULL,
                  fcreat,//TRUNCATE_EXISTING,
            foattrib,//FILE_ATTRIBUTE_NORMAL,
                                NULL);

    if ((newhandle) != (INVALID_HANDLE_VALUE))
      {
          WriteSuccess=
      WriteFile(newhandle,buffer,(*towrite),BWriteptr,NULL);            
          if (WriteSuccess)
          {
              //Closep=CloseHandle(*FHandle);
              Closec=CloseHandle(newhandle);
              if ((Closep) /*&& (Closec=)*/ )
                return 0;


Any ideas?

0
Comment
Question by:Brentg
1 Comment
 

Accepted Solution

by:
markusk earned 100 total points
ID: 1162755
If you first open the file, you should do this by:

LPCTSTR lpszFileName = "YourFile";
DWORD   fdwAccess = GENERIC_READ;     // | GENERIC_WRITE
DWORD   fdwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
DWORD   fdwCreate = OPEN_EXISTING;    // or what you want
DWORD   fdwAttrsAndFlags = FILE_ATTRIBUTE_NORMAL;
CreateFile (lpszName, fdwAccess, fdwShareMode, NULL, fdwCreate, fdwAttrsAndFlags, NULL);


The second call should look like the following code fragment:
LPCTSTR lpszFileName = "YourFile";
DWORD   fdwAccess = GENERIC_WRITE     // | GENERIC_READ
DWORD   fdwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; // you must specfiy the FILE_SHARE_READ flag because the fist call to CreateFile opened the file in GENERIC_READ mode. If not, the function call will fail.
DWORD   fdwCreate = OPEN_EXISTING;    // or what you want
DWORD   fdwAttrsAndFlags = FILE_ATTRIBUTE_NORMAL;
CreateFile (lpszName, fdwAccess, fdwShareMode, NULL, fdwCreate, fdwAttrsAndFlags, NULL);

hope this will help...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question