为什么我需要时间关闭流中的文件

Why I need times in closing files in flow

本文关键字:文件 时间 为什么      更新时间:2023-10-16

我是 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() {
        }
    };

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