Hi I updated my file so that the readfile would re-open the file. But
even though it now shows a value of a successful reading, the
strContent does not fill with any type of string. Any suggestions?


////// Store Date in memory (file) //////////////////////////
HANDLE hanFile;
CString x = "Quit.dat";
hanFile = CreateFile(x, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL ); //successful
CString y = "Test Input String";
DWORD dw = 0;
BOOL t;
t = WriteFile(hanFile, x, 500, &dw,NULL);
CloseHandle(hanFile);


/// test read from file ///
//open file
hanFile = CreateFile(x, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN, NULL );
CString strContent;
int i;
i = ReadFile(hanFile, &strContent, 500, &dw, NULL); //dw returns set
to 500,
//strContent still ""
CloseHandle(hanFile);
MessageBox(strContent);

Re: readfile evc by Michael

Michael
Tue Jan 20 10:20:58 CST 2004

Change "CString strContent" to "CHAR strContent[500]". And "&strContent" to
"strContent".

Btw, there doesn't seem to be any benefit to using CString (at least in your
example code). Why not just use CHAR (in the write case, too)? Just an
idea.

Also, you are writing CHARs. So you'll be reading CHARs, too. But the
MessageBox call wants WCHAR *. So you've got a problem there, too. You'd
need to convert from CHAR to WCHAR (MultiByteToWideChar() function).

If you want to have a UNICODE file (made up of WCHARs), you should also
write a byte-order mark at the beginning of the file (look up UNICODE BOM in
MSDN).
--

Michael Salamone [eMVP]
Entrek Software, Inc.
www.entrek.com


"x" <aotemp@hotmail.com> wrote in message
news:26d721d9.0401200759.6ec89a8e@posting.google.com...
> Hi I updated my file so that the readfile would re-open the file. But
> even though it now shows a value of a successful reading, the
> strContent does not fill with any type of string. Any suggestions?
>
>
> ////// Store Date in memory (file) //////////////////////////
> HANDLE hanFile;
> CString x = "Quit.dat";
> hanFile = CreateFile(x, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
> CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL ); //successful
> CString y = "Test Input String";
> DWORD dw = 0;
> BOOL t;
> t = WriteFile(hanFile, x, 500, &dw,NULL);
> CloseHandle(hanFile);
>
>
> /// test read from file ///
> //open file
> hanFile = CreateFile(x, GENERIC_READ, FILE_SHARE_READ, NULL,
> OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN, NULL );
> CString strContent;
> int i;
> i = ReadFile(hanFile, &strContent, 500, &dw, NULL); //dw returns set
> to 500,
> //strContent still ""
> CloseHandle(hanFile);
> MessageBox(strContent);



Re: readfile evc by Trevor

Trevor
Tue Jan 20 10:30:45 CST 2004

x,

You are using a CString as a dynamically allocating buffer, which it is not.
One approach is to allocate your own buffer. For example:

char buffer[1024]; // 1kb of data space
HANDLE hanFile = INVALID_HANDLE_VALUE;
CString file = "Quit.dat";
hanFile = CreateFile(...);
if (hanFile != INVALID_HANDLE_VALUE)
{
strcpy(buffer, "Hello World"); // Copy "Hello World" to buffer
WriteFile(hanFile, buffer, strlen(buffer), ....); // Only write the length
of the string no more no less.
CloseHandle(hanFile);
}

When you read it:

char buffer[1024]; // 1kb of data space
HANDLE hanFile = INVALID_HANDLE_VALUE;
CString file = "Quit.dat";
hanFile = CreateFile(...);
if (hanFile != INVALID_HANDLE_VALUE)
{
ReadFile(hanFile, buffer, sizeof(buffer), ....); // Only read as many bytes
as buffer can hold
CloseHandle(hanFile);
MessageBox(NULL, buffer, NULL, 0);
}

If you have some question about this go ahead and post a new question.
Understanding buffers is pretty important. Are you aware of the class
CFile? It does everything you are trying to do here. You still need to
allocate your own buffer though.

"x" <aotemp@hotmail.com> wrote in message
news:26d721d9.0401200759.6ec89a8e@posting.google.com...
> Hi I updated my file so that the readfile would re-open the file. But
> even though it now shows a value of a successful reading, the
> strContent does not fill with any type of string. Any suggestions?
>
>
> ////// Store Date in memory (file) //////////////////////////
> HANDLE hanFile;
> CString x = "Quit.dat";
> hanFile = CreateFile(x, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
> CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL ); //successful
> CString y = "Test Input String";
> DWORD dw = 0;
> BOOL t;
> t = WriteFile(hanFile, x, 500, &dw,NULL);

You try to write 500 bytes from x, but there are only 8 bytes in x.
Try this:

t = WriteFile(hanFile, (LPCTSTR)x, x.GetLength(), &dw, NULL);

> CloseHandle(hanFile);
>
>
> /// test read from file ///
> //open file
> hanFile = CreateFile(x, GENERIC_READ, FILE_SHARE_READ, NULL,
> OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN, NULL );
> CString strContent;
> int i;
> i = ReadFile(hanFile, &strContent, 500, &dw, NULL); //dw returns set
> to 500,

Again, CString is not meant to be used like this.

> //strContent still ""
> CloseHandle(hanFile);
> MessageBox(strContent);