Do you know that the C++ library Asio provides functionality for manipulating files? Do you know how we can access these files and get file details using functions?
In this article, we will discuss Asio files. We will see how basic files, basic random access file, and basic stream fileprovides functionalities. We will also discuss about member functions and data members that are used in Asio files. Moving forward, let’s first understand a bit about the Asio library.
What is Asio?
Asio stands for Asynchronous Input Output. Asio is a C++ library that allows processing data asynchronously. It provides asynchronous models with the latest C++ approach and basic building blocks for C++ networking and concurrency. Asio is a low-level input-output programming. Before moving further, let’s first understand how we can run our programs using the Asio library. Asio library is useful as it allows network programming in C++.
Setting Up Asio
For executing programs using the Asio library, we must need Visual Studio, while installing visual studio, select Desktop environment for C/C++. After that, we must download Asio C++ Library. After downloading both Visual Studio and Asio C++ Library, follow the below steps to set up the project using Asio on visual studio.
Create an empty project on visual studio.
2. Once the project is created, right-click on Source Files -> Add -> New Item.
3. Create a file with a .cpp extension.
4. To include the Asio library, right-click on Project name -> properties. Select C/C++ Configuration.
5. In Additional Include Directories, add the path till Asio include folder. For example, in our case the path is C:\Users\user\Downloads\asio- 1.24.0\asio-1.24.0\include
6. Now you are good to use Asio. Now let’s understand about Asio files and how we can use them.
The Asio library provides functionality to manipulate files in Asio. For manipulating stream-oriented and random access files, we can useasio::stream_file::write_only for writing in a stream-oriented file that is newly created. Whereas if you just want to read from a random access file then, asio::random_access_file::read_only can be used. Asio provides various file functionality, let’s discuss them.
basic_file
The basic_file is a class that helps in providing file functionality for both stream-oriented files and random access files. For using the basic_file class, we must include the asio/basic_file.hpp header in our program.
basic_random_access_file
The basic_random_access_file is a class that helps in providing asynchronous and blocking random-access file functionality. For using the basic_random_access_file class, we must include the asio/basic_random_access_file.hpp header in our program.
basic_stream_file
The basic_stream_file is a class that helps in providing asynchronous and blocking stream-oriented file functionality. For using the basic_stream_file class, we must include the asio/basic_stream_file.hpp header in our program.
There are certain member functions and data members which we can use in Asio Files:
Member Functions
👉 assign is used for assigning an existing native file to the file.
👉 cancel function is used for canceling all the asynchronous operations.
👉 native_handle gets the native file representation.
👉 release function helps in releasing the ownership of the underlying native file.
There are a few more functions, like resize and size, which are used for altering the file size and getting the file size, respectively. Also, for synchronization files and file data to disk, sync_all and sync_data can be used.
Data Members
👉 append helps in opening the file in append mode.
👉 sync_all_on_write allows opening a file so that writing operations synchronize file data and metadata to disk automatically.
👉 truncate helps in opening a file with truncated contents.
Let’s understand Asio files with the help of an example by using few member functions and data members.
Code
#include<iostream>
#include<asio.hpp>
#include<asio/basic_file.hpp>
using namespace std;
int main() {
asio::io_context io;
asio::random_access_file file(io);
file.open("C:/hello.cpp", asio::random_access_file::read_only);
cout << "Opening a File..." << endl;
cout << "Size of file in bytes is " ;
cout << file.size() << endl;
cout << "File is opened, So is_open() function is returning ";
cout << file.is_open() << endl;
cout << "Closing the Opened File..." << endl;
file.close();
cout << "File is Closed, So is_open() function is returning ";
cout << file.is_open() << endl;
io.run();
return 0;
}
Explanation
In the above example, A cpp file named hello.cpp is created in the C drive.
Then we are using random_access_file and setting it for only reading by using data member read_only. The function open() is used to open filewhose path is mentioned. Then we are printing the file size of the file using size() function. For checking whether the file is open or not, we can use is_open() function. In the end, we are closing the opened file using close() function and then again checking whether the file is closed or not.
Asynchronous means that a process is independent of the execution of any other process.
What is the file_base class?
File_base class is a base of both basic_stream_file and basic_random_access_file classes. For using this in our program, we can includeasio/file_base.hpp header.
What is the difference between is_open and open functions?
The is_open helps in checking whether the file is open or not. Whereas the open function helps in opening the file.
What is the full form of Asio?
Asio stands for Asynchronous Input Output.
What is the use of Asio library?
Asio helps in doing network programming using C++ language. It allows the processing of data asynchronously by providing asynchronous I/O models.
Conclusion
In this article, we have discussed Asio. We have seen how we can set up the Asio library in our machine for programming. We have also discussed Asio files, their member functions, and the data members that can be used. We have checked few functions with the help of an example. Check out our other blogs on Asio:
I hope this article has helped you in understanding about Asio library. If this article helped you in any way, then you can read more such articles on our platform, Coding Ninjas Studio. You will find articles on almost every topic on our platform. Also, you can practice coding questions at Coding Ninjas to crack good product-based companies. For interview preparations, you can read the Interview Experiences of popular companies.
Happy Coding!
Live masterclass
Google Data Analyst roadmap: Essential SQL concepts
by Maaheen Jaiswal, Data Analyst @ Google
12 Nov, 2024
01:30 PM
Amazon Data Analyst tips: Advanced Excel interview questions
by Megna Roy, Data Science & Analytics @Amazon
11 Nov, 2024
01:30 PM
Google Data Analyst roadmap: Essential SQL concepts
by Maaheen Jaiswal, Data Analyst @ Google
12 Nov, 2024
01:30 PM
Amazon Data Analyst tips: Advanced Excel interview questions