-1

次のコードに小さな問題があります。ステートマシン内からクラスで呼び出しますthis->write_file(this->d_filename);。ループ内のケースは数回ヒットしますが、作成したいCSVファイルには1行のエントリしかありません。

なぜなのかわかりません。this->open(filename)書き込み関数でファイルを開きます。ファイル記述子を返します。ファイルはO_TRUNK、およびで開かれますif ((d_new_fp = fdopen(fd, d_is_binary ? "wba" : "w")) == NULL)。abaは書き込み、バイナリ、追加を指します。したがって、私は複数の行を期待しています。

fprintfステートメントは私のデータを書き込みます。また、があり\nます。

 fprintf(d_new_fp, "%s, %d %d\n", this->d_packet, this->d_lqi, this->d_lqi_sample_count);

ファイルが大きくならない理由がわかりません。

最高、マリウス

 inline bool
    cogra_ieee_802_15_4_sink::open(const char *filename)
    {
      gruel::scoped_lock guard(d_mutex); // hold mutex for duration of this function

      // we use the open system call to get access to the O_LARGEFILE flag.
      int fd;
      if ((fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC | OUR_O_LARGEFILE,
          0664)) < 0)
        {
          perror(filename);
          return false;
        }

      if (d_new_fp)
        { // if we've already got a new one open, close it
          fclose(d_new_fp);
          d_new_fp = 0;
        }

      if ((d_new_fp = fdopen(fd, d_is_binary ? "wba" : "w")) == NULL)
        {
          perror(filename);
          ::close(fd);
        }

      d_updated = true;
      return d_new_fp != 0;
    }

    inline void
    cogra_ieee_802_15_4_sink::close()
    {
      gruel::scoped_lock guard(d_mutex); // hold mutex for duration of this function

      if (d_new_fp)
        {
          fclose(d_new_fp);
          d_new_fp = 0;
        }
      d_updated = true;
    }

    inline void
    cogra_ieee_802_15_4_sink::write_file(const char* filename)
    {
      if (this->open(filename))
        {

          fprintf(d_new_fp, "%s, %d %d\n", this->d_packet, this->d_lqi,
              this->d_lqi_sample_count);
          if (true)
            {
              fprintf(stderr, "Writing file %x\n", this->d_packet);
            }
        }
    }
4

1 に答える 1

1

オープンマンO_TRUNCからの説明:

ファイルがすでに存在し、通常のファイルであり、オープンモードで書き込みが許可されている場合(つまり、O_RDWRまたはO_WRONLY)、長さ0に切り捨てられます。ファイルがFIFOまたは端末デバイスファイルの場合、O_TRUNCフラグは無視されます。それ以外の場合、O_TRUNCの効果は指定されていません。

ファイルはへの呼び出しごとに開かれ、write_file()以前に書き込まれたものはすべて削除されます。に置き換えO_TRUNCますO_APPEND

于 2012-05-30T09:58:09.437 に答える