C++ File Writer-Reader application using Windows Threads Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 CPP // 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:"; // Printing the data onto the console cout << buffer << endl; // Closing the opened file fileReader.close(); return 1; } Create a Thread function for Writing data into the file CPP // 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:"; // Write the given input into the file fileWriter << buffer << endl; // Closing the opened file fileWriter.close(); return 1; } Create two threads using CreateThread function for both Writing and Reading data from the fileUse 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: CPP // 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_32Output: Comment More infoAdvertise with us Next Article C++ File Writer-Reader application using Windows Threads G gvishalsai Follow Improve Article Tags : C++ Programs Operating Systems Articles cpp-multithreading Similar Reads File Mapping in C++ Applications File mapping is a concept where a file map object can be created for a file on the disk. Thereafter, different processes can create a view of this file mapping object in their virtual address spaces. A process can create one or more views of the file mapping object in its virtual address space and w 4 min read How to work with file handling in C++ Prerequisite: File Handling through C++ Classes In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream available in fstream header file. In this post we will discuss how to store data using file handling. The idea is to take an example of Book Database and implement it the 5 min read C++ program to read file word by word Given a text file, extract words from it. In other words, read the content of file word by word. Example : Input: And in that dream, we were flying. Output: And in that dream, we were flying. Approach : 1) Open the file which contains string. For example, file named "file.txt" contains a string "gee 1 min read What is the Efficient Way of Reading a Huge Text File? In C++, reading a large text file efficiently requires a careful approach to ensure optimal performance in terms of memory usage and processing speed. In this article, we will learn how to read a huge text file efficiently in C++. Read a Large Text File Efficiently in C++The most efficient way to re 2 min read C++ Program to Read Content From One File and Write it Into Another File Here, we will see how to read contents from one file and write it to another file using a C++ program. Let us consider two files file1.txt and file2.txt. We are going to read the content of file.txt and write it in file2.txt Contents of file1.txt: Welcome to GeeksForGeeks Approach: Create an input f 2 min read Like