Problem with writing to a File


I would like to write the data from the sensors to a file after booting from the CF card. However, after executing the program, when I open the file, it is empty.

I have checked that the file is opened correctly, the number of bytes writen is correct and that the file is closed. When i open the file on the host(desktop), the file is empty and the last edited time is that at which I created the file in the host. This means that the data is not saved when the cf card is in the PC/104 CPU.

What could be the problem?

Would appreciate very much your help.

The simple code is below:

#include <stdio.h>
#include <time.h>
#include <sys/syspage.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/types.h>

int main(int argc, char *argv[]) {
int i=0,k=0;
FILE *data;
//int data;

int errvalue;
int check;
errno = EOK;

data = fopen(“file.txt”,“w”);


printf("file is opened = ");
errvalue = errno;
printf(“error is %d\n”,errvalue);
printf(“means : %s\n”, strerror(errvalue));

errvalue = errno;
printf("error is %d ",errvalue);
printf("means : %s\n", strerror(errvalue));

printf(“trying to close file =”);
check = fclose(data);

errvalue = errno;
printf("error in closing file :%d ",errvalue);
printf(“means : %s\n”, strerror(errvalue));

printf(“file is closed\n”);
printf(“file is not opened\n”);

return 0;

To make sure the data is written you should check the return value of fprintf. errno is set only if the function return an error. Same thing with fopen, since it didn’t failed the printf that shows errno are totaly meaningless as per C standard.

Also be aware that in your example the data is written to the disk only at the fclose call, because fprintfs are buffered and there are no \n or fflush to force a flush.

That being said your code looked ok. I have seen some model of flash ssd that have the write cache turned on by default and that would create the scenario you described. After many attempt at fixing the problem by software we contacted the maker of the flash ssd ( m-system) and got special version of the disk with the write cache turn off.

What happened if you for example copy a file with cp does the same thing happen?

Hi Mario,

thanks for your reply. Do you imply that it is a hardware issue that the write cache is turned on?

Can I avoid this problem by using for example, fwrite?

What would you suggest?

Thanks in advance!


I’d recommend calling ‘sync’ after you do an fclose. This flushes the cache in Fsys to update the CF card. This is especially important if your file is small in size (not a lot of data) and it may fit entirely in the cache.

IE after your fclose do:

system (“sync”);

Also, depending on your CF card, some have faster write times than others. So you definitely want to make sure you have a good CF card (like a Sandisk Ultra III or Ultra IV)


I’m not saying it is but it’s a possibility.

If you try to copy a file with cp, then do a sync then wait 10 seconds then reboot and after reboot the file isn’t there it most probably hardware.