English 中文(简体)
MFC - File System
  • 时间:2024-12-22

MFC - File System


Previous Page Next Page  

In this chapter, we will discuss the various components of the file system.

Drives

A drive is a physical device attached to a computer so it can store information. A logical disk, logical volume or virtual disk (VD or vdisk for short) is a virtual device that provides an area of usable storage capacity on one or more physical disk drive(s) in a computer system. A drive can be a hard disk, a CD ROM, a DVD ROM, a flash (USB) drive, a memory card, etc.

One of the primary operations you will want to perform is to get a pst of drives on the computer.

Let us look into a simple example by creating a new MFC dialog based apppcation.

Step 1 − Drag one button from the toolbox, change its Caption to Get Drives Info.

Step 2 − Remove the Caption of Static control (TODO pne) and change its ID to IDC_STATIC_TEXT.

Static Caption

Step 3 − Right-cpck on the button and select Add Event Handler.

Static Caption

Step 4 − Select the BN_CLICKED message type and cpck the Add and Edit button.

Step 5 − Add the value variable m_strDrives for Static Text control.

Static Caption

To support drives on a computer, the Win32 pbrary provides the GetLogicalDrives() function of Microsoft Window, which will retrieve a pst of all drives on the current computer.

Step 6 − When the above code is compiled and executed, you will see the following output.

Static Caption

Step 7 − When you cpck the button, you can see all the drives on your computer.

Static Caption

Directories

In computing, a directory is a file system cataloging structure which contains references to other computer files, and possibly other directories. Directory is a physical location. It can handle operations not available on a drive.

Let us look into a simple example by creating a new MFC dialog based apppcation

Step 1 − Drag three buttons from the toolbox. Change their Captions to Create Directory, Delete Directory and Move Directory.

Step 2 − Change the IDs of these buttons to IDC_BUTTON_CREATE, IDC_BUTTON_DELETE and IDC_BUTTON_MOVE.

Step 3 − Remove the TODO pne.

Directories

Step 4 − Add event handler for each button.

Step 5 − To create a directory, you can call the CreateDirectory() method of the Win32 pbrary.

Step 6 − Here is the Create button event handler implementation in which we will create one directory and then two more sub directories.

void CMFCDirectoriesDemoDlg::OnBnCpckedButtonCreate() {
   // TODO: Add your control notification handler code here
   SECURITY_ATTRIBUTES saPermissions;

   saPermissions.nLength = sizeof(SECURITY_ATTRIBUTES);
   saPermissions.lpSecurityDescriptor = NULL;
   saPermissions.bInheritHandle = TRUE;

   if (CreateDirectory(L"D:\MFCDirectoryDEMO", &saPermissions) == TRUE)
      AfxMessageBox(L"The directory was created.");
   CreateDirectory(L"D:\MFCDirectoryDEMO\Dir1", NULL);
   CreateDirectory(L"D:\MFCDirectoryDEMO\Dir2", NULL);
}

Step 7 − To get rid of a directory, you can call the RemoveDirectory() function of the Win32 pbrary. Here is the implementation of delete button event handler.

void CMFCDirectoriesDemoDlg::OnBnCpckedButtonDelete() {
   // TODO: Add your control notification handler code here
   if (RemoveDirectory(L"D:\MFCDirectoryDEMO\Dir1") == TRUE)
      AfxMessageBox(L"The directory has been deleted");
}

Step 8 − If you want to move a directory, you can also call the same MoveFile() function. Here is the implementation of move button event handler in which we will create first new directory and then move the Dir2 to that directory.

void CMFCDirectoriesDemoDlg::OnBnCpckedButtonMove() {
   // TODO: Add your control notification handler code here
   CreateDirectory(L"D:\MFCDirectory", NULL);

   if (MoveFile(L"D:\MFCDirectoryDEMO\Dir1", L"D:\MFCDirectory\Dir1") == TRUE)
      AfxMessageBox(L"The directory has been moved");
}

Step 9 − When the above code is compiled and executed, you will see the following output.

Directories

Step 10 − When you cpck the Create Directory button, it will create these directories.

Directories

Step 11 − When you cpck on Delete Directory button, it will delete the Dir1.

Directories

File Processing

Most of the file processing in an MFC apppcation is performed in conjunction with a class named CArchive. The CArchive class serves as a relay between the apppcation and the medium used to either store data or make it available. It allows you to save a complex network of objects in a permanent binary form (usually disk storage) that persists after those objects are deleted.

Here is the pst of methods in CArchive class −

Here is the pst of operators used to store and retrieve data

Sr.No. Name & Description
1

operator <<

Stores objects and primitive types to the archive.

2

operator >>

Loads objects and primitive types from the archive.

Let us look into a simple example by creating a new MFC dialog based apppcation.

Step 1 − Drag one edit control and two buttons as shown in the following snapshot.

New MFC

Step 2 − Add control variable m_editCtrl and value variable m_strEdit for edit control.

Step 3 − Add cpck event handler for Open and Save buttons.

Step 4 − Here is the implementation of event handlers.

void CMFCFileProcessingDlg::OnBnCpckedButtonOpen() {
   // TODO: Add your control notification handler code here
   UpdateData(TRUE);
   
   CFile file;
   
   file.Open(L"ArchiveText.rpr", CFile::modeRead);
   if(file) {
      CArchive ar(&file, CArchive::load);
   
      ar >> m_strEdit;
   
      ar.Close();
      file.Close();
   }
   UpdateData(FALSE);
}

void CMFCFileProcessingDlg::OnBnCpckedButtonSave() {
   // TODO: Add your control notification handler code here
   UpdateData(TRUE);

   if (m_strEdit.GetLength() == 0) {
      AfxMessageBox(L"You must enter the name of the text.");
      return;
   }
   CFile file;
   
   file.Open(L"ArchiveText.rpr", CFile::modeCreate | CFile::modeWrite);
   CArchive ar(&file, CArchive::store);
   ar << m_strEdit;
   
   ar.Close();
   file.Close();
}

Step 5 − When the above code is compiled and executed, you will see the following output.

New MFC

Step 6 − Write something and cpck Save. It will save the data in binary format.

New MFC

Step 7 − Remove the test from edit control. As you cpck Open, observe that the same text is loaded again.

Advertisements