다음을 통해 공유


CStdioFile::CStdioFile

Constructs and initializes a CStdioFile object.

CStdioFile(); 
CStdioFile( 
   CAtlTransactionManager* pTM 
); 
CStdioFile( 
   FILE* pOpenStream  
); 
CStdioFile( 
   LPCTSTR lpszFileName, 
   UINT nOpenFlags  
); 
CStdioFile( 
   LPCTSTR lpszFileName,
   UINT nOpenFlags,
   CAtlTransactionManager* pTM 
);

Parameters

  • pOpenStream
    Specifies the file pointer returned by a call to the C run-time function fopen.

  • lpszFileName
    Specifies a string that is the path to the desired file. The path can be relative or absolute.

  • nOpenFlags
    Specifies options for file creation, file sharing, and file access modes. You can specify multiple options by using the bitwise OR (|) operator.

    One file access mode option is required; other modes are optional. See CFile::CFile for a list of mode options and other flags. In MFC version 3.0 and later, share flags are allowed.

  • pTM
    Pointer to CAtlTransactionManager object.

Remarks

The default constructor does not attach a file to the CStdioFile object. When using this constructor, you must use the CStdioFile::Open method to open a file and attach it to the CStdioFile object.

The single-parameter constructor attaches an open file stream to the CStdioFile object. Allowed pointer values include the predefined input/output file pointers stdin, stdout, or stderr.

The two-parameter constructor creates a CStdioFile object and opens the corresponding file with the given path.

If you pass NULL for either pOpenStream or lpszFileName, the constructor throws a CInvalidArgException*.

If the file cannot be opened or created, the constructor throws a CFileException*.

Example

TCHAR* pFileName = _T("CStdio_File.dat");
CStdioFile f1;
if(!f1.Open(pFileName, CFile::modeCreate | CFile::modeWrite 
   | CFile::typeText)) 
{
   TRACE(_T("Unable to open file\n"));
}

CStdioFile f2(stdout);
try
{
   CStdioFile f3( pFileName,
      CFile::modeCreate | CFile::modeWrite | CFile::typeText );
}
catch(CFileException* pe)
{
   TRACE(_T("File could not be opened, cause = %d\n"),
      pe->m_cause);
   pe->Delete();
}

Requirements

Header: afx.h

See Also

Reference

CStdioFile Class

Hierarchy Chart