00> <info> app: Reading: data.csv...
00>
00> <info> app: data.csv sucessfully opened!
00>
00> <info> app: File size: 37876 bytes
00>
00> <info> app: File successfully read!
00>
00> <info> app: 0 bytes read
我试图读取一个CSV文件,我可以写入我的北欧NRF52840。文件类型是CSV。文件本身只是一个ID值,它旁边有一些传感器/数据值。
我也希望能阅读这份文件。优选地基于ID值读取行。但我在阅读数据时遇到了问题。在我的终端中,我可以看到文件存在,并且它有一个从我的读取函数中找到的文件大小。但是,当我试图读取该文件时。它会产生0字节的读取。
下面是我阅读CSV的代码,任何提示都是非常感谢的。
void SD_CARD_Read()
{
uint16_t size;
UINT bytesRead;//From sd card driver library
while (fno.fname[0]);
ff_result = f_open(&file, FILE_NAME, FA_READ | FA_WRITE | FA_OPEN_APPEND);
if(ff_result != FR_OK)//Not passing if the file is missing
{
if (ff_result != FR_OK)
{
NRF_LOG_INFO("Unable to open or create file: " FILE_NAME ".");
SD_CARD_PRESENT = 0;
return;
}
}
else//File was openned fine
{
NRF_LOG_RAW_INFO("");
NRF_LOG_INFO("Reading: " FILE_NAME "...");
NRF_LOG_INFO(FILE_NAME" sucessfully opened!");
size = f_size(&file);
char * data = NULL;
data = malloc(size); /* allocate memory to store image data */
NRF_LOG_INFO("File size: %d bytes", size);
ff_result = f_read(&file, data, (UINT) size, &bytesRead);
if (ff_result == FR_OK){
NRF_LOG_INFO("File successfully read!");
NRF_LOG_INFO("%d bytes read", bytesRead);
for (int i=0; i < bytesRead; i++)
{
NRF_LOG_INFO("data[%d]: 0x%x", i, data[i]);
}
}
free(data); // free allocated memory when you don't need it
}
(void) f_close(&file);
return;
}
这是我终端的输出。如您所见,它标识了一个名为data.csv的文件及其大小,但不读取任何数据。
00> <info> app: Reading: data.csv...
00>
00> <info> app: data.csv sucessfully opened!
00>
00> <info> app: File size: 37876 bytes
00>
00> <info> app: File successfully read!
00>
00> <info> app: 0 bytes read
据我理解,f_read将bytesRead设置为0。我正在用FA_OPEN_APPEND打开文件。下面是传递给read函数的sdk参数:
FRESULT f_read (
FIL* fp, /* Pointer to the file object */
void* buff, /* Pointer to data buffer */
UINT btr, /* Number of bytes to read */
UINT* br /* Pointer to number of bytes read */
)
发布于 2020-03-02 16:00:06
这个答案是猜测,因为我不知道任何有关SD卡库的细节。
也许库没有单独的指针来读取和写入(附加到)文件。如果FA_OPEN_APPEND将位置设置为文件的末尾,那么f_read将不会从该位置获得任何数据。
尝试使用没有f_open的FA_OPEN_APPEND,甚至没有FA_WRITE。
ff_result = f_open(&file, FILE_NAME, FA_READ);https://stackoverflow.com/questions/60491572
复制相似问题