EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

CFile class is used for opening a file. This can operate on a file handle which is already opened with Win32 CreateFile/OpenFile. However File path as string should be given to open a new file. This can be given in overloaded constructor CFile() or in Open() member call.

CFile file open

CFile();
CFile(HANDLE hFile);
CFile(LPCTSTR lpszFileName, UINT nOpenFlags);
virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL);

File open flags

nOpenFlags Sharing and access mode. Specifies the action to take when opening the file. You can combine options listed below by using the bitwise-OR (|) operator. One access permission and one share option are required; the modeCreate and modeNoInherit modes are optional. The values are as follows:

CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length.
CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.
CFile::modeRead Opens the file for reading only.
CFile::modeReadWrite Opens the file for reading and writing.
CFile::modeWrite Opens the file for writing only.
CFile::modeNoInherit Prevents the file from being inherited by child processes.
CFile::shareDenyNone 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 Opens the file and denies other processes read access to the file. Create fails if the file has been opened in compatibility mode or for read access by any other process.
CFile::shareDenyWrite Opens the file and denies other processes write access to the file. Create fails if the file has been opened in compatibility mode or for write access by any other process.
CFile::shareExclusive Opens the file with exclusive mode, denying other processes both read and write access to the file. Construction fails if the file has been opened in any other mode for read or write access, even by the current process.
CFile::shareCompat This flag is not available in 32 bit MFC. This flag maps to CFile::shareExclusive when used in CFile::Open.
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).
CFile::osNoBuffer See FILE_FLAG_NO_BUFFERING in CreateFile in the Platform SDK.
CFile::osWriteThrough See FILE_FLAG_WRITE_THROUGH in CreateFile in the Platform SDK.
CFile::osRandomAccess See FILE_FLAG_RANDOM_ACCESS in CreateFile in the Platform SDK.
CFile::osSequentialScan See FILE_FLAG_SEQUENTIAL_SCAN in CreateFile in the Platform SDK.

CFile::Open() example

This promram is a clone of DOS COPY command. It takes two arguments source file and destination file. Source file is opened with read flag and destication file is opened with write flag. It show how these open flags will be used. It copies the content of source file to destination file.
/* mycopy.cpp This console program uses CFile to copy binary files. */
/* syntax: mycopy */
#include <afx.h>
#include <afxwin.h>
#include <iostream>
using namespace std;
10  CWinApp theApp;
11 
12  int main(int argc, char *argv[])
13  {
14    if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))
15    {
16      cout << "panic: MFC couldn't initialize!" << endl;
17      return 1;
18    }
19 
20    CFile sourceFile;
21    CFile destFile;
22 
23    if (argc != 3)
24    {
25      cout << "usage: " << argv[0];
26      cout << " " << endl;
27      cout << endl;
28      return 1;
29    }
30 
31    CFileException ex;
32 
33    /* Open source file in read, shared write deny node */
34    /* file should not be in write mode by another program */
35    /* while this is open in my app. i.e. shareDenyWrite */
36     
37    if (!sourceFile.Open(argv[1],
38        CFile::modeRead | CFile::shareDenyWrite, &ex))
39    {
40      cout << "Couldn't open source file: ";
41      return 1;
42    }
43    else
44    {
45      /* Destination file is in write mode. It will erase */
46      /* existing content and write new content from the begining */
47      /* modeCreate is given in case file is not present, it will be created. */
48 
49      if (!destFile.Open(argv[2], CFile::modeWrite |
50          CFile::shareExclusive | CFile::modeCreate, &ex))
51      {
52        cout << "Couldn't open destination file: ";
53        sourceFile.Close();
54        return 1;
55      }
56 
57      BYTE buffer[4096];
58      DWORD dwRead;
59 
60      /* Content copy loop */
61      do
62      {
63        dwRead = sourceFile.Read(buffer, 4096);
64        destFile.Write(buffer, dwRead);
65      }
66      while (dwRead > 0);
67 
68      destFile.Close();
69      sourceFile.Close();
70    }
71    return 0;
72  }

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Tue 29 Apr/2014 07:51:33(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.