输出未写入res.txt文件 [英] The output is not getting written into res.txt file
本文介绍了输出未写入res.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输出未写入res.txt文件.
The output is not getting written into res.txt file.
#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);
}
推荐答案
这里有三个可以解决问题的链接,您可以从此处了解更多信息
http://www.cplusplus.com/doc/tutorial/files/ [ http://www.cplusplus.com/forum/general/12075/ [
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.
嘿,我认为问题出在这里,
Hey i think problem lies here,
int main()
{
_beginthread(wr_thread, 0, NULL );
_beginthread(rd_thread, 0, NULL );
cin>>ip;
return(0);
}
您正在创建两个线程,并且正在从主线程返回,而无需等待子线程完成工作. _beginthread()的示例
http://msdn.microsoft.com/en-us/library/kdzttdcb.aspx [ ^ ]
另外,当您共享相同的资源时,还必须同步线程.
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.
这篇关于输出未写入res.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文