read text file problem

This is a discussion on read text file problem within the Windows Programming forums, part of the Platform Specific Boards category; Dear all, i ;m writing smart device c++ applcation, and here is the code: Code: HANDLE hFile = CreateFile (L"\Temp\text.txt", ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    74

    read text file problem

    Dear all,



    i ;m writing smart device c++ applcation, and here is the code:


    Code:
    HANDLE hFile = CreateFile (L"\\Temp\\text.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0,NULL);
    
    
    
    if (hFile == INVALID_HANDLE_VALUE)
    
    {
    
    DWORD dw = GetLastError ();
    
    }
    
    else
    
    {
    
    DWORD dwRead = 0;
    
    DWORD dwSize = GetFileSize (hFile, NULL);
    
     
    
    if (dwSize == -1)
    //;//Protokoll(LOG_ERROR, 0, "GetFileSize () fehlgeschlagen");
    
    else if (dwSize == 0)
    
    //;//Protokoll(LOG_ERROR, 0, "Die Datei ist leer");
    else
    
    {
    
     
    dwSize = MAX_PATH;
    
    char szFile[MAX_PATH] = "\0";
    
    
    ::ReadFile (hFile, szFile, dwSize, &dwRead, NULL);
    
    
    wchar_t szFile2[MAX_PATH] = L"\0";
    
    mbstowcs(szFile2,szFile,strlen(szFile)+1);
    
    detailvalue.AddString(szFile2);
    
    CloseHandle(hFile);
    
    }
    however the line added in the detailvalue listbox is empty line, does anyone can point out what's wrong with that?

    thanks

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    Indent properly please. That looks like tomato juice to me.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I believe you need a drive letter for CreateFile. Also, there are a few other changes as indicated below:

    Code:
    HANDLE hFile = CreateFile (L"C:\\Temp\\text.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0,NULL);
        if (hFile == INVALID_HANDLE_VALUE)
        {
            DWORD dw = GetLastError ();
            printf("CreateFile failed, error %d\n", dw);
        }
        else
        {
            DWORD dwRead = 0;
            DWORD dwSize = GetFileSize (hFile, NULL);
            if (dwSize == -1)
                printf("dwSize = -1\n"); //;//Protokoll(LOG_ERROR, 0, "GetFileSize () fehlgeschlagen");
            else if (dwSize == 0)
                printf("dwSize = 0\n");  //;//Protokoll(LOG_ERROR, 0, "Die Datei ist leer");
            else
            {
                dwSize = MAX_PATH;
                char szFile[MAX_PATH+1] = {0};            
                ::ReadFile (hFile, szFile, dwSize, &dwRead, NULL);
                wchar_t szFile2[MAX_PATH+1] = {0};
                mbstowcs(szFile2,szFile,strlen(szFile)+1);
                szFile2[MAX_PATH+1] = 0;            
                detailvalue.AddString(szFile2);
                CloseHandle(hFile);
            }
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read from file problem
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 08:33 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 09:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21