Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Activate Template library is a collection of C++ template-based classes. It is used to create a variety of objects with the help of lightweight, efficient controls. It supports many key features like ActiveX controls, connection points, and COM features.
ATL is an effective technology that adds features to Microsoft Foundation Classes.
In this blog, we will study one of the ATL classes, i.e., CAtlTemporaryFile, which is a file-handling class.
CAtlTemporaryFile
As the name suggests, it provides the methods for the use and easy creation of the temporary file. The class provides several methods, like opening, closing, creating, etc., for opening, closing, and creating the file. The temporary file is automatically opened, named, deleted, and closed.
The file can be accessed after it is closed, and the contents of that file can be saved to a new file with a specified name.
Syntax
class CAtlTemporaryFile
You can also try this code with Online C++ Compiler
The constructors and the destructors of the CAtlTemporaryFile class are CAtlTemporaryFile::CAtlTemporaryFile and CAtlTemporaryFile::~CAtlTemporaryFile, respectively.
Public Methods
Let us take a look at the public methods provided in this class.
Close
CAtlTemporaryFile::Close: This method is called to close a file, either store the contents of the file to a new file under the specified file name or delete its contents.
Syntax
Syntax of the above method is-
HRESULT Close(LPCTSTR szNewName = NULL) throw();
You can also try this code with Online C++ Compiler
In the above method, the parameter pszDir store the path of the file. And if pszDir = NULL GetTempPathW, retrieve the path of the directory.
Return Value
Meaning
S_OK
Success
HRESULT
Failure
Flush
CAtlTemporaryFile::Flush: This method is used to push/force any data remaining in the file buffer so that it can be written to the temporary file. It is similar to the HandsOff method except that the file is not closed in this case.
Syntax
Syntax of the above method is-
HRESULT Flush() throw();
You can also try this code with Online C++ Compiler
CAtlTemporaryFile::GetPosition: This method is called to find the current file pointer position. And if you want to change the position of the file pointer, you can use Seek method of CAtlTemporaryFile.
The nLen parameter takes the number of bytes in the temporary file.
Return Value
Meaning
S_OK
Success
HRESULT
Failure
Hands_Off
CAtlTemporaryFile::HandsOff: This method is called to disconnect the temporary file from the object. Both HandsOff and HandsOn methods are used to disconnect or reattach the file from the object as per the needs.
The HandsOff method will not permanently close or delete the file; to do this, you can call the Close method; the HandsOff method will push any data remaining in the file buffer so that it can be written to the temporary file and then close the file.
Syntax
Syntax of the above method is-
HRESULT HandsOff() throw();
You can also try this code with Online C++ Compiler
CAtlTemporaryFile::HandsOn: HandsOn is one of the member functions of the CAtlTemporaryFile class. In most cases, the temporary file is deleted automatically if it moves out of the scope. Hence, because of this issue, HandsOn is mainly used when one needs to create a temporary file and does not want it to get automatically deleted.
Using this function, one can open any of the existing temporary files. This function positions the pointer at the end of the file. No parameter is required to pass to this function. It returns a HANDLE type.
Syntax
Syntax of the above method is-
HRESULT HandsOn() throw();
You can also try this code with Online C++ Compiler
CAtlTemporaryFile::LockRange: Nowadays, the security of data is one of the most important things. Also, you want to prevent some application processes from accessing a particular area of any file. LockRange helps in this situation by locking a specific area or region of a file. We need to pass the following two things to this function in order to lock a region:-
nPos: The starting position of the lock.
nCount: The length of the lock, i.e., the byte range.
More than one area can be locked, but overlapping areas are not allowed. In order to unlock, we need to use the UnlockRange function. If the adjacent areas are locked in separate calls, then both of them have to be unlocked separately.
One can also change the pointer using the Seek() function.
Return Value
Meaning
S_OK
Success
HRESULT
Failure
Seek
CAtlTemporaryFile::Seek: This method is called if you want to move the pointer of the temporary file and if you want to get the current file pointer position, call GetPosition Method.
The parameter nPos tells about the position from where the unlock should begin in the file.
The nCount parameter tells about the length of the range to be unlocked.
Return Value
Meaning
S_OK
Success
HRESULT
Failure
Write
CAtlTemporaryFile::Write: This method is called to write the data to the temporary file. The data will be written from the position that is indicated by the file pointer.
The nBufSize parameter is the number of bytes to be transferred from the buffer.
The pBuffer parameter tells the buffer having the data to be written to the temporary file.
The pnBytesWritten parameter tells the number of bytes written in the file.
Return Value
Meaning
S_OK
Success
HRESULT
Failure
Frequently Asked Questions
Name the public methods in CAtlTemporaryFile class.
The methods used in the CAtlTemporaryFile class are Flush, Create, Read, HandsOff, HandsOn, Write, Close, etc.
What is the use of the Create method in CAtlTemporaryFile?
The create method of CAtlTemporaryFile is called to make a temporary file.
What is the use of the Read method in CAtlTemporaryFile?
The Read method of CAtlTemporaryFile is used to read a particular area of a file.
Conclusion
Kudos on finishing the blog! We have discussed the CAtlTemporaryFile class in the active template library(ATL). Further, we have discussed various member functions in the CAtlTemporaryFile of the File Handling Class.
This blog has helped to enhance your knowledge of the CAtlTemporaryFile Class in the active template library. Do not stop learning! Read some of our articles on the Active template library: