home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / apps / openfile.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  2KB  |  70 lines

  1. /*   This program demonstrates the use of the function OpenFile
  2.  *   This function creates, opens, reopens or deletes a file.  In
  3.  *   this program, "OpenFile.txt" will be opened, and the contents
  4.  *   displayed in a MessageBox.
  5.  */
  6.  
  7. #include "string.h"
  8. #include <windows.h>
  9.  
  10. #define Buf_Size 12                /* Buffer size to read with */
  11. #define Name_Size 13               /* Max length for name of external file */
  12.  
  13. int     read (int, char *, int);
  14. int     close (int);
  15.  
  16. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  17. HANDLE    hInstance, hPrevInstance;
  18. LPSTR     lpszCmdLine;
  19. int       cmdShow;
  20.   {
  21.   HANDLE hMem;
  22.  
  23.   OFSTRUCT ReOpenBuff;
  24.   int   errchk;
  25.   int   hFile;
  26.   char  *szBuf;
  27.  
  28. /***************************************************************************/
  29.  
  30.   MessageBox (NULL, (LPSTR)"About to open and read OPENFILE.TXT.",
  31.       (LPSTR)"Ready", MB_OK);
  32.  
  33.   hFile = OpenFile ( (LPSTR)"openfile.txt",   /* Open file.          */
  34.   (LPOFSTRUCT) & ReOpenBuff, OF_READ);
  35.   if (hFile == -1)
  36.     {                                      /* If not successful, say so.  */
  37.     MessageBox (NULL,
  38.         (LPSTR) "Problem opening file - OpenFile.txt",
  39.         (LPSTR) "Error",
  40.         MB_OK);
  41.     }
  42.   else
  43.     {
  44.     hMem = LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, Buf_Size);
  45.     szBuf = LocalLock (hMem);
  46.     errchk = read (hFile, szBuf, Buf_Size - 1);
  47.     szBuf[Buf_Size - 1] = '\0';
  48.  
  49.     if (errchk == -1)
  50.       {                     /* If problem while reading,   */
  51.                             /* say so.                     */
  52.       MessageBox (NULL, (LPSTR) "Problem reading file.",
  53.           (LPSTR) "ERROR", MB_OK);
  54.       }
  55.     errchk = close (hFile);
  56.     if (errchk != -1)
  57.       {                             /* If no error, print message box */
  58.                                     /* with file contents.            */
  59.       MessageBox (NULL, (LPSTR) szBuf, (LPSTR) "Success", MB_OK);
  60.       }
  61.     else
  62.       {                             /* Error closing file.           */
  63.       MessageBox (NULL, (LPSTR) "File not closed", (LPSTR) "ERROR", MB_OK);
  64.       }
  65.     }
  66.   LocalUnlock (hMem);
  67.   LocalFree (hMem);
  68.   return TRUE;
  69.   }
  70.