We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello,
I have trouble writing an file-copy function. My fcopy-function should read data from a file into a buffer, then write the buffer to another file.
It works fine for several times in succession (see test_fcopy), but then the sourcefile suddenly can't be opend (fopen returns 0x00).
Heap size is 0x10000 (wich is very big, I guess). Any ideas?
Marco
int test_fcopy() { int rc; int i; for(i=0;i<100;i++) { rc = fcopy("F:dest.txt", "F:source.txt"); if(rc != FILESYSTEM_OK) { //rc != FILESYSTEM_OK after 10 cycles, //depending on file size break; } } } int fcopy(char *destinationfile, char *sourcefile) { #define COPY_BYTES 512 char copy_data[COPY_BYTES]; FILE *dest; FILE *source; int c_read; int c_write; int rc; rc = FILESYSTEM_OK; //open source file for reading source = fopen (sourcefile, "r"); //open destination file for writing dest = fopen (destinationfile, "w"); if((source) && (dest)) {//if both files could be opend while (!feof (source)) { //read bytes from source file to ram c_read = fread((void*)©_data, 1, COPY_BYTES, source); //write bytes from ram to destination file c_write = fwrite((void*)©_data, 1, c_read, dest); if(c_read != c_write) { rc = FILESYSTEM_ERROR; } } //Close files fclose(source); fclose(dest); } else { rc = FILESYSTEM_ERROR; } return(rc); }
I guess that, it fails because there are no space available. It is not allowed to "modify" the block of a Flash memory. It is only allowed to "erase" the block of a Flash memory.
http://www.keil.com/support/man/docs/rlarm/rlarm_fs_mem_org.htm
When the content of the file is modified, the old file content is invalidated and the new memory block is allocated. The Flash Block is erased when all the data stored in the Flash Block is invalidated.
John,
thanks for your input. I checked if the Flash is full using ffree(). There is plenty of space left. After all, I delete the destination file bevore I copy. And why would it work a few times in a row and then won't open the source file?
Next thing I tried was copying a file from drive R: to drive F: -> works perfect ....
You delete the destination file - but how? Remember that flash isn't read-write memory, so you can't just delete a file from a flash file system. The flash file system can mark the file as deleted, but then needs some form of "garbage collect" where it merges partial information from different flash sectors into a new flash sector and then erases the older flash sectors.
Hi Per,
I certainly assume that the fdelete() function works for all drives, right? It returns 0, so no error occured. The FlashFS manages the deleting of blocks a.s.o.
Hope I'm right ...