반응형
CFile 사용법
1. 생성
1) 생성자
CFile( );
CFile( int hFile );
CFile( LPCTSTR lpszFileName, UINT nOpenFlags );
throw( CFileException );
nOpenFlags:
CFile::modeCreate
파일을 생성한다. 만일 이미 동명의 파일이 존재한다면, 파일 크기를 0으로 만들어 생성한다.
CFile::modeNoTruncate
modeCreate option과 함께 사용된다.
동명의 파일이 없을 경우 새로 생성하고, 있다면, Open한다.
CFile::modeRead
read only 로 파일을 open한다.
CFile::modeReadWrite
읽기/쓰기 용으로 open한다.
CFile::modeWrite
write only로 파일을 open한다.
CFile::modeNoInherit
child process에의해 상속되어 지슨 ㄴ것을 방지한다.
CFile::shareDenyNone
다른 process가 파일에 접근할 수 있도록 한다.
Opens the file without denying other processes read or write access to the file.
Create fails if the file has been opened in compatibility mode by any other process.
CFile::shareDenyRead
다른 process가 이 파일을 읽을 수 없도록 하여 Open한다.
CFile::shareDenyWrite
다른 process가 이 파일을 쓰지 못하도록 하여 Open한다.
CFile::shareExclusive
다른 process가 이 파일을 읽거나 쓰지 못하도록 하여 Open한다.
CFile::shareCompat
This flag is not available in 32 bit MFC.
CFile::typeText
Sets text mode with special processing for carriage return?linefeed pairs (used in derived classes only).
CFile::typeBinary
Sets binary mode (used in derived classes only).
2) Sample
char* pFileName = "test.dat";
try
{
CFile f( pFileName, CFile::modeCreate | CFile::modeWrite );
}
catch( CFileException, e )
{
AfxMessageBox( "File Open failed" );
}
2. Open
1) Proto-Type
virtual BOOL Open( LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL );
lpszFileName: 파일명
nOpenFlags: 생성자 참조
pError: CFileException object의 pointer
return: 성공하면, 0 이 아닌 숫자. 실패하면, 0
2) Sample code
CFile f;
CFileException e;
char* pFileName = "test.dat";
if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) )
{
AfxMessageBox( "File open fail:" + e.m_cause );
}
3. Read
1) Proto-Type
☞ 64KB 미만을 읽을 경우
virtual UINT Read( void* lpBuf, UINT nCount );
throw( CFileException );
☞ 64KB 이상을 읽을 경우
DWORD ReadHuge( void* lpBuffer, DWORD dwCount );
throw( CFileException );
return: 읽혀진 data의 byte수. return 값이 nCount보다 작다면, 파일 끝에 왔다는 뜻.
2) Sample code
char pbuf[100];
UINT nBytesRead = cfile.Read( pbuf, 100 );
4. Write
1) Proto-Type
☞ 64 KB 미만을 쓸 경우
virtual void Write( const void* lpBuf, UINT nCount );
throw( CFileException );
☞ 64 KB 이상을 쓸 경우
void WriteHuge( const void* lpBuf, DWORD dwCount );
throw( CFileException );
2) Sample code
char pbuf[100];
cfile.Write( pbuf, 100 );
5. Positioning
File pointer를 옮기는 기능
1) Proto-Type
☞ 위치 이동
virtual LONG Seek( LONG lOff, UINT nFrom );
throw( CFileException );
nFrom: CFile::begin, CFile::current, CFile::end(이 경우 lOff는 음수여야 한다)
☞ 처음으로
void SeekToBegin( );
throw( CFileException );
☞ 끝으로
DWORD SeekToEnd( );
throw( CFileException );
☞ 파일 크기
virtual DWORD GetLength( ) const;
throw( CFileException );
☞ 파일 크기 변경
virtual void SetLength( DWORD dwNewLen );
throw( CFileException );
6. 닫기
virtual void Close( );
throw( CFileException );
파일을 닫지 않는 경우, destructor가 닫아 준다.
1. 생성
1) 생성자
CFile( );
CFile( int hFile );
CFile( LPCTSTR lpszFileName, UINT nOpenFlags );
throw( CFileException );
nOpenFlags:
CFile::modeCreate
파일을 생성한다. 만일 이미 동명의 파일이 존재한다면, 파일 크기를 0으로 만들어 생성한다.
CFile::modeNoTruncate
modeCreate option과 함께 사용된다.
동명의 파일이 없을 경우 새로 생성하고, 있다면, Open한다.
CFile::modeRead
read only 로 파일을 open한다.
CFile::modeReadWrite
읽기/쓰기 용으로 open한다.
CFile::modeWrite
write only로 파일을 open한다.
CFile::modeNoInherit
child process에의해 상속되어 지슨 ㄴ것을 방지한다.
CFile::shareDenyNone
다른 process가 파일에 접근할 수 있도록 한다.
Opens the file without denying other processes read or write access to the file.
Create fails if the file has been opened in compatibility mode by any other process.
CFile::shareDenyRead
다른 process가 이 파일을 읽을 수 없도록 하여 Open한다.
CFile::shareDenyWrite
다른 process가 이 파일을 쓰지 못하도록 하여 Open한다.
CFile::shareExclusive
다른 process가 이 파일을 읽거나 쓰지 못하도록 하여 Open한다.
CFile::shareCompat
This flag is not available in 32 bit MFC.
CFile::typeText
Sets text mode with special processing for carriage return?linefeed pairs (used in derived classes only).
CFile::typeBinary
Sets binary mode (used in derived classes only).
2) Sample
char* pFileName = "test.dat";
try
{
CFile f( pFileName, CFile::modeCreate | CFile::modeWrite );
}
catch( CFileException, e )
{
AfxMessageBox( "File Open failed" );
}
2. Open
1) Proto-Type
virtual BOOL Open( LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL );
lpszFileName: 파일명
nOpenFlags: 생성자 참조
pError: CFileException object의 pointer
return: 성공하면, 0 이 아닌 숫자. 실패하면, 0
2) Sample code
CFile f;
CFileException e;
char* pFileName = "test.dat";
if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) )
{
AfxMessageBox( "File open fail:" + e.m_cause );
}
3. Read
1) Proto-Type
☞ 64KB 미만을 읽을 경우
virtual UINT Read( void* lpBuf, UINT nCount );
throw( CFileException );
☞ 64KB 이상을 읽을 경우
DWORD ReadHuge( void* lpBuffer, DWORD dwCount );
throw( CFileException );
return: 읽혀진 data의 byte수. return 값이 nCount보다 작다면, 파일 끝에 왔다는 뜻.
2) Sample code
char pbuf[100];
UINT nBytesRead = cfile.Read( pbuf, 100 );
4. Write
1) Proto-Type
☞ 64 KB 미만을 쓸 경우
virtual void Write( const void* lpBuf, UINT nCount );
throw( CFileException );
☞ 64 KB 이상을 쓸 경우
void WriteHuge( const void* lpBuf, DWORD dwCount );
throw( CFileException );
2) Sample code
char pbuf[100];
cfile.Write( pbuf, 100 );
5. Positioning
File pointer를 옮기는 기능
1) Proto-Type
☞ 위치 이동
virtual LONG Seek( LONG lOff, UINT nFrom );
throw( CFileException );
nFrom: CFile::begin, CFile::current, CFile::end(이 경우 lOff는 음수여야 한다)
☞ 처음으로
void SeekToBegin( );
throw( CFileException );
☞ 끝으로
DWORD SeekToEnd( );
throw( CFileException );
☞ 파일 크기
virtual DWORD GetLength( ) const;
throw( CFileException );
☞ 파일 크기 변경
virtual void SetLength( DWORD dwNewLen );
throw( CFileException );
6. 닫기
virtual void Close( );
throw( CFileException );
파일을 닫지 않는 경우, destructor가 닫아 준다.
반응형