猿问

多线程文件读写

如何用双线程对这个文件进行同步写读?
#include <iostream>
#include <fstream>
#include<windows.h>
#include<ctime>
#include<vector>
#include<mutex>
using namespace std;
vector<string>vtData = { "123","dfdfdfdfd","123","dfdfdfd"};FILE*f = nullptr;
long writePos = 0;
size_t writeLen = 0;
condition_variable writeOK;
condition_variable writeOK;
mutex m;
void writeToFile()
{
//写一行
for (size_t i = 0; i < vtData.size(); i++)
{
//vtData[i];//写入文件
writePos = ftell(f);
writeLen = vtData[i].length();
fwrite( );
fflush(f); }}void readFromFile()
{
char*s = new char[writeLen + 1];
fseek(f, writePos, SEEK_SET);
fread(s, 1, writeLen, f); printf(s);
delete s;}void main()
{


}

慕村2513822
浏览 1611回答 0
0回答
随时随地看视频慕课网APP
我要回答