今天在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