C++ File Writer-Reader application using Windows Threads
In this article, we will create a simple Writer-Reader application, which uses two threads, one for Writing into the file and another for Reading from the file. Here we will discuss the approach using Win32 Threads in C/C++. A windows thread can be created using the CreateThread() method.
Approach:
- Create a Thread function for Reading data from the file
// Thread function used to Read data from the fileDWORDWINAPI ReadFromAFile(PVOIDlpParam){// Create a buffercharbuffer[1024] = { 0 };// Creating ifstream objectifstream fileReader;// Opening the file in read modefileReader.open("sample.txt");// Reading the data into the buffercout <<"Reading data from the file:";// Printing the data onto the consolecout << buffer << endl;// Closing the opened filefileReader.close();return1;}chevron_rightfilter_none - Create a Thread function for Writing data into the file
// Thread function used to Write data into the fileDWORDWINAPI WriteIntoAFile(PVOIDlpParam){// Create a buffercharbuffer[1024] = { 0 };// Creating ofstream objectofstream fileWriter;// Opening the file in write modefileWriter.open("sample.txt");cout <<"Enter data to write into the file:";// Write the given input into the filefileWriter << buffer << endl;// Closing the opened filefileWriter.close();return1;}chevron_rightfilter_none - Create two threads using CreateThread function for both Writing and Reading data from the file
- Use WaitForSingleObject to wait until the specified object is in the signaled state or time out interval elapses.
Below is the implementation of the above program:
// C++ program for File Writer-Reader // application using Windows Threads #include <fstream> #include <iostream> #include <string.h> #include <winsock2.h> using namespace std; // Thread function used to Read data from the file DWORD WINAPI ReadFromAFile(PVOID lpParam) { // Create a buffer char buffer[1024] = { 0 }; // Creating ifstream object ifstream fileReader; // Opening the file in read mode fileReader.open("sample.txt"); // Reading the data into the buffer cout << "Reading data from the file:" << endl; fileReader >> buffer; // Printing the data onto the console cout << buffer << endl; // Closing the opened file fileReader.close(); return 1; } // Thread function used to Write data into the file DWORD WINAPI WriteIntoAFile(PVOID lpParam) { // Create a buffer char buffer[1024] = { 0 }; // Creating ofstream object ofstream fileWriter; // Opening the file in write mode fileWriter.open("sample.txt"); cout << "Enter data to write " << "into the file:" << endl; cin >> buffer; // Write the given input into the file fileWriter << buffer << endl; // Closing the opened file fileWriter.close(); return 1; } // Driver code int main() { WSADATA WSAData; char buffer[1024]; DWORD tid; ofstream fileWriter; ifstream fileReader; HANDLE t1, t2; int choice, flag = 1; while (flag) { cout << "================================" << "========================" << "==================" << endl; cout << "Select your option" << "\t1.Run the application " << "\t2.Exit" << endl; cin >> choice; switch (choice) { case 1: // Create the first thread for Writing t1 = CreateThread(NULL, 0, WriteIntoAFile, &fileWriter;, 0, &tid;); WaitForSingleObject(t1, INFINITE); // Create the second thread for Reading t2 = CreateThread(NULL, 0, ReadFromAFile, &fileReader;, 0, &tid;); WaitForSingleObject(t2, INFINITE); break; case 2: // Exiting the application cout << "Thank you for using" << " the application" << endl; flag = 0; break; default: // For any query other than 1 and 2 cout << "Enter a valid query!!" << endl; } } return 0; } |
Run the application in the cmd using the command:
g++ MultiThreading.cpp -lws2_32
Recommended Posts:
- Compare file system in Windows and Linux
- Sharing a queue among three threads
- Print 1 2 3 infinitely using threads in C
- What are Threads in Computer Processor or CPU?
- Threads and its types in Operating System
- Two way communication between Client and Server using Win32 Threads
- Producer-Consumer solution using threads in Java
- Difference between DOS and Windows
- Difference between Linux and Windows
- Microsoft Windows (10) Vs macOS (Mojave)
- Distributed Application Paradigms
- Difference between Application Software and Operating System
- Getting Started with Cross-Platform Mobile Application using Flutter
- Chat application between two processes using signals and shared memory
- Full screen OpenCV / GtK application in C++ running on Raspberry PI
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


