This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Delete upper part of data from a file in sd card

In our application  we have connected  the TM4C1231H6 controller  with  a microsd card on SPI interface.We are using FAT file system library   file "ff.h ".

1)We create a file in sd card which keeps on saving data.
2) When data crosses a  limit we would like to divide the  file in two parts.
3)We would like to  delete the upper half of data in the file and retain the  lower half (i.e. the latest data).
 
Please suggest method to accomplish this task.

  • Hello Vahbiz

    The correct approach would be to either f_unlink to remove a file or f_rename the upper half file with a new time stamped version so that log does exist.

    Regards
    Amit
  • Thank You Amit for your quick reply!!

    But i am sorry to say i did not understand either of your solutions. I have little knowledge of file system.

    Could you please explain in more details?

    I want to retain (keep) the lower half of the file, and remove the upper half.


    Thanks,

    Vahbiz

  • But i am sorry to say i did not understand either of your solutions. I have little knowledge of file system.

    Then this is a good time to come to grips with your task. If you had any real understanding of a file system and it's implementation, you wouldn't ask such questions.

    A "file" is just a metaphor for a data item of potentially unlimited size outside of the main memory, consisting of a stream of bytes. A "file system" is a metaphor for managing such files. File systems like FatFS are implementations of this metaphor. Go read it up.

    Find out what methods/functions your file system implementation offers to solve your problem.

    Hint: there are no "upper halfes" and "lower halfes" of files.

  • Hello f.m.

    We are using functions from ff.h file present in the original source package at \ti\TivaWare_C_Series-2.0.1.11577\third_party\fatfs\src

    FRESULT f_mount (BYTE vol, FATFS* fs); /* Mount/Unmount a logical drive */

    FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */

    FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from a file */

    FRESULT f_lseek (FIL* fp, DWORD ofs); /* Move file pointer of a file object */

    FRESULT f_close (FIL* fp); /* Close an open file object */

    FRESULT f_opendir (DIR* dj, const TCHAR* path); /* Open an existing directory */

    FRESULT f_readdir (DIR* dj, FILINFO* fno); /* Read a directory item */

    FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */

    FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to a file */

    FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */

    FRESULT f_truncate (FIL* fp); /* Truncate file */

    FRESULT f_sync (FIL* fp); /* Flush cached data of a writing file */

    FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */

    FRESULT f_mkdir (const TCHAR* path); /* Create a new directory */

    FRESULT f_chmod (const TCHAR* path, BYTE value, BYTE mask); /* Change attribute of the file/dir */

    FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change times-tamp of the file/dir */

    FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */

    FRESULT f_chdrive (BYTE drv); /* Change current drive */

    FRESULT f_chdir (const TCHAR* path); /* Change current directory */

    FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */

    FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* sn); /* Get volume label */

    FRESULT f_setlabel (const TCHAR* label); /* Set volume label */

    FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */

    FRESULT f_mkfs (BYTE vol, BYTE sfd, UINT au); /* Create a file system on the drive */

    FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work); /* Divide a physical drive into some partitions */

    int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */

    int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */

    int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */

    TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */

     

    My requirement is to erase the upper half of the file. So I am talking of upper and lower half.

    I want to do the exact reverse of "f_truncate" function. I want to keep the lower part and remove the upper part.

    Also I went through the 3 functions mentioned by Amit (The correct approach would be to either f_unlink to remove a file or f_rename the upper half file with a new time stamped version so that log does exist.)

    1. f_unlink  which will Delete the existing file
    2. f_rename which will Rename/Move a file
    3. f_utime which will Change the times-tamp of the file/dir


    I can not understand how I can achieve my goal by using these functions.
    Request you to please give your valuable suggestions to resolve my issue.

    Thanks in Advance!

    Vahbiz

  • Request you to please give your valuable suggestions to resolve my issue.

    I request you to get up, learn something yourself, and do something yourself. Do you honestly believe I'm going to do this for you, for free ?

    There is nothing TM4C specific, not even microcontroller specific in your request, just simple, plain C library stuff. There are literally thousands of pages around, both webpages and printed paper.

    You didn't even try anything for yourself, did you ? Perhaps you can sucker in a TI employee, but hardly any volunteering user.

  • f. m. said:
    You didn't even try anything for yourself, did you ? Perhaps you can sucker in a TI employee, but hardly any volunteering user.

     Hi F.M., I like your answer and is exactly what I think about too many request we get here, this has to be TI task but as we noted too many times this bad habit persist and this seems a teaching all for free or better as CB1 asked "Asylum".

     Is time to TI stop this and split then RAISE LEVEL!!

     I am sorry and also if this one start offending me or you (remember last Mr Aquino?) we lose time teach how to use two line of c or modify two simple basic HTML lines.

     I am tired like I feel some others too.

     Sorry for poster we have nothing personal but we cannot waste our time nor I think can do Amit or Stellaris I see like it  escaped from.

     @ Blake can we have e better and usable focused forum? And leave some more time to our Good I think tired too Amit?

     Sorry but I too much seldom see something really on TM4C topic related or real issue.

     

  • Vahbiz Pestonjamasp said:

    RESULT f_truncate (FIL* fp); /* Truncate file */

    FRESULT f_sync (FIL* fp); /* Flush cached data of a writing file */

    F

     Standard function are here.. so the only issue, soorry point to that is your complete lack of basic file I/O programming culture. I don't repeat the good idea of metafora as I used for Semaphore... these are programming language and culture and have nothing to do with TIVA TM4C programming nor problem to silicon.

     We are so sorry but we have to survive, if you need learn get to my course where I am paid for.

    Vahbiz Pestonjamasp said:

    I can not understand how I can achieve my goal by using these functions.
    Request you to please give your valuable suggestions to resolve my issue.

    Thanks in Advance!

     Finishing this as Tanks, also if some please are interspersed this is hidden imperative form and sound quite offensive to forum people that are here to exchange idea not solving simple tasks for free.

     I suggest again this humour sketch as "metafora" about  language usage:

    https://www.youtube.com/watch?v=KAfKFKBlZbM

  • Thanks Roberto for the moral support.

    While I understand TI (this suckers may "perhaps", "at one time", "in the distant future", become TI customers, so don't offense anybody by holding a mirror up to him), I also feel annoyed by this flood of stupid requests. The lazyness of most of those posters is only surpassed by their blatant obtrusiveness. I know these tactics from my kids. If they are late with their homework, they try to provoke me to tell them a ready-made solution - just like those posters. Not sure if brushing them off helps much - perhaps just ignore them.

    And you are true, I noticed this hidden imperative, and it really upsets me. I often have to restrain myself to choose my words carefully.

    And by the way, I visit other MCU vendor's fora, and it's exactly the same elsewhere. Seems like The-Rise-Of-The-MCU-Forum-Troll-Zombies ...

  • Hi,
    This is for anyone who is facing this same issue. I finally figured out the "neat way"!!

    1. Change the way you write the file.
    2. Always keep the latest entry first.
    .................entry3!
    .................entry2!
    .................entry1!

    3. Always write at the start of the file. ( write pointer will always be reset to 1 , so basically no need for a write pointer at all.)
    4. After a write operation , increment the read pointer by 1.
    5. After a read operation , decrement the read pointer by 1.
    6. Now when the file has increased beyond a certain size , we can use the f_truncate function of our filesystem to delete the old data (which happens to be in the lower half of the file).

    Thank you!
    Vahbiz