2.2.5.1.2 file::file

Description

Set the public data member m_hFile to a null value.

Create a File object and open the file having the specified path.

Syntax

file( )


file( LPCTSTR lpszFileName, UINT nOpenFlags )

Parameters

lpszFileName
[input] A string containing an absolute or relative path to the desired file.
nOpenFlags
[input] Specifies the share and access modes of the opened file. Modes are defined below and can be combined using the bitwise OR operator "|". At least one access and one share mode should be specified.
modeRead, Opens the file in read only mode.
modeWrite, Opens the file in write only mode.
modeReadWrite, Opens the file in read and write modes.
modeNoInherit, Opens the file but disallows inheritance by child processes.
modeCreate, Creates a new file of length 0 bytes. Pre-existing files are truncated to 0 bytes by default.
modeNoTruncate, Used in combination with modeCreate to specify that pre-existing files should not be truncated. When used together, modeCreate and modeNoTruncate open a specified file if it exists or create the specified file if it does not exist.
shareCompat, Not available...see shareExclusive below.
shareExclusive, Opens the file disallowing read and write access to all other processes. Construction fails if file is already opened in read or write mode.
shareDenyWrite, Opens the file disallowing write access to all other processes. Construction fails if file is already opened in write mode.
shareDenyRead, Opens the file disallowing read access to all other processes. Construction fails if file is already opened in read mode.
shareDenyNone, Opens the file allowing read and write access to all other processes.
typeText, Sets text mode for specified file enabling special processing of carriage return-line feed character combinations. Used in derived classes only.
typeBinary, Sets binary mode for specified file. Used in derived classes only.

Return

Examples

EX1

void file_file_ex1()
{
    char szTemp[MAXFULLPATH];
    GetTempPath(MAXFULLPATH, szTemp); // Get temp folder path.
    string strFile = szTemp + "\\test.txt";
    file ff(strFile, file::modeCreate | file::modeWrite); // Create a new write only file.
    int bb[4] = {1, 2, 3, 4};
    ff.Write(bb, sizeof(bb));
    ff.Close();
}

Remark

Creates a file object and opens the file having the path specified by the parameter lpszFileName. This constructor combines the functions of the default constructor file() and the Open member function. An exception is thrown if an error occurs while opening the file. Depending on the severity of the error, the user should be alerted by an exception handler.

See Also

file::Close, file::Open

Header to Include

origin.h