64,683
社区成员
发帖
与我相关
我的任务
分享
HANDLE hPipeInputRead, hPipeInputWrite, hPipeOutputRead, hPipeOutputWrite;
//创建两个管道
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
//数据输入管道
CreatePipe(&hPipeInputRead, &hPipeInputWrite, &sa, 0);
//数据输出管道
CreatePipe(&hPipeOutputRead, &hPipeOutputWrite, &sa, 0);
//从文件中读取数据,写入管道ReadFile中.
const int BUFSIZE = 4096;
CHAR chBuf[BUFSIZE] = {0};
DWORD dwRead, dwWritten;
BOOL fSuccess;
HANDLE hInputFile = CreateFile("infile.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0/*FILE_ATTRIBUTE_READONLY*/, NULL);
while (true)
{
//从文件中读取数据
fSuccess = ReadFile(hInputFile, chBuf, BUFSIZE, &dwRead, NULL);
if (!fSuccess || dwRead == 0)
break;
//将数据写入管道
fSuccess = WriteFile(hPipeInputWrite, chBuf, dwRead, &dwWritten, NULL);
memset(chBuf,0,BUFSIZE); //数据清空
if (!fSuccess)
break;
}
//关闭输入数据管道
CloseHandle(hInputFile);
hInputFile = NULL;
CloseHandle(hPipeInputWrite);
hPipeInputWrite = NULL;