application that helps to copy file between SD and mbed_local. insert/remove SD card is too troublesome for me. And also, this is a simple sample of the fread/fwrite

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 
00004 //DigitalOut led[]    = { LED4, LED3, LED2, LED1 };
00005 BusOut          led( LED4, LED3, LED2, LED1 );
00006 SDFileSystem    sd(p5, p6, p7, p8, "sd");
00007 LocalFileSystem local("local");
00008 Serial          pc(USBTX, USBRX); // tx, rx
00009 
00010 //#define        SOURCE_FILE        "/local/source.jpg"
00011 //#define        TARGET_FILE        "/sd/target.jpg"
00012 
00013 #define        SOURCE_FILE        "/sd/temp_log.txt"
00014 #define        TARGET_FILE        "/local/temp_log.txt"
00015 
00016 #define        COPY_BLOCK_SIZE    2048
00017 
00018 int main() {
00019 
00020     FILE    *fs_src;
00021     FILE    *fs_tgt;
00022     char    s[ COPY_BLOCK_SIZE ];
00023     int     size;
00024     int     total    = 0;
00025     int     progress_ind    = 0;
00026     
00027     pc.printf( "\nfile copier started.\n" );
00028     pc.printf( "  source:%s >>> target:%s\n", SOURCE_FILE, TARGET_FILE );
00029 
00030     if ( NULL == (fs_src    = fopen( SOURCE_FILE, "rb" )) )
00031     {
00032         error( "couldn't open source file" );
00033         return ( 1 );
00034     }   
00035     if ( NULL == (fs_tgt    = fopen( TARGET_FILE, "wb" )) )
00036     {
00037         error( "couldn't open target file" );
00038         return ( 2 );
00039     }   
00040 
00041     while (  size    = fread( s, sizeof( char ), COPY_BLOCK_SIZE, fs_src ) )
00042     {
00043         fwrite( s, sizeof( char ), size, fs_tgt );
00044         total    += size;
00045 
00046         led    = (0x1 << progress_ind++ % 4);
00047 
00048         pc.printf( "  %d bytes copied\r", total );
00049     }
00050     
00051     led    = 0xF;
00052     
00053     fclose( fs_src );
00054     fclose( fs_tgt );
00055     
00056     pc.printf( "\ndone\n" );
00057 
00058 }