Thread: read text file problem

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,058
    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, 05:02 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10: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, 10:01 AM