如何解决在C中同时读取和写入文件的两个过程
我有两个通过文本文件进行通信的进程。 父进程不断从文件读取数据并写入shared_file。
FILE *shared_file = fopen("shared_file.txt","w");
while(fscanf(fp,"%s",currentLine) != EOF){
if (strstr(currentLine,"$GPGLL")) {
sleep(1);
fputs(data,shared_file);
fputs("\n",shared_file);
fflush(shared_file);
}
}
子进程读取新行并将其写入另一个文件。
FILE *shared_file = fopen("shared_file.txt","r");
FILE *another_file = fopen("another_file.txt","w");
char currentLine[256];
while(TRUE){
if (fgets(currentLine,sizeof(currentLine),shared_file)) {
fputs(currentLine,another_file);
fflush(another_file);
}
else{
sleep(1);
clearerr(another_file);
}
}
此代码不适用于我。子进程仅向“ another_file.txt”中写入几行。
任何线索都将不胜感激。谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。