📜  使用Windows线程的C++ File Writer-Reader应用程序

📅  最后修改于: 2021-05-20 06:03:52             🧑  作者: Mango

在本文中,我们将创建一个简单的Writer-Reader应用程序,该应用程序使用两个线程,一个用于写入文件,另一个用于从文件读取。在这里,我们将讨论在C / C++中使用Win32线程的方法。可以使用CreateThread()方法创建Windows线程。

方法:

  1. 创建用于从文件读取数据的线程函数
    // 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;
    }
    
  2. 创建用于将数据写入文件的线程函数
    // 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;
    }
    
  3. 使用CreateThread函数创建两个线程,以从文件写入和读取数据
  4. 使用WaitForSingleObject等待,直到指定的对象处于发信号状态或超时间隔过去。

下面是上述程序的实现:

// C++ program for File Writer-Reader
// application using Windows Threads
  
#include 
#include 
#include 
#include 
  
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;
}

使用以下命令在cmd中运行应用程序

g++ MultiThreading.cpp -lws2_32

输出:

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”