ZedBoard学习(3)U盘读写

今天在Linux下操作U盘时才发现,以前对Linux的理解太浅了,对ARM Linux的理解有太浅了,因为需要进行数据的存储,最初的想法移植停留在怎么写U盘的驱动,其实Linux里U盘的驱动都已经写好了,那么U盘就更PC上的存储器是一样的,直接进行文件的读写就可以了。

写一段简单的读写文件的代码,进行测试,从file1中拷贝内容到file2。

 

#include <stdio.h>

int main(int argc, char **argv)

{

    FILE* sourceFile;

    FILE* destFile;

    char buf[50];

    int numBytes;

    sourceFile = fopen("/mnt/file1", "rb");

    destFile = fopen("/mnt/file2", "wb");

 

    if(sourceFile==NULL)

    {

        printf("Could not open source file\n");

        return 2;

    }

    if(destFile==NULL)

    {

        printf("Could not open destination file\n");

        return 3;

    }

 

    while(numBytes=fread(buf, 1, 50, sourceFile))

    {

        fwrite(buf, 1, numBytes, destFile);

    }

}

交叉编译环境编译 arm-xilinx-linux-gnueabi-gcc USB.c

连上网线进行FTP传输,挂载U盘,mount /dev/sda /mnt,这个时候U盘已经挂在了/mnt文件夹下

通过超级终端执行程序 ./a.out

查看file2中的内容,就会发现拷贝已经完成。

cat /mnt/file2

 

 

原文地址:https://www.cnblogs.com/libing64/p/2878719.html