Solved

Createfile and Sharing

Posted on 1997-04-16
1
801 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
markusk earned 50 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

751 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