Share via


CFile::Open

オーバーロードされます。Open 関数は、既定の CFile コンストラクターと合わせて使うようにデザインされています。

virtual BOOL Open(
   LPCTSTR lpszFileName,
   UINT nOpenFlags,
   CFileException* pError = NULL 
);
virtual BOOL Open(
   LPCTSTR lpszFileName,
   UINT nOpenFlags,
   CAtlTransactionManager* pTM,
   CFileException* pError = NULL
);

パラメーター

  • lpszFileName
    目的のファイルのパスを示す文字列。相対パス、絶対パス、またはネットワーク名 (UNC) を指定できます。

  • nOpenFlags
    ファイルの共有モードとアクセス モードを定義する UINT。このパラメーターは、ファイルを開くときに実行するアクションを指定します。オプションをビットごとの OR (|) 演算子を使用して、複数のオプションを指定できます。アクセス許可モードと共有モードは、それぞれ 1 つは指定する必要があります。modeCreate モードと modeNoInherit モードはオプションです。モード オプションの一覧については、CFile コンストラクターの説明を参照してください。

  • pError
    操作が失敗した場合にステータス情報を受け取るファイル例外オブジェクトへのポインター。

  • pTM
    CAtlTransactionManager オブジェクトへのポインター。

戻り値

ファイルが正常に開かれた場合は 0 以外。それ以外の場合は 0。pError パラメーターは、0 が返された場合にのみ意味を持ちます。

解説

2 つの関数を使うと、ファイルを開くときに、失敗する可能性があっても "安全に" 操作できます。

エラーが発生した場合、CFile コンストラクターは例外をスローしますが、OpenFALSE を返します。しかし、Open は、エラーを説明するために CFileException オブジェクトを初期化できます。パラメーター pError を指定しなかった場合、またはパラメーター pError に NULL を渡した場合は、OpenFALSE を返し、CFileException はスローしません。既存の CFileException へのポインターを渡すと、エラーが発生した場合、Open 関数はエラーを説明する情報を補います。いずれにしても、Open が例外をスローすることはありません。

Open を呼び出した場合の結果を次の表で説明します。

pError

エラー発生の有無

戻り値

CFileException の内容

NULL

いいえ

TRUE

適用なし

CFileException へのポインター

いいえ

TRUE

変更なし

NULL

はい

FALSE

適用なし

CFileException へのポインター

はい

FALSE

エラーを説明するために初期化

使用例

CFile f;
CFileException e;
TCHAR* pszFileName = _T("Open_File.dat");
if(!f.Open(pszFileName, CFile::modeCreate | CFile::modeWrite, &e))
{
   TRACE(_T("File could not be opened %d\n"), e.m_cause);
}
//A second example for CFile::Open.
//This function uses CFile to copy binary files.
bool BinaryFileCopy(LPCTSTR pszSource, LPCTSTR pszDest)
{
   // constructing these file objects doesn't open them
   CFile sourceFile;
   CFile destFile;

   // we'll use a CFileException object to get error information
   CFileException ex;

   // open the source file for reading
   if (!sourceFile.Open(pszSource,
      CFile::modeRead | CFile::shareDenyWrite, &ex))
   {
      // complain if an error happened
      // no need to delete the ex object

      TCHAR szError[1024];
      ex.GetErrorMessage(szError, 1024);
      _tprintf_s(_T("Couldn't open source file: %1024s"), szError);
      return false;
   }
   else
   {
      if (!destFile.Open(pszDest, CFile::modeWrite |
         CFile::shareExclusive | CFile::modeCreate, &ex))
      {
         TCHAR szError[1024];
         ex.GetErrorMessage(szError, 1024);
         _tprintf_s(_T("Couldn't open source file: %1024s"), szError);

         sourceFile.Close();
         return false;
      }

      BYTE buffer[4096];
      DWORD dwRead;

      // Read in 4096-byte blocks,
      // remember how many bytes were actually read,
      // and try to write that many out. This loop ends
      // when there are no more bytes to read.
      do
      {
         dwRead = sourceFile.Read(buffer, 4096);
         destFile.Write(buffer, dwRead);
      }
      while (dwRead > 0);

      // Close both files

      destFile.Close();
      sourceFile.Close();
   }

   return true;
}

必要条件

ヘッダー : afx.h

参照

関連項目

CFile クラス

階層図

CFile::CFile

CFile::Close