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

RL-FlashFS - File overwrite?

Hello,

I'm wondering if it's possible, and if so if someone has an example, to overwrite a portion of a file using the RL-FlashFS library.

Basically I have a working setup and I have a small file system on NOR flash. I have a fixed size file and I want to overwrite a middle section of this file.

If I try something like:

            if(f = fopen("my_test_file", "a"))
            {
                int j = 38;
                rewind(f);
                fseek(f, header.head_index-1 * (4 * sizeof(uint32_t)), SEEK_SET);
                fwrite(&j, sizeof(uint32_t), 1, f);
                fwrite(&j, sizeof(uint32_t), 1, f);
                fwrite(&j, sizeof(uint32_t), 1, f);
                fwrite(&j, sizeof(uint32_t), 1, f);
                fseek (f, -1L, SEEK_END);
                fclose(f);
            }

The rewind doesn't seem to do anything and it just appends the 4 ints to the end of the file.

Any ideas? Am I doing something wrong?

Thanks.

M

Parents
  • The reply from support:

    **********************
    Thank you for using Keil software.

    The file pointer only gets used for reading.

    The C library does not allow you to open a file in a mode in which you can overwrite a portion of the file, you can either replace the contents of file, or append to it.

    If you want to delete part of a file, you need to:

    1) make a temporary copy of the file
    2) open the original file with mode "w", and the copy in mode "r"
    3) copy the portions you want to keep, from the temporary file to the original file
    4) close both files
    5) delete the temporary file

    **********************

    I followed up to see if there are any plans to implement other modes (like "r+")...

Reply
  • The reply from support:

    **********************
    Thank you for using Keil software.

    The file pointer only gets used for reading.

    The C library does not allow you to open a file in a mode in which you can overwrite a portion of the file, you can either replace the contents of file, or append to it.

    If you want to delete part of a file, you need to:

    1) make a temporary copy of the file
    2) open the original file with mode "w", and the copy in mode "r"
    3) copy the portions you want to keep, from the temporary file to the original file
    4) close both files
    5) delete the temporary file

    **********************

    I followed up to see if there are any plans to implement other modes (like "r+")...

Children