【问题标题】:Why I need times in closing files in flow为什么我需要时间来关闭流中的文件
【发布时间】:2013-09-25 07:43:24
【问题描述】:

我是 C++ 新手,我有疑问。
我有这样的代码:

    void FlowState::closeFile() {
        if (outfile) {
//from here
            struct timeval times[2];
            times[0] = myflow.tstart;
            times[1] = myflow.tstart;
            outfile->flush();
            outfile->close();
//to here
        }
    }

我对所选部分有疑问。为什么我需要这个?效果如何?

这可能会有所帮助:

class FlowState {
public:
    string flowid;
    class Streamdemux &demux;
    Flow myflow;            

    // ...

    FlowState(class Streamdemux &demux_, const Flow &flow_,
            u_int32_t isn_, bool tcp);
    virtual ~FlowState();

    // ...

};

还有:

 class Flow: public flowAddr {
    public:
        int32_t vlan;               
        struct timeval tstart;      
        struct timeval tlast;       
        uint64_t packet_count;      
        uint64_t connection_count;      
        string fileNameTemplate;

        Flow() :
                vlan(), tstart(), tlast(), packet_count(), connection_count() {
            fileNameTemplate = "%S:%s-%D:%d"; //brakuje ID połączenia
        }

        Flow(const flowAddr &flowAddr_, uint16_t vlan_,
                const struct timeval &t1, const struct timeval &t2,
                uint64_t connection_count_) :
                flowAddr(flowAddr_), vlan(vlan_), tstart(t1), tlast(t2), packet_count(
                        0), connection_count(connection_count_) {
            fileNameTemplate = "%S:%s-%D:%d";
        }

        virtual ~Flow() {
        }

    };

【问题讨论】:

  • 您不需要刷新即将关闭的文件,关闭它会自动刷新它。
  • 谁告诉你需要struct timeval 的东西?
  • 我得到了这样的代码,我想知道为什么会这样写

标签: c++ flow


【解决方案1】:

这样想:你有一个运行时间很长的程序,不时打开文件。但是,文件句柄与计算机上的所有其他内容一样,是一种有限的资源。因此,如果程序不关闭文件,它迟早会用完可用的文件句柄。

【讨论】:

  • 这部分代码怎么样:struct timeval times[2];次[0] = myflow.tstart;次[1] = myflow.tstart;为什么我需要它?
  • @akuzma 你没有,它没有做任何有用的事情。我以前可能用过,但是代码变了,现在就剩下了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 2013-04-12
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
相关资源
最近更新 更多