I have program in C language which prints some data to standard output. I used printf
for this but I need to save data to file as well so I changed it to fprintf
. Problem is, that with using fprintf, the data are in wrong order, doesn t matter, if the output is stdout or file.
if(...) {
out = fopen(param.F, "w");
} else {
out = stdout;
}
// some code here
fprintf(out, "a");
// more code
fprintf(out, "b");
....
Atfer this, output is always different. 有时,
acb
有时,
ca
...... 我尝试了各种 set制和粉碎,但没有帮助。
EDIT:我也许必须知道,我在方案中使用某些<条码>fork(),但正如我前面提到的那样,在使用印本时没有任何问题。
我的产出如下:
1. 检查
2. 客户1:
3. 客户1:进入
5: 早餐
6:客户1:备妥
4:客户2:创造
7: 结 论
8: 酒吧:检查
9:客户2:进入
10: customer 1: served
11: barber: ready
12:客户2:准备好
13: barber: finished
14:酒吧:检查
15:客户2:服务
应按数字顺序排列。 该方案的结构如下:
int main() {
pid = fork();
if(pid == 0) {
// barber printfs
} else if (pid > 0) {
// customer printfs
}
}