#include <fstream>
#include <iostream>
#include <string.h>
#include <winsock2.h>
using
namespace
std;
DWORD
WINAPI ReadFromAFile(
PVOID
lpParam)
{
char
buffer[1024] = { 0 };
ifstream fileReader;
fileReader.open("sample.txt");
cout << "Reading data from the file:" << endl;
fileReader >> buffer;
cout << buffer << endl;
fileReader.close();
return
1;
}
DWORD
WINAPI WriteIntoAFile(
PVOID
lpParam)
{
char
buffer[1024] = { 0 };
ofstream fileWriter;
fileWriter.open("sample.txt");
cout << "Enter data to write "
<< "into the file:"
<< endl;
cin >> buffer;
fileWriter << buffer << endl;
fileWriter.close();
return
1;
}
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:
t1 = CreateThread(NULL, 0,
WriteIntoAFile,
&fileWriter,
0, &tid);
WaitForSingleObject(t1, INFINITE);
t2 = CreateThread(NULL, 0,
ReadFromAFile,
&fileReader,
0, &tid);
WaitForSingleObject(t2, INFINITE);
break
;
case
2:
cout << "Thank you
for
using
"
<< " the application"
<< endl;
flag = 0;
break
;
default
:
cout << "Enter a valid query!!"
<< endl;
}
}
return
0;
}