开发者

C++ file operations cause "crash" on embedded Linux

I'm in a embedded led measuring system project now. It uses ARM & linux, and has 64M memory and 1G storage. When measuring, it's supposed to write data to a .csv file. I did it this way:

  1. Create/open a file before measurement begins
  2. In the measuring loop, when data is ready, put it into the file, then go to next measuring
  3. When user stop the measurement, the file will be closed

But, when I add this feature, the program keep running several hours, then the machine won't respond to anything ( measuring stopped, UI still display but doesn't respond to any action, etc.). And the csv file is about 15MB. While without this feature, the machine can work well all day. I've thought about this, maybe It's because the memory is used up. With such a small memory, is it possible to keep writing a file? Or should I close it every time I finished writing data? (In that case, I will have to open/close the file very frequently, it will cause our system to be slow, what is not glad to see) Apologize for my poor English, maybe someone can understand it and give me some help. God is lighting your path, thank you all!

ps: I do believe the file operations itself is correct.

the code like this:

std::ofstream out_put;
out_put.open(filePath, std::ofstream::out | std::ofstream::trunc);

while(!userStoped()){

    doSomeMesuring();

    for(int itemIndex = 0; itemIndex < itemCount; ++itemIndex){
    out_put << ',开发者_C百科' << itemName.toStdString() << ',' 
            << data->mdata.item[itemIndex].mvalue << ',' 
            << data->mdata.item[itemIndex].judge << std::endl;
    }
}

out_put.close();


You write to 'out_put', the ofstream, but never check if the stream is still valid. You could change it to

while (out_put.good() && (!userStoped())

To prove to yourself that it is the writing to a stream which is causing the problem, comment out all of the measuring code, just write lots of 'x' (or your choice of character!) to the stream to see if you have the same result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜