Advertisement
andruhovski

CFile Demo

Mar 11th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "CFileDemo.h"
  3.  
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #endif
  7.  
  8.  
  9. // The one and only application object
  10.  
  11. CWinApp theApp;
  12.  
  13. using namespace std;
  14.  
  15. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  16. {
  17.     int nRetCode = 0;
  18.  
  19.     HMODULE hModule = ::GetModuleHandle(NULL);
  20.  
  21.     if (hModule != NULL)
  22.     {
  23.         // initialize MFC and print and error on failure
  24.         if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
  25.         {
  26.             // TODO: change error code to suit your needs
  27.             _tprintf(_T("Fatal Error: MFC initialization failed\n"));
  28.             nRetCode = 1;
  29.         }
  30.         else
  31.         {
  32.             // TODO: code your application's behavior here.
  33.             char Buffer[101];
  34.             ZeroMemory(Buffer,sizeof(Buffer));
  35.             CFile File;
  36.             CFileException fe;
  37.             if (!File.Open(_T("my_file.txt"), CFile::modeRead, &fe)) {
  38.                 fe.ReportError();
  39.             return FALSE;
  40.         }
  41.         TRY {
  42.             while (File.Read(&Buffer, sizeof(Buffer)-1))
  43.             {
  44.                 printf("%s",Buffer);
  45.                 ZeroMemory(Buffer,sizeof(Buffer));
  46.             }
  47.             File.Close();
  48.         }
  49.         CATCH_ALL(e) {
  50.             File.Abort(); // will not throw an exception
  51.             e->ReportError();
  52.             return FALSE;
  53.         }
  54.         END_CATCH_ALL
  55.         }
  56.     }
  57.     else
  58.     {
  59.         // TODO: change error code to suit your needs
  60.         _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
  61.         nRetCode = 1;
  62.     }
  63.     return nRetCode;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement