C++网络编程 , page75 中的这个示例:
#include "ace/ACE.h"
#include "ace/OS.h"
#include "ace/Message_Block.h"
int main(int argc, char* argv[])
{
ACE_Message_Block* head = new ACE_Message_Block(BUFSIZ);
ACE_Message_Block* mblk = head;
for(;;)
{
ssize_t nbytes = ACE::read_n( ACE_STDIN, mblk->wr_ptr(), mblk->size() );
if( nbytes <= 0 )
break; //break out at EOF or error.
//Allocate message block and chain it at the end of list.
mblk->cont( new ACE_Message_Block(BUFSIZ) );
mblk = mblk->cont();
}
for( mblk = head; mblk != 0; mblk = mblk->cont() )
ACE::write_n( ACE_STDOUT, mblk->rd_ptr(), mblk->length() );
head->release(); //This releases all the memory in the chain.
return 0;
}
这个程序怎么用啊.//break out at EOF or error.
程序怎么输入才能自己结束啊 . 别告诉我是 ctrl+Z,这个我知道. 但如果是这样的话怎么看结果呢.

最新回复
这是书上说的啊。
现在,运行程序后我输入数据, 但是结束不了输了。 程序中的提示是//break out at EOF or error. 或是怎么才能使输入让程序识别EOF呢。 不知道所以问大家。
其在结束不了输入就用了ctrl+z.
不过要保证1.txt文件大于BUFSIZ,因为ACE::read_n函数的实现中对于小于BUFSIZ部分的内容无法读取,不知道这算不算一个bug。
#include "ace/ACE.h"
#include "ace/OS.h"
#include "ace/Message_Block.h"
int main(int argc, char* argv[])
{
ACE_HANDLE hFile = open("/home/1.txt", O_RDONLY);
if (ACE_INVALID_HANDLE == hFile)
{
perror("open file");
return -1;
}
ACE_Message_Block* head = new ACE_Message_Block(BUFSIZ);
ACE_Message_Block* mblk = head;
for(;;)
{
ssize_t nbytes = ACE::read_n(hFile/* ACE_STDIN*/, mblk->wr_ptr(), mblk->size() );
if( nbytes <= 0 )
break; //break out at EOF or error.
//Allocate message block and chain it at the end of list.
mblk->cont( new ACE_Message_Block(BUFSIZ) );
mblk = mblk->cont();
}
for( mblk = head; mblk != 0; mblk = mblk->cont() )
ACE::write_n( ACE_STDOUT, mblk->rd_ptr(), mblk->length() );
head->release(); //This releases all the memory in the chain.
return 0;
}
[ 本帖最后由 cruel 于 2008-6-28 21:53 编辑 ]