复制从一个指针字符串到字符串 [英] Copying a string from a pointer to a string

查看:159
本文介绍了复制从一个指针字符串到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一些code从SD卡(使用FATFS)读取文件的名称,并将其显示在屏幕上。下面是我的工作是什么,这种打印出预期的卡上的文件snipet -

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

其中,

typedef char TCHAR

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;

} FILINFO;

我需要将文件名复制到进行处理,但是我已经尝试了一些方法,但似乎无法得到阵列工作的数组。我曾尝试创建一个任意大阵的tchars和取消引用的文件名的指针,但这打印不需要的。

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

我怀疑这是关于指针或字符数组的正确使用一个简单的错误,但它已经4年以上,因为我已经触及去年C和我迷路了!

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!

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

推荐答案

第一个问题:目前你的文件列表是字符数组,而它应该是的字符串数组的。所以把它声明为

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];

然后分配合适长度的字符串每个文件名(不要忘记为终止0字符的额外空间)。您还需要对文件名明确地复制到你的名字列表,因为的fileInfo 的内容,获得每个循环覆盖,因此简单地贮存指针将导致包含列表最后一个文件名的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).

另外,如果你知道文件名长度的上限,可以声明定长文件名的数组,并摆脱动态分配。但你应该使用函数strncpy 而不是的strcpy 的是在安全方面,以prevent缓冲区溢出。这也要求终止0字符被追加,又是在安全方面:

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';

这篇关于复制从一个指针字符串到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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