读写sd卡代码分析(vivado sdk c++)

sdReader.cc

void ReadFloatsFromSDFile(float *weightsFromFile, const std::string file_name) 
{
    FIL fil;        /* File object */
    FATFS fatfs;
    FILINFO file_info;
    char *SD_File;
    FRESULT Res;
    UINT NumBytesRead;

    Res = f_mount(&fatfs, "0:/", 0);
    if (Res != FR_OK) printf( "Could not mount SD card.");

    //printf("SD Card Mounted successfully\n");
    SD_File = (char *)file_name.c_str();

    Res = f_open(&fil, SD_File, FA_READ | FA_OPEN_EXISTING);
    if (Res) throw Res;
    //printf("FILE Opened successfully\n");

    Res = f_lseek(&fil, 0);
    if (Res) throw "Failed to seek opened file.";



    Res = f_stat(SD_File, &file_info);
    //DWORD fil_size = file_info.fsize+1;

    //printf("Size: %u\n",file_info.fsize);
    Res = f_stat(SD_File, &file_info);
    //DWORD fil_size = file_info.fsize+1;
    for(int i = 0; i < 51902; i++) {

        float number;
        Res = f_read(&fil, &number, sizeof(number), &NumBytesRead);
        if (Res) throw "Failed to read file.";
        //if(i==49154)printf("the first weight value is %.2f\n", number);
        weightsFromFile[i] = number;
        //weightsFromFile[i+1] = number[2];


    }
  1. const: const 类型的对象在程序执行期间不能被修改改变。
  2. 这个函数需要weightsFromFile和存在sd卡中的file_name。
  3. 命名空间
    这里std::string file_name前面的std是为了指明命名空间,因为很多函数可能在很多的库中出现,指明了之后就不会引起混乱。
    简单的说,命名空间是为了防止冲突而建立的。比如你在命名空间a声明一个方法c,在命名空间b也声明一个方法c,那么在使用的时候就可以通过a::c和b::c来分别调用两个方法。
    使用命名空间的语句是:using namespace [命名空间的名称]
    一旦使用了这个语句,你就可以直接写该命名空间的方法名,比如:
using namespace std; // 使用std命名空间
cout << "xxx" << endl; // 你还是可以写成std::cout << "xxx" << std::endl;的。

延伸几点:

  • 这里还有一点,使用标准函数库时需要加std,而<iostream.h>则不用。
  • endl为换行符,std::cout<<"love you"<<endl;
    "endl"与"\n"的区别是"endl"还调用输出流的flush函数,刷新缓冲区,让数据直接写在文件或者屏幕上。
  1. std:string
    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 = 进行赋值操作,== 进行比较,+ 做串联(是不是很简单?)。我们尽可以把它看成是C++的基本数据类型。
    详情:[http://blog.csdn.net/debugconsole/article/details/8677313]

  2. f_read
Res = f_read(&fil, &number, sizeof(number), &NumBytesRead);

函数的详解如下:
The f_read function reads data from a file.

FRESULT f_read (
  FIL* fp,     /* [IN] File object */
  void* buff,  /* [OUT] Buffer to store read data */
  UINT btr,    /* [IN] Number of bytes to read */
  UINT* br     /* [OUT] Number of bytes read */
);

main.cc

float *weightsFromFile = (float *) malloc(numOfParameters*sizeof(float));
int *test_labels = (int *) malloc(10000 * sizeof(int));
std::vector<std::vector<float> > test_images;
int correctPrediction;
  1. malloc
    float *weightsFromFile = (float *) malloc(numOfParameters*sizeof(float));
    此句为分配numOfParameters个float型变量。
    函数原型是void *malloc(unsigned int num_bytes);
    但是void型不能赋值给float/int,因此要用(float *)强制转换。
    sizeof(float)为float型变量的字节。

  2. std::vector<std::vector<float> > test_images;
    注意<flaot>之后必须空一格,原因暂不明。
    vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,能够增加和压缩数据。
    为了可以使用vector,必须在你的头文件中包含下面的代码:
#include <vector>

vector属于std命名域的,因此需要通过命名限定,如下:

using std::vector;
vector<int> vInts;

或者连在一起,使用全名:

std::vector<int> vInts;

转载于:https://www.cnblogs.com/litingyu/p/8311681.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值