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.

Problem with f_lseek function of FAT library in SD card

Hi everyone.

I'm having a trouble with the F_lseek function of the tiva c fat library, when i go to another line the previous line is filed with trash.

This is my code:

        FIL fil;
        uint32_t count =8*515;
        f_open(&fil, "testfile.txt", FA_OPEN_ALWAYS|FA_WRITE);    
        f_write(&fil, "Line1", 5, &count);
        f_close(&fil);
        f_open(&fil, "testfile.txt", FA_OPEN_ALWAYS|FA_WRITE);
        f_lseek(&fil,1024);          //moves the file pointer to the 2nd line
        f_write(&fil, "Line2", 5, &count);
        f_close(&fil);
        f_mount(0, NULL);

with this code i get this txt file:

Line1 v     v     v     ´v     Äv     Ðv     Üv   

Line2

Thanks for your help.

  •  Hi, which error you are experiencing?

     What is FRESULT you trash away?

     ......

    Luis Chachayma said:
            uint32_t count =8*515;

     what was the purpose of this preloaded constant?

    Luis Chachayma said:
            f_write(&fil, "Line1", 5, &count);

     This write just 5 of 6 character of string, so take account of null terminator too....

    Luis Chachayma said:
            f_lseek(&fil,1024);          //moves the file pointer to the 2nd line
            f_write(&fil, "Line2", 5, &count);

     this move pointer to 1024 then write again string without null terminator... End of file truncate garbage ...

     Writing nothing then get the sector to buffer, write the data len then rewrite modified sector.

     You wrote some data garbage remained what it was. Every operating system work this way.

     if you print count after write you can see how many bytes where really written.

     IMHO if your issue is the one you reported FSFat work right as every file system.

    so if where file is written data was this:

    garbagegarbagegarbagegar....

     Seek to 4 then write Hello result in

    garbHellorbagegarbagegar....

  • Hi  Roberto

    I did this modifications according to your suggestions and i still see garbage

    FIL fil;
            uint32_t size=6;
            f_open(&fil, "testfile.txt", FA_OPEN_ALWAYS|FA_WRITE);
            SysCtlDelay(SysCtlClockGet()/3);
            f_write(&fil, "Line1", size, &size);
            f_close(&fil);
             f_open(&fil, "testfile.txt", FA_OPEN_ALWAYS|FA_WRITE);
             f_lseek(&fil,1024);
            f_write(&fil, "Line2", size, &size);
            iFResult = f_close(&fil);
                f_mount(0, NULL);

    Thanks for your help

  • Luis Chachayma said:
    I did this modifications according to your suggestions and i still see garbage

     And you still get garbage forever... to not get garbage you need a binary file zero filled, this case you get no garbage than zero fill...

     From Murphy rule:

     Add small amount of garbage to wine and you get garbage

     Add wine to garbage and you get garbage too...

     Everything you write in an unknown garbage is garbage too..

     So try move pointer far away your second line, write just a char and you can see again "THE garbage" where on sector before write... When you write on some point garbage is overwritten but remain all around your data starting at end of written data.

     To remove garbage you MUST fill in with data.

     If this explained you how filesystem work, please mark answer as verify'd.

  • Thanks it worked. Now i want to ask you one more think please.
    Im going to put a header in each line of the txt file(im using the SD to save monitoring data), how can i search a header in the txt file?

  • Luis Chachayma said:
    m going to put a header in each line of the txt file(im using the SD to save monitoring data), how can i search a header in the txt file?

     Hi Luis, I suppose you are a C beginner from your request, don't worry about but just to respect forum and have it more usable please check this thread as verified and open another with title

    "header search in a file"

    this help other search how to and save time to all us we are unpayd for this job and we wish help our colleague.

      So try open the new question and start : I am writing a Text | Binary file (select appropriate) , I am in nedd to search for an Header

     Again fixed binary, record ...

     Try describe at your best and we can find a better solution everyone read can use. Don't forget detail only you can know!

     Thank for understanding.

  • thanks for your help.

    regards

  •  Hi Luis, I was remembering of your request differing from main title but I never seen the new thread, did you solved too?