我正在使用命名管道,因此读者和作者可以进行通信。我正在移动的文本来自一个字符串文本[行]的数组,每行包含由空格分隔的4个单词(或数字)。 读者是:
string temptext1[lines];
int bufs = 1024;
char tempchar1[bufs];
for (int l = 0; l < lines; l++)
{
ssize_t chars1 = read(rfd1, tempchar1, bufs);
if (chars1 > 0)
temptext1[l].assign(tempchar1, chars1);
tempchar1[0] = '\0';
}
作者:
wfd = open(fifoname.c_str(), O_WRONLY);
for (int p = 0; p < lines; p++)
{
write(wfd, text[p].c_str(), text[p].length());
}
close(wfd);
然而,作者的输出并不总是相同的。有时它会省略更改线路,因此输出会以块为单位。有些是按照它们的预期,其他的只有在达到缓冲区大小时才会改变行。例如:
1197332 Hunter Steven 62926191228732 Watkins Wanda 49260191248845 Raynor Martha 65254191251116 Chang Maurice 47853191265074 Cunningham Wesley 30632191273625 Vaughn Alicia 66399191277060 Doyle Kate 48450191289231 Patrick Eva 17533191291543 Hinson Sylvia 46649191311074 Fink Theodore 50564191321632 Stark Grace 64052191343213 Cooper Kristen 69265191357025 Bernstein Scott 22360191371284 Wall Erica 26280191372274 Knox Jill 44535191402568 Cox Claudia 27013191415579 Duke Gayle 39376191427406 Stein Evan 56028191442007 Barker Justin 15260191442433 Gross Robert 23075
191456852 McCall Nelson 3580
191471786 Lassiter Steven 13812
191475523 Hayes Warren 38732
191478103 Spencer Christine 46819
191516369 Skinner Lisa 51427
191522499 O'Connor Nicholas 66851
如果我在一个文件中打印数组的内容:
ofstream outofs("Taksinomimena3.txt", ios::app);
if (!outofs.is_open())
return 1;
for (int p = 0; p < lines; p++)
outofs << text[p] << "\n";
outofs.close();
所有内容都按照预期印刷。