cancel
Showing results for 
Search instead for 
Did you mean: 

How to Modify text file in USB host mode

waleedaslam89
Associate II
Posted on August 09, 2012 at 11:28

does anyone have idea how to modify a text file in usb host mode using Fatfs library. 

I have a working project of usb host and successfully writing a new file using the code below. but i dont have the idea how to modify or append info in already existing file.

/* Register work area for logical drives */

    f_mount(0, &fatfs);

    

    if(f_open(&file, ''0:STM32.TXT'',FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)

    { 

      /* Write buffer to file */

      bytesToWrite = sizeof(writeTextBuff); 

      res= f_write (&file, writeTextBuff, bytesToWrite, (void *)&bytesWritten);   

      

      if((bytesWritten == 0) || (res != FR_OK)) /*EOF or Error*/

      {

        USART2_SendData_s(''> STM32.TXT CANNOT be writen.\n'');

      }

      else

      {

        USART2_SendData_s(''> 'STM32.TXT' file created\n'');

      }

      

      /*close file and filesystem*/

      f_close(&file);

    } 
1 REPLY 1
Posted on August 09, 2012 at 14:53

Use FA_OPEN_ALWAYS, and f_lseek()

http://elm-chan.org/fsw/ff/en/open.html

Perhaps :

FIL f;

if( f_open( &f, ''log.txt'', FA_WRITE|FA_OPEN_ALWAYS ) == FR_OK )

{

u32 wrt=0;

f_lseek( &f, f.fsize );

f_write( &f, buffer, strlen(buffer), &wrt );

f_close( &f);

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..