This example is the usual sequence to fill a file :
// File fill >1MB #define FILL_FILE_NB_WRITE 855L #define FILL_FILE_BUF_SIZE 120L Bool fill_file( void ) { const UNICODE _MEM_TYPE_SLOW_ name[50]={'l','o','g','.','b','i','n',0}; U16 u16_nb_write; memset( g_trans_buffer , 0x55 , FILL_FILE_BUF_SIZE ); if( !nav_drive_set(LUN_DISK)) // Enter in disk return FALSE; if( !nav_partition_mount()) // Mount partition of disk return FALSE; if( !nav_file_create( (const FS_STRING) name )) // Create file return FALSE; if( !file_open(FOPEN_MODE_W)) // Open file in write mode with force file size 0 return FALSE; for( u16_nb_write=0; u16_nb_write<FILL_FILE_NB_WRITE; u16_nb_write++ ) { // HERE, at each write file, a allocation in FAT area is run // so, if you have many buffer to write the execution may be slow. if( !file_write_buf( g_trans_buffer , FILL_FILE_BUF_SIZE )) { file_close(); return FALSE; } } file_close(); return TRUE; }
This example allow to accelerate write access with a prealloc routine :
// File fill >1MB #define FILL_FILE_NB_WRITE 855L #define FILL_FILE_BUF_SIZE 120L Bool fill_file_fast( void ) { const UNICODE _MEM_TYPE_SLOW_ name[50]={'l','o','g','_','f','a','s','t','.','b','i','n',0}; _MEM_TYPE_SLOW_ Fs_file_segment g_recorder_seg; U16 u16_nb_write; memset( g_trans_buffer , 0x55 , FILL_FILE_BUF_SIZE ); if( !nav_drive_set(LUN_DISK)) // Enter in disk return FALSE; if( !nav_partition_mount()) // Mount partition of disk return FALSE; if( !nav_file_create( (const FS_STRING) name )) // Create file return FALSE; if( !file_open(FOPEN_MODE_W)) // Open file in write mode and forces the file size to 0 return FALSE; // Define the size of segment to prealloc (unit 512B) // Note: you can alloc more in case of you don't know total size g_recorder_seg.u16_size = (FILL_FILE_NB_WRITE*FILL_FILE_BUF_SIZE + 512L)/512L; // ****PREALLLOC****** the segment to fill if( !file_write( &g_recorder_seg )) { file_close(); return FALSE; } // Check the size of segment allocated if( g_recorder_seg.u16_size < ((FILL_FILE_NB_WRITE*FILL_FILE_BUF_SIZE + 512L)/512L) ) { file_close(); return FALSE; } // Close/open file to reset size file_close(); // Closes file. This routine don't remove the previous allocation. if( !file_open(FOPEN_MODE_W)) // Opens file in write mode and forces the file size to 0 return FALSE; for( u16_nb_write=0; u16_nb_write<FILL_FILE_NB_WRITE; u16_nb_write++ ) { // HERE, the file cluster list is already allocated and the write routine is more fast. if( !file_write_buf( g_trans_buffer , FILL_FILE_BUF_SIZE )) { file_close(); return FALSE; } } file_close(); return TRUE; }
Create a file of 100.2KB (buffer 120B * nb write 855) on a disk 256MB (FAT16-cluster 4KB):
Create a file of 1.1MB (buffer 120B * nb write 10000) on a disk 256MB (FAT16-cluster 4KB):