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); }
Maybe this is a upper case/lower case file name issue. FlashFS treats anything shorter than 8 characters as a upper case file name. Here are the rules:
- if name is less than or equal 8 characters and extension less or equal to 3 and only one case characters, (all lower or all upper) file is coded as short filename which only contains upper cases letters so: test.txt => TEST.TXT TEST.TXT => TEST.TXT
- if case of letters are mixed it is automatically considered a long filename and filename is encoded as it was written Test.txt => Test.txt teSt.txt => teSt.txt
- if there are more than 8 characters in filename or more than 3 in extension or more . (dots) in filename it is encoded as long filename
- filename Test.txt is different than filename tEst.txt as they are both long and encoded as they were written
I have experienced similar misery (code that fails "occasionally") and this was the issue.
Michael,
thanks for your quick response. I changed the filenames to upper case and tried it with lower case but still no success .....
do you use an operating system? maybe this is a preemption issue, where 2 tasks try to write to the same file concurrently...?
View all questions in Keil forum