简体   繁体   English

同时写和读-C

[英]Write and Read at the Same Time - C

This is a sketch how I want to write a .txt: 这是我要编写.txt的草图:

time_t timer;
struct tm y2k = { 0 };
int seconds;
int seconds_prev= 0;
int i = 0;
y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;

FILE *f = fopen("file.txt", "w");

if (f == NULL)
{
    printf("Error opening file!\n");
    return 0;
}

for (;;){

    time(&timer);   
    seconds = difftime(timer, mktime(&y2k));

    if (seconds % 2 == 0 && seconds!=seconds_prev) {
        seconds_prev= seconds;
        const char *text = "AAAAAA";
        fprintf(f, "%d-%s\n",i, text);
        fflush(f);
        i++
        if (i == 20){
            break;
        }
    }
}

fclose(f);

This script enable writing roughly 1 line per second in a text file. 该脚本可以每秒在文本文件中写入大约1行。 Now I want to read this file until it is complete. 现在,我想读取此文件,直到完成。

FILE *file;
size_t nread;

file = fopen("file.txt", "r");
if (file) {
    /*pseudo code*/
    //(reading loop)
    //(some condition that detects the true end of file writing)
    //(buffering information)
    //(end of loop)
    /**************/

    if (ferror(file)) {
        /* deal with error */
    }
    fclose(file);
}

So the main objective is to read the "file.txt" while it is been writing. 因此,主要目的是在编写文件时读取“ file.txt”。 The big problem that I face, is the fact that the script only reads the information already written and stops the reading. 我面临的最大问题是,该脚本仅读取已写入的信息并停止读取。 I am searching some condition, flag, etc that me let resolve this problem. 我正在搜索让我解决此问题的某些条件,标志等。 So far, nothing 到目前为止,什么都没有

I think you can achieve this with named pipes . 我认为您可以使用命名管道来实现这一点。 nodejs shouldn't matter for this because as far as your programs are concerned, it's just a normal file. nodejs对此无关紧要,因为就您的程序而言,它只是一个普通文件。

Read session 阅读会议

$ mkfifo text.fifo
$ cat text.fifo

foo.c foo.c的

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
  FILE * f = fopen ("text.fifo", "w");

  for (int i = 0; i < 10; ++i)
  {
    fprintf (f, "Sup %d\n", i);
    fflush (f);
    sleep(1);
  }

  fclose(f);

  return 0;
}

Write Session 写会议

$ gcc foo.c -std=c99
$ ./a.exe
$

Read Session 阅读会议

$ mkfifo text.fifo
$ cat text.fifo
Sup 0
Sup 1
Sup 2
Sup 3
Sup 4
Sup 5
Sup 6
Sup 7
Sup 8
Sup 9

$

My ugly final solution: 我丑陋的最终解决方案:

writer: 作家:

for (;;){`

   //printing single chatacters
   char c[25] = "ABCDEFGHIJLMNOPQRSTUVXZ";

    //fprintf(f, "A character: %c\n", c[i]);
    fprintf(f, "-%c", c[i]);
    fflush(f);
    printf("-%c", c[i]);
    i++;
    _sleep(1000);
    if (i == 20){
        break;
    }

    fclose(f); 

reader: 读者:

file = fopen("file.txt", "r");

if (file) {
    //while (((nread = fread(buf, 1, sizeof buf, file)) > 0)){
    while ((((nread = fread(buf, 1, sizeof buf, file)) >= 0))){
        fwrite(buf, 1, nread, stdout);
        //_sleep(1000);

        int size= sizeof buf;
        for (int i = 0; size; i++){
            if (buf[i] == 'R') { //my [eof]
                out = 1;
                break;
            }
            if (i == 10){//preventing buf[] empty - need improvement
                break;
            }
        }
        if (out== 1) break;
    }
    if (ferror(file)) {
        printf("Bad. \n");
    }
    fclose(file);
}

Is not very efficient but works fine. 不是很有效,但是效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM