<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to read the file with f_read() function - USB Mass Storage class module</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/47333/how-to-read-the-file-with-f_read-function---usb-mass-storage-class-module</link><description>Hello guys, 
 
 I am currently playing with the usbd_msc example from SDK15.3.0 that use USB Class storage module. 
 I was able to drop some files from my PC to 8MB drive made of external QSPI flash memory on nRF52840-DK board. What confuses me now is</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 16 May 2019 10:47:59 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/47333/how-to-read-the-file-with-f_read-function---usb-mass-storage-class-module" /><item><title>RE: How to read the file with f_read() function - USB Mass Storage class module</title><link>https://devzone.nordicsemi.com/thread/187494?ContentTypeID=1</link><pubDate>Thu, 16 May 2019 10:47:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:682f5f51-53a4-40d4-8811-ba46e34bafb1</guid><dc:creator>bojan</dc:creator><description>&lt;p&gt;By exploring &lt;a href="http://elm-chan.org/fsw/ff/00index_e.html"&gt;this&lt;/a&gt; site I found more about FatFS.&lt;/p&gt;
&lt;p&gt;One way to reading a file would be to open it first with f_open() function by knowing the name of the file in advance.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;            FRESULT fr;            
            FIL file; 
            uint16_t size;
            UINT bytesRead;

            fr = f_open(&amp;amp;file, &amp;quot;eyes2.bmp&amp;quot;, FA_READ);

            if (fr == FR_OK){
                NRF_LOG_INFO(&amp;quot;File eyes2.bmp sucessfully opened!&amp;quot;);
                size = f_size(&amp;amp;file);
                char * data = NULL;

                data = malloc(size); /* allocate memory to store image data */
                NRF_LOG_INFO(&amp;quot;File size: %d bytes&amp;quot;, size);

                fr = f_read(&amp;amp;file, data, (UINT) size, &amp;amp;bytesRead);
                if (fr == FR_OK){
                    NRF_LOG_INFO(&amp;quot;File successfully read!&amp;quot;);
                    NRF_LOG_INFO(&amp;quot;%d bytes read&amp;quot;, bytesRead);
                    for (int i=0; i &amp;lt; bytesRead; i++)
                    {
                        NRF_LOG_INFO(&amp;quot;data[%d]: 0x%x&amp;quot;, i, data[i]);
                    }
                }
                free(data); // free allocated memory when you don&amp;#39;t need it

                f_close(&amp;amp;file);
            }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;In the code above, I know there is a file called &amp;quot;eyes2.bmp&amp;quot; and I was able to open it and read it. What if I don&amp;#39;t know the name of the file (the user can drop the files in folder from PC and the code should be able to read the files without having any idea about the file name)?&lt;/p&gt;
&lt;p&gt;Should I list the directory first with fatfs_ls() to find out how many files are there and after that open the particular file from the list (e.g. 5th file from the list of 10 files available in the folder)?&lt;/p&gt;
&lt;p&gt;Thanks once again.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;Bojan.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>