C ++文件操作导致“崩溃”在嵌入式Linux上 [英] C++ file operations cause "crash" on embedded Linux
问题描述
$ b $ ol
如果没有这个功能,机器可以整天工作。
我想过这个,也许是因为内存已经用完了。有了这么小的内存,是否有可能继续写一个文件?或者我应该每次写完数据后关闭它? (在这种情况下,我将不得不经常打开/关闭文件,这会导致我们的系统变慢,不好意思看到)
为我可怜的英语道歉,也许有人能理解它并给予我一些帮助。
上帝照亮你的道路,谢谢大家!
ps:我相信文件操作本身是正确的。
这样的代码:
std :: ofstream out_put;
out_put.open(filePath,std :: ofstream :: out | std :: ofstream :: trunc); $!
$ b while(!userStoped()){
doSomeMesuring(); (int itemIndex = 0; itemIndex< itemCount; ++ itemIndex){
out_put<< ','<< itemName.toStdString()<< ','
<< data-> mdata.item [itemIndex] .mvalue<< ','
<< data-> mdata.item [itemIndex] .judge<<的std :: ENDL;
}
}
out_put.close();
你写信给'out_put'从不检查流是否仍然有效。 $()&&(!userStoped())
您可以将其更改为
为了向您自己证明这是写入一个正在导致问题的流,请注释掉所有的测量代码,只是写了很多'x'(或你的字符选择!)到流,看看你是否有相同的结果。
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:
- Create/open a file before measurement begins
- In the measuring loop, when data is ready, put it into the file, then go to next measuring
- 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 << ',' << 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.
这篇关于C ++文件操作导致“崩溃”在嵌入式Linux上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!