fputs(), fputc() appending string/characters twice issue

mafia2g2001
Posts: 4
Joined: Sun Aug 20, 2023 9:37 pm

fputs(), fputc() appending string/characters twice issue

Postby mafia2g2001 » Sun Aug 20, 2023 11:03 pm

When opening a file in append mode and start putting a string into file stream then closing the file and opening the file again in read mode it shows the string is written/flushed twice. Example code:
  1. char *txt = "World";
  2. FILE *fs_a = fopen("/filesPartitionFatfs/test.txt", "a");
  3. fputs(txt, fs_a);
  4. fclose(fs_a);
  5.  
  6. FILE *fs = fopen("/filesPartitionFatfs/test.txt", "r");
  7. do{
  8.      if(feof(fs)){ break; };
  9.      char c = fgetc(fs);
  10.      printf("%c", c);      
  11. }while(1);
  12. fclose(fs);
The output result:

Code: Select all

WorldWorld
I have tried this code on my pc and it works fine just as intended but in ESP-IDF i cannot figure out what is happening. Any help will be much appreciated.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: fputs(), fputc() appending string/characters twice issue

Postby ESP_Sprite » Mon Aug 21, 2023 12:29 am

Well, you're opening the file to append; are you sure your program didn't run one time before and dropped the initial 'world' there, with you looking at the second time where it appended the second 'world'?

mafia2g2001
Posts: 4
Joined: Sun Aug 20, 2023 9:37 pm

Re: fputs(), fputc() appending string/characters twice issue

Postby mafia2g2001 » Mon Aug 21, 2023 6:27 am

ESP_Sprite wrote:
Mon Aug 21, 2023 12:29 am
Well, you're opening the file to append; are you sure your program didn't run one time before and dropped the initial 'world' there, with you looking at the second time where it appended the second 'world'?
I am sure and one more thing is that if I open the file in write mode, empty string, close then open in append mode, someString, close then open in read mode everything works perfectly.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: fputs(), fputc() appending string/characters twice issue

Postby ESP_Sprite » Mon Aug 21, 2023 7:00 am

mafia2g2001 wrote:
Mon Aug 21, 2023 6:27 am
I am sure and one more thing is that if I open the file in write mode, empty string, close then open in append mode, someString, close then open in read mode everything works perfectly.
How are you sure, what reason do you have for that? If anything, your second point makes it more likely that your program somehow gets run twice as the append works fine if you have a guaranteed-empty file before it runs.

EDIT: For shits and giggles, can you take the code that exhibits the problem and put a vTaskDelay(pdMS_TO_TICKS(2000)); at the top of app_main? See if it still does the thing?

mafia2g2001
Posts: 4
Joined: Sun Aug 20, 2023 9:37 pm

Re: fputs(), fputc() appending string/characters twice issue

Postby mafia2g2001 » Mon Aug 21, 2023 8:26 am

ESP_Sprite wrote:
Mon Aug 21, 2023 7:00 am

How are you sure, what reason do you have for that? If anything, your second point makes it more likely that your program somehow gets run twice as the append works fine if you have a guaranteed-empty file before it runs.

EDIT: For shits and giggles, can you take the code that exhibits the problem and put a vTaskDelay(pdMS_TO_TICKS(2000)); at the top of app_main? See if it still does the thing?
    I really appreciate the help thank you. I expected since the code to append has some print string and it is only printed once to the terminal that is why I claimed to be sure. Anyways below is the code to the barebones in app_main()
      1. void app_main(void){
      2.      // Mounting partition -- shows ESP_LOGI -->Sensor: Partition html mounted with /page directory....
      3.      mount_fatfs_custom_partition(HTML_PART_LABEL, HTML_PART_FOLDER);
      4.  
      5.      // Opening file in append mode
      6.      FILE *file_obj_a = fopen("/page/index.txt", "a");
      7.      fputs("log1", file_obj_a);
      8.      fclose(file_obj_a);
      9.      printf("/////log appended/////\n"); \\-->This is printed only once but the file appended twice
      10.  
      11.      // Opening file in read mode
      12.      FILE *file_obj_r = fopen("/page/index.txt", "r");
      13.      do{
      14.              char c = fgetc(file_obj_r);
      15.              if (feof(file_obj_r)){
      16.                    break;
      17.              }
      18.              printf("%c", c);
      19.          } while (1);
      20.      fclose(file_obj_r);
      21.      printf("\n");
      22. }
        Output to terminal

          Code: Select all

          I (1305) main_task: Calling app_main()
          I (1325) Sensor: Partition html mounted with /page directory....
          /////log appended/////
          log1log1
          I (1595) main_task: Returned from app_main()
          
            I could not follow your suggestion on adding the code with vTaskDelay(pdMS_TO_TICKS(2000)) on top of app_main, If you can explain more please.

            ESP_Sprite
            Posts: 9739
            Joined: Thu Nov 26, 2015 4:08 am

            Re: fputs(), fputc() appending string/characters twice issue

            Postby ESP_Sprite » Mon Aug 21, 2023 10:10 am

            mafia2g2001 wrote:
            Mon Aug 21, 2023 8:26 am
            I really appreciate the help thank you. I expected since the code to append has some print string and it is only printed once to the terminal that is why I claimed to be sure.
            What I think is happening is that after programming, the ESP32 is reset and will actually run without its output being captured. Then, a little bit later when the terminal program starts up again, the ESP32 is reset again and the program will run a second time.
            I could not follow your suggestion on adding the code with vTaskDelay(pdMS_TO_TICKS(2000)) on top of app_main, If you can explain more please.
            See below:

            1. #include <freertos/FreeRTOS.h>
            2. #include <freertos/task.h>
            3.  
            4. void app_main(void){
            5.     vTaskDelay(pdMS_TO_TICKS(2000));
            6.      // Mounting partition -- shows ESP_LOGI -->Sensor: Partition html mounted with /page directory....
            7.      mount_fatfs_custom_partition(HTML_PART_LABEL, HTML_PART_FOLDER);
            8.  
            9.      // Opening file in append mode
            10.      FILE *file_obj_a = fopen("/page/index.txt", "a");
            11.      fputs("log1", file_obj_a);
            12.      fclose(file_obj_a);
            13.      printf("/////log appended/////\n"); \\-->This is printed only once but the file appended twice
            14.  
            15.      // Opening file in read mode
            16.      FILE *file_obj_r = fopen("/page/index.txt", "r");
            17.      do{
            18.              char c = fgetc(file_obj_r);
            19.              if (feof(file_obj_r)){
            20.                    break;
            21.              }
            22.              printf("%c", c);
            23.          } while (1);
            24.      fclose(file_obj_r);
            25.      printf("\n");
            26. }
            If my hunch is correct, the first time the program runs, it only runs for a short while. By delaying startup for two seconds, it will never get to the append routine before being reset again, meaning you should now only have one 'log appended'.

            mafia2g2001
            Posts: 4
            Joined: Sun Aug 20, 2023 9:37 pm

            Re: fputs(), fputc() appending string/characters twice issue

            Postby mafia2g2001 » Mon Aug 21, 2023 10:40 am

            ESP_Sprite wrote:
            Mon Aug 21, 2023 10:10 am
            What I think is happening is that after programming, the ESP32 is reset and will actually run without its output being captured. Then, a little bit later when the terminal program starts up again, the ESP32 is reset again and the program will run a second time.

            If my hunch is correct, the first time the program runs, it only runs for a short while. By delaying startup for two seconds, it will never get to the append routine before being reset again, meaning you should now only have one 'log appended'.
            That is exactly what was happening since I added line vTaskDelay(pdMS_TO_TICKS(5000)) :arrow: 2 seconds wasn't long enough, the append works fine now. Your support is much appreciated, thank you.

            Who is online

            Users browsing this forum: Majestic-12 [Bot], romboidale, sohaib.q and 91 guests