Results 1 to 3 of 3

Thread: Newbie trying to learn from a crappy book...

  1. #1
    Guest
    Hi,

    Could someone explain how to write some text to a file, my documentation is truly appalling.
    PS some code to write text with my Dot-matrix printer would be cool too.

    I am using the ANSI 32-bit RHIDE C++ compiler (DOS).

    Thanks

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    hEdit = handle of the window containing text
    pszFileName = filename

    Code:
    BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
    {
       HANDLE hFile;
       BOOL bSuccess = FALSE;
    
       hFile = CreateFile(pszFileName, GENERIC_WRITE, NULL, NULL,
          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
       if(hFile != INVALID_HANDLE_VALUE)
       {
          DWORD dwTextLength;
          dwTextLength = GetWindowTextLength(hEdit);
          if(dwTextLength > 0)// No need to bother if there's no text.
          {
             LPSTR pszText;
             pszText = (char*)GlobalAlloc(GPTR, dwTextLength + 1);pszText != NULL)
             {
                if(GetWindowText(hEdit, pszText, dwTextLength + 1))
                {
                   DWORD dwWritten;
                   if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                      bSuccess = TRUE;
                }
                GlobalFree(pszText);
             }
          }
          CloseHandle(hFile);
       }
       return bSuccess;
    }
    
    
    BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
    {
       HANDLE hFile;
       BOOL bSuccess = FALSE;
    
       hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
          OPEN_EXISTING, NULL, NULL);
       if(hFile != INVALID_HANDLE_VALUE)
       {
          DWORD dwFileSize;
          
             if(dwFileSize = GetFileSize(hFile, NULL);
          if(dwFileSize != 0xFFFFFFFF)
          {
             LPSTR pszFileText;
             pszFileText = (char*)GlobalAlloc(GPTR, dwFileSize + 1);
             if(pszFileText != NULL)
             {
                DWORD dwRead;
                if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
                {
                   pszFileText[dwFileSize] = 0; // Null terminator
                   if(SetWindowText(hEdit, pszFileText))
                      bSuccess = TRUE; // It worked!
                }
                GlobalFree(pszFileText);
             }
          }
          CloseHandle(hFile);
       }
       return bSuccess;
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    How's about:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main() {
        ofstream MyFile("file.txt");
        char *pcText = "Here is text";
    
        MyFile << pcText << endl;
    
        MyFile.close();
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width