【问题标题】:opening a file (and closing it later) only if flag is set仅在设置标志时打开文件(并稍后关闭)
【发布时间】:2011-11-06 17:40:19
【问题描述】:

在我的程序中,我目前有一段看起来像这样的代码

 void foo()
 {
    // defining local variables

    for (long i =0; i<maxiterations; i++)
    {
       // here the core of the code is executed
       // for each iteration an object of a class is created and modified given the conditions imposed
    }

    if (flag) savedata();

    // and here a call to the destructor of the class is called (tested, it destroys all the previously created objects)

 }

目前savedata()如下

 void savedata()
 {
    char filenameI[1024];
    sprintf_s(filenameI, 1024, "%s_%i", filename, id);
    FILE* File;
    errno_t err;
    err = fopen_s(&File, filenameI, "w");
    if (err!=0)
    {
            cout << "\nFile" << filenameI << "could not be opened.\nPlease press Ctrl+C to terminate" << endl; // the program is run via Matlab
            cin.get();
    }
    else
    {
        cout << "Saving file " << filenameI << endl;
    }

    for (long i =0; i<maxiterations; i++)
    {
        fprintf(File, "%10li", data); //not the actual line, but fprintf is used in this way
    }

    fclose(File);

 }

由于maxiterations 是一个运行时集,并且考虑到存储单个对象所需的内存很重要(即我需要更高的值,但我达到了内存限制),我正在考虑修改以下方式:

 void foo()
 {
     // defining local variables
     if (flag) openfile();

     for (long i =0; i<maxiterations; i++)
    {
         // executing the same as before
         if (flag) savedata(i); // obviously the function would be modified
    }

    if (flag) closefile();

 }

现在,最后,我的问题:

使用相同类型的输出调用(FILE* 而不是 ofstream 对象),是否有可能实现我所需要的?

我的怀疑源于循环内的内容仅在该循环中具有作用域,因此我担心当我退出第一个 if 语句而不是调用 closefile() 时文件可能会关闭。

我错了吗?

感谢任何愿意提供帮助的人。

费德里科

【问题讨论】:

    标签: c++ visual-c++ fileoutputstream


    【解决方案1】:

    建议:

    FILE* f = NULL;
    if (flag) f = openfile();
    
     for (long i =0; i<maxiterations; i++)
        {
             // executing the same as before
             if (flag) savedata(i, f); // pass in filehandle, obviously the function would be modified
        }
    
        if (flag) closefile(f); //close file on handle passed.
    

    【讨论】:

    • 明白,谢谢。我会尝试实施它,最终我会回复你。
    • @Federico,我不明白你为什么不使用 ofstream?
    • 两个原因:1.是格式化输出,我不是很实用2.不是我原创的,我只是维护一下
    【解决方案2】:

    这将消除额外的检查:

    void foo()
    {
        // defining local variables
        if (flag)
        {
            openfile();
            for (long i = 0; i<maxiterations; i++)
            {
                // executing the same as before
                savedata(i); // obviously the function would be modified
            }
            closefile();
        }
    }
    

    【讨论】:

    • 这不会进行计算,这可能会产生我们在提供的 sn-p 中看不到的副作用。
    • @RedX:感谢您为我回答这个问题:D 未探索:必须执行 for 循环,并且不会使用您的代码。
    • @Federico 他在循环中所做的只是再次检查标志并调用 savedata(i)。不知道这和我的代码有什么不同
    • @Federico 没关系,他那里有一些关于更多代码的 cmets
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2015-03-25
    相关资源
    最近更新 更多