How to detect SD card presence in C++ In Linux
<p>Good afternoon,</p>

<p>I'm running a simple loop for checking whether SD card has been inserted or removed, but it doesn't seem to work correctly all the time.</p>

<p>The problem is, when the program starts with SD card inserted, the <strong>if(f.good())</strong> statement is <strong>True</strong>.</p>

<p>When SD card is removed this statement is <strong>False</strong>.</p>

<p>However, when the SD card is re-inserted, this statement is still <strong>False</strong>.</p>

<p>Is there a more reliable way in C++ to detect SD card presence?
I'm Running Linux Yocto, based on OpenEmbedded.
I would prefer to avoid using any external libraries, and use file IO or system calls if possible.</p>

<p>Thanks.</p>

<p>My loop is shown below,</p>

<pre><code>    while (running)
    {
        ifstream f("/dev/mmcblk1");
        if (f.good()) {
            f.close();
            if (!mounted)
            {
                system("mount /dev/mmcblk1p1 /mnt/Storage");   
                mounted = true;
            }
            sdPresent = true;
        }
        else {
            f.close();
            sdPresent = false;
            mounted = false;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }
</code></pre>