Hi...
I saw a simmilar problem in this forum, but now i have a new problem....
I want to read string from a txt file: "myfile.txt".....
inside the file: "i want to read this text".
then I write:

LRESULT CALLBACK WindowProcedure(HWND hwnd....
HDC               hDC;
PAINTSTRUCT Ps;
HANDLE          hFile;
DWORD          n;
char               someread[24];

    switch (message)                  
    {   
        case WM_CREATE:

            hFile = CreateFile ( "myfile.txt", GENERIC_READ, 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
            ReadFile(hFile, &someread, sizeof(someread), &n, 0);
            CloseHandle ( hFile ); 
            break;
        case WM_PAINT:
             hDC = BeginPaint(hwnd, &Ps);
             TextOut(hDC,0,0,someread,24);
             EndPaint(hwnd, &Ps);
             break;
       case WM_DESTROY:......

But when i run this.......in the client area don't appear: "i want to read this text"
appear something as :"¼üAD1ýüAD2ýüAD3ýüAD4"....
I don't understand.....
ReadFile give BYTE but no char???....Unicode???...ascii???.....TextOut()????
Please help me......
Thanks.....

It's up to you to figure out what kind of text you're trying to read. All ReadFile() does is grab raw data from a file and put it in a buffer. It doesn't care whether it's binary or ASCII or Unicode or something else, it's just doing what you told it to do. :)

line 13 is wrong. You are passing a pointer to a pointer as the second parameter. Leave off the & symbol ReadFile(hFile, someread, sizeof(someread), &n, 0); Also the program should check to see that CreateFile() succeeded. Never assume success.

If myfile.txt is a standard ascii text file with lots of lines then you would be better off reading it with FILE and associated functions instead of ReadFile().

Thanks......all is fine!!!!!
my problem was other.......
The code lines are well.....
When I define the variables as global variables, it all run perfectly...

what happened was that to be local variables, at every turn loop messages their values changed randomly......

:P

Then there are bugs in your code!

> at every turn loop messages their values changed randomly
The function is called with WM_CREATE
someread contains trash, as it's an uninitialised local variable
The file read function initialises someread
The function then exits (and the changes to someread are lost)

The function is called with WM_PAINT
someread contains trash, as it's an uninitialised local variable
The uninitialised someread is displayed on screen
The function then exits

If you want to keep persistent data from one invocation of a windows message function to the next, then you either need to make that data global, or static
Like this static char someread[24];

Hi,
This is vijay bhaskar. I got the same problem when i am working with first time. But now i got the solution for this. What u have to do is......

Create the instance of the ASCIIEncoding object like this

ASCIIEncoding objEncoder = new ASCIIEncoding();

And then you can call read file function, it will return byte array containing how many bytes you want like the following way....

char[] result = new char[MAX_VALUE];
byte[] someread;
initialize "no_of_bytes_required" to some value which was depend
upon how many bytes u want.
someread = new byte[no_of_bytes_required];
int value = ReadFile(hFile, someread, no_of_bytes_required, &n, 0);

now you got the byte array containing the required no of bytes in "someread".

Now convert this byte array to Character array by uisng objEncoder, as shown below

if(value != 0) /* Checking whether ReadFile function was read characters successfully */
{
result = objEncoder.GetChars(someread);
}

Now you got the character array in the form of result.

Now convert this character array to string

string loc_string = new string(result);

Hence loc_string contains actual data.


If u have any doubts feel free to ask me and please let me know whether
it is working or not.

Regards,
Vijay Bhaskar


Hi...
I saw a simmilar problem in this forum, but now i have a new problem....
I want to read string from a txt file: "myfile.txt".....
inside the file: "i want to read this text".
then I write:

LRESULT CALLBACK WindowProcedure(HWND hwnd....
HDC               hDC;
PAINTSTRUCT Ps;
HANDLE          hFile;
DWORD          n;
char               someread[24];

    switch (message)                  
    {   
        case WM_CREATE:

            hFile = CreateFile ( "myfile.txt", GENERIC_READ, 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
            ReadFile(hFile, &someread, sizeof(someread), &n, 0);
            CloseHandle ( hFile ); 
            break;
        case WM_PAINT:
             hDC = BeginPaint(hwnd, &Ps);
             TextOut(hDC,0,0,someread,24);
             EndPaint(hwnd, &Ps);
             break;
       case WM_DESTROY:......

But when i run this.......in the client area don't appear: "i want to read this text"
appear something as :"¼üAD1ýüAD2ýüAD3ýüAD4"....
I don't understand.....
ReadFile give BYTE but no char???....Unicode???...ascii???.....TextOut()????
Please help me......
Thanks.....

commented: No comment. -3

First of all you need to use errors handling and do something like this:
//WM_CRREATE

someread[0] = 0;//make string null-terminated
hFile = CreateFile(...);
if(INVALID_HANDLE_VALUE ==hFile)
{
   MessageBox("file can not be opened",...);
}
else
{
   if(ReadFile(hFile, &someread, sizeof(someread)-1, &n, 0))   someread[n] = 0;//make null-terminated
   else   MessageBox("file can not be read",...);
  CloseHandle(hFile);
}
//WM_PAINT
hDC = BeginPaint(hwnd, &Ps);
TextOut(hDC,0,0,someread,strlen(someread));
EndPaint(hwnd, &Ps);

Try this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.