Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The output is not getting written into res.txt file.

C++
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<stdio.h>
#include<process.h>
using namespace std;

void wr_thread(void *pParams)
{
    std::ofstream ot("test.txt");
    for(int i=0;i<10;i++)
        ot<<i<<"\n";
}
void  rd_thread(void *pParams)
{
     char * buffer;
     long size;
     std::ofstream res("res.txt");
     srd::ifstream rd("test.txt");
     int n;
     rd.open("test.txt");
     rd>>n;
     rd.seekg(0,ifstream::end);
     size=rd.tellg();
     rd.seekg(0);

     buffer = new char [size];
     if(!rd)
     {
         cout<<"error\n";
         exit(1);
     }
     else
     {
         rd.read (buffer,size);
         res.write(buffer,size);
     }
     delete []buffer;

     res.close();
     rd.close();
}
int main()
{
     _beginthread(wr_thread, 0, NULL );
     _beginthread(rd_thread, 0, NULL );
     cin>>ip;
     return(0);
}
Posted
Updated 7-May-11 8:40am
v3
Comments
IncredibleRam 7-May-11 14:44pm    
is it working now

here are three links which can solve the problem and you can learn more from here
http://www.cplusplus.com/doc/tutorial/files/[^]
http://www.cplusplus.com/forum/general/12075/[^]
best regards.
 
Share this answer
 
v2
Hey i think problem lies here,

int main()
{
     _beginthread(wr_thread, 0, NULL );
     _beginthread(rd_thread, 0, NULL );
     cin>>ip;
     return(0);
}


You are creating two threads and you are returning from the main thread with out waiting for the child threads to do there work.Check this link& example for _beginthread()

http://msdn.microsoft.com/en-us/library/kdzttdcb.aspx[^]

Also you have to synchronize the threads when you are sharing same resources.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900