C ++文件操作导致“崩溃”在嵌入式Linux上 [英] C++ file operations cause "crash" on embedded Linux

查看:309
本文介绍了C ++文件操作导致“崩溃”在嵌入式Linux上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我在一个嵌入式led测量系统项目。它使用ARM& linux,并拥有64M内存和1G存储。测量时,应该将数据写入.csv文件。我这样做了:
$ b $ ol
  • 在测量开始前创建/打开一个文件
  • 循环,当数据准备就绪后,把它放到文件中,然后进行下一步测量。 当用户停止测量时,文件将被关闭但是,当我添加这个功能,程序保持运行几个小时,然后机器不会响应任何事情(测量停止,用户界面仍然显示,但不响应任何行动等)。而csv文件大约是15MB。
    如果没有这个功能,机器可以整天工作。
    我想过这个,也许是因为内存已经用完了。有了这么小的内存,是否有可能继续写一个文件?或者我应该每次写完数据后关闭它? (在这种情况下,我将不得不经常打开/关闭文件,这会导致我们的系统变慢,不好意思看到)
    为我可怜的英语道歉,也许有人能理解它并给予我一些帮助。
    上帝照亮你的道路,谢谢大家!

    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:

    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 << ',' << 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屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆