返回首页

您好,

我有下面的代码片段:


#include "TimeUtils.hpp"



FILE *output;                   // internal filename



int main(int argc, char *argv[])

{

 

//?

//More code

//?



time01 = time_now(); // Returns the time now

    while(1)

    {

        time02 = time_now(); // Returns the time now

        static long count  = 0;

        count  = (time02-time01)/1000; // Number of seconds since time01?


        Rangek.runge4(count, y, dist); // Method executed in real time [Is this the way to do this?]



/*

My problem:

	I want to print to file output?only once per second.

	How do I do that?

*/

        if((count%2) != 0)

        {

            fprintf(output, "%f\t%f\n", count, y[0]);

            continue;

        }

 

        cout << "Counting: " << count << " seconds\n";

 

        if( ((time02-time01)/1000) >= 10) break; // Stop after 10 second

    }

 



    fclose(output);

 

return 0;

 

}

我应该看到我的文件只有10线,但我有几百行
我,我可以使它每秒打印一次?

感谢你的帮助。

的问候,

埃尔韦| hervebags

回答

评论会员: 时间:2
它看起来像你写的每一个迭代的文件(计数2%)是真正添加变量称为lastcount的尝试,并设置它等于-1。然后,如果计数和lastcount是不同的,打印的文件,使lastcount计数。codeprelang="c++"spanclass="code-keyword"if/span(lastcount!=count){fprintf(output,spanclass="code-string""/spanspanclass="code-string"%f\t%f\n"/span,count,y[spanclass="code-digit"0/span]);lastcount=count;spanclass="code-keyword"continue/span;}/pre/code:理查德MacCutchan
{的C2}什么是应该做的,是什么类型和y和区?
值{体C3}计数%2,将产生非零为奇数,所以这将是后1秒,3秒等;,所以每两秒钟
与fprintf()语句使用%f格式控制字符打印一个浮点值,但变量count是一个长整数。我不知道什么Y [0]可能。
continue语句追溯到循环的顶部,并得到了一次又一次。这将可能相当接近最后一次迭代,因此它会调用当前一秒内第二次与fprintf()语句。