2.2.5.1.9 file::Open

Description

Open a file returning FALSE or 0 on failure and TRUE or a non-zero on success.

Syntax

BOOL Open( 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. See the default File constructor for definitions of all supported modes. Modes can be combined using the bitwise OR operator "|". At least one access and one share mode should be specified.

Return

Returns FALSE or 0 if the file fails to open and returns TRUE or a non-zero if the file opens successfully.

Examples

EX1

void file_open_ex1()
{
    file ff;
    //Get user file folder
    string strFile = GetAppPath() + "OriginC" + "\\test.txt";
    
    //create a new write only file.
    BOOL bOK = ff.Open(strFile, file::modeCreate | file::modeWrite); 
    int bb[4] = {1, 2, 3, 4};
    ff.Write(bb, sizeof(bb));
    ff.Close();
    
    //open the file created previously and set it to read only file.
    bOK = ff.Open(strFile, file::modeRead | file::shareDenyWrite );            
    int aa;
    ff.Read(&aa, sizeof(aa));        //aa equals 1 now.
    printf("%d\n",aa);
    ff.Close();
}

EX2

//Get temp folder path.
void file_open_ex2()
{
    file ff;
    char sztemp[MAXFULLPATH];
    DWORD cc = GetTempPath(MAXFULLPATH, sztemp);
    string str1, strFile;
    str1 = sztemp;
    strFile = str1 + "\\test.txt";
    BOOL bOK = ff.Open(strFile, file::modeCreate | file::modeReadWrite | file::typeText);
    string		str("ABC");	//Write text type "ABC" to test.txt
    ff.Write(str.GetBuffer(0), 3);
    str.ReleaseBuffer();
    ff.Close();
}

EX3

//This console program is used to copy files.
int file_open_ex3()
{
    char sztemp[MAXFULLPATH];	//Get temp folder path
    DWORD aa = GetTempPath(MAXFULLPATH, sztemp);
    string str1, strFile, strFile2;
    str1 = sztemp;
    strFile = str1 + "\\test.txt";
    strFile2 = str1 + "\\dest.txt";
 
       // constructing these file objects doesn't open them
       file sourceFile;
       file destFile;
 
       // open the source file for reading
       bool bOK = sourceFile.Open(strFile, file::modeCreate | file::modeNoTruncate | file::modeRead | file::shareDenyWrite);
       if(!bOK)
       {
           out_str("failed to open the source file");
           return 0;
       }
 
       // open the destination file for writing
       bOK = destFile.Open(strFile2, file::modeWrite | file::shareExclusive | file::modeCreate);
       if(!bOK)
       {
           out_str("failed to open the destination file");
           return 0;
       }
 
       BYTE buffer[196];
       DWORD dwRead;
 
    // Read in 196-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, 196);
         destFile.Write(buffer, dwRead);
      }
      while (dwRead > 0);
 
      // Close both files
      destFile.Close();
      sourceFile.Close();
 
      return 1;
}

Remark

A safe method of opening a file when failure is a likely outcome. The Open member function should be called after the default File constructor has been invoked. It is a safe method of opening a file because it returns FALSE or 0 on failure instead of throwing an exception. When a file is opened the current pointer position is initialized to 0.

See Also

file::file, file::Close

Header to Include

origin.h