This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

fopen fails after numerous successes

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*)&copy_data,
                       1,
                       COPY_BYTES,
                       source);
      //write bytes from ram to destination file
      c_write = fwrite((void*)&copy_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);
}

Parents
  • 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.

Reply
  • 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.

Children