Arm Mbed OS support forum

How to use the append option in fopen to add data to the same file continuously

Could someone help me with the following?
I’m trying to write temperature reading to an output file on my mbed device. It creates the output file but only ever puts in one value. Should be updating reading every 10 secs. I have tried various functions like “a” “a+” to see but only ever one value in my output file. attached below is code

#include “mbed.h”
#include “LM75B.h”
#include “C12832.h”
#include
using std::string;

C12832 lcd(p5, p7, p6, p8, p11);

LM75B sensor(p28,p27);
Serial pc(USBTX,USBRX);
LocalFileSystem local(“local”); // Create the local filesystem under the name “local”

int count;
float temperature;

void writeValue (float temperature){
FILE *fp = fopen("/local/out.txt", “a+”); // Open “out.txt” on the local file system for writing
//if (!fp) fp = fopen("/local/out.txt", “w”);
fprintf(fp,"%f\r\n",temperature);
//fputs(“f\r\n”,fp);
fclose(fp);
printf(“New Entry Added\r\n”);
return;
}

int main ()
{
//count = 0;
//Try to open the LM75B
while (1) {
writeValue((float)sensor);
displayValues((float)sensor);
readFile();
printf(“About to Wait 10 seconds\r\n”);
wait(10.0);
count++;
}
//printf(“We are done!”);
}

Hello Tony,

please be so kind and use formatting (simply add ``` before and after your code) for better presentation of your code.

I not see anything wrong with your implementation of the writeValue function. I use it similar but not with LocalFileSystem.
However, your code seems be not complete. The readFile is not there, so I not see how you verified your result.
Also your variable temperature is declared as global and also as input parameter of your function. That can lead to a collision or confusion, I think.

BR, Jan

Johnny.

Attached is complete code I have tried.
I can see on Terra Term the read comments.
I can see Temp displayed on LCD of mbed and reading every 10 secs with blink of led light.
Problem is Out txt file. Don’t see values in that. Have use “a” “a+” etc . Was doing more research on line etc to see if I could fine any additional info.
This is a student related project. Any help you can provide would be greatly appreciated.

#include “mbed.h”
#include “LM75B.h”
#include “C12832.h”
#include
#include

using std::string;

C12832 lcd(p5, p7, p6, p8, p11);

LM75B sensor(p28,p27);
Serial pc(USBTX,USBRX);
LocalFileSystem local(“local”); // Create the local filesystem under the name “local”

int count;
float temperature;

void writeValue (float temperature){
FILE *fp = fopen("/local/out.txt", “a”); // Open “out.txt” on the local file system for writing
fprintf(fp,"%f\r\n",temperature);
if (fp)
{
putc(’\n’,fp);
//for(int i=0; i<; i++)
//putc(string[i],fp);
}
//fputs(“f\r\n”,fp);
fclose(fp);
printf(“New Entry Added\r\n”);
return;
}
void readFile (){
FILE *fp = fopen("/local/out.txt", “r”);
if (fp == NULL){
printf(“File not found\r\n”);
}
printf(“Reading File\r\n”);

   fclose(fp);
   
   return;

}
void displayValues (float temperature){

lcd.cls();
lcd.locate(0,3);
lcd.printf("Temp = %.3f\r\n", temperature);
lcd.printf("Average = %.3f\r\n", 100.000);
return;

}

int main ()
{
count = 0;
//Try to open the LM75B
while (1) {
writeValue((float)sensor);
displayValues((float)sensor);
readFile();
printf(“About to Wait 10 seconds\r\n”);
wait(10.0);
count++;
}
//printf(“We are done!”);
}

https://os.mbed.com/users/tonyk37/code/lab-3-app-board-LM75B/

How I wrote, I see nothing wrong with your writeValue function.
Maybe you can try to update (right click > update…) the mbed library to latest.

BR, Jan