Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6358323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:21:07+00:00 2026-05-24T23:21:07+00:00

I’m developing some code which reads file names from an sd-card (using FatFs) and

  • 0

I’m developing some code which reads file names from an sd-card (using FatFs) and displays them to the screen. Here’s a snipet of what I have working, this prints out the files on the card as expected –

FRESULT result;
        char *path = '/'; //look in root of sd card
        result = f_opendir(&directory, path);   //open directory
        if(result==FR_OK){
            for(;;){
                result = f_readdir(&directory, &fileInfo); //read directory
                if(result==FR_OK){
                    if(fileInfo.fname[0]==0){ //end of dir reached
                        //LCD_UsrLog("End of directory.\n");
                        break;
                    }
                    if(fileInfo.fname[0]=='.')continue; //ignore '.' files

                    TCHAR *fn_ptr; //file name, why a pointer?
                    fn_ptr=&fileInfo.fname; //get file name            
                    LCD_UsrLog("%s\n",fn_ptr);
                    for(delay=0;delay<0x0FFFFF;delay++){ShortDelay();} //delay to display

                }//end result==fr_ok
            }//end for
        }//end result==fr_ok

Where

typedef char TCHAR

and

typedef struct {
DWORD   fsize;          /* File size */
WORD    fdate;          /* Last modified date */
WORD    ftime;          /* Last modified time */
BYTE    fattrib;        /* Attribute */
TCHAR   fname[13];      /* Short file name (8.3 format) */

} FILINFO;

I need to copy the names of the files into an array for processing however I’ve tried a few ways but can’t seem to get the array working. I have tried creating an arbitrarily large array of TCHARs and dereferencing the file name pointer but this prints garbage.

FRESULT result;
        char *path = '/'; //look in root of sd card
        TCHAR fileList[50];
        u32 index=0;
        result = f_opendir(&directory, path);   //open directory
        if(result==FR_OK){
            for(;;){
                result = f_readdir(&directory, &fileInfo); //read directory
                if(result==FR_OK){
                    if(fileInfo.fname[0]==0){ //end of dir reached
                        //LCD_UsrLog("End of directory.\n");
                        break;
                    }
                    if(fileInfo.fname[0]=='.')continue; //ignore '.' files

                    TCHAR *fn_ptr; //file name, why a pointer?
                    fn_ptr=&fileInfo.fname; //get file name            

                    fileList[index]=*fn_ptr;
                    LCD_UsrLog("%s\n",fileList[index]);
                    for(delay=0;delay<0x0FFFFF;delay++){ShortDelay();} //delay to display
                    index++;
                }//end result==fr_ok
            }//end for
        }//end result==fr_ok

I suspect this is a simple mistake regarding pointers or the proper usage of an array of chars but it has been 4+ years since I’ve last touched C and I’m lost!

Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:21:07+00:00Added an answer on May 24, 2026 at 11:21 pm

    First problem: currently your file list is an array of chars, while it should be an array of strings. So declare it as

    TCHAR* fileList[50];
    

    then allocate the strings of proper length for each filename (not forgetting the extra space for the terminating 0 char). You also need to explicitly copy the filenames into your name list, because the contents of fileInfo get overwritten in each loop cycle, so simply storing the pointers would result in your list containing the name of the last file 50 times.

    All in all, you need something like this:

                    if(fileInfo.fname[0]=='.')continue; //ignore '.' files
    
                    fileList[index] = malloc(strlen(fileInfo.fname) + 1);
                    strcpy(fileList[index], fileInfo.fname);
    
                    LCD_UsrLog("%s\n",fileList[index]);
    

    (Disclaimer: No guarantee that this works as it is, I have no chance to test it, but hopefully this gives you the idea).

    Alternatively, if you know the upper limit of the filename length, you can declare an array of filenames with fixed length, and get rid of dynamic allocation. But then you should use strncpy instead of strcpy to be on the safe side, to prevent buffer overflows. And this also requires the terminating 0 character to be appended, again to be on the safe side:

    TCHAR fileList[50][MAX_FILENAME_LENGTH + 1];
    
    ...
    strncpy(fileList[index], fileInfo.fname, strlen(fileInfo.fname));
    fileList[index][MAX_FILENAME_LENGTH] = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.