We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I'm posting this question here since Keil's support haven't answered my email so far: ---------------------------------------- I have the following piece of code:
fH = fopen(FILE_NAME_CLOCK, "a"); fseek(fH,20,SEEK_SET); fwrite(jbuf,1,sizeof(jbuf),fH); fclose(fH);
It is supposed to write to position 20 right, but if you look at the attached file it appends the data to the end of the file. What am I missing or doing wrong or is it a bug?
PS: Using Keil's RTK file system and µVision3 V3.80 on a ARM7 from NXP.
What am I missing or doing wrong or is it a bug?
It's not a bug. If you want to use fseek() this way, you need to open the file as a binary file, not as a text file. Refer to a C textbook (K&R will do) for more information.
Thanks Christoph, I read Keils documentation which mentions nothing about differences in opening text or binary files or using the "+" option. But I changed my code anyway to:
fH = fopen(FILE_NAME_CLOCK, "ab"); rewind(fH); fwrite(jbuf, 1, sizeof(jbuf), fH); fclose(fH);
As you can see I replaced fseek() with rewind() - and still it appends the string to the end of the file!
Is there someone who actually used Keil's file system before and how did you write to an arbitrary location inside the file?
What happens if you use r+ (file must exist).
Ok, I just tried that and when I open it with the attribute of "r+" the returned FILE pointer is NULL. And just to confirm I changed it back to "r" and it works again.
Let me also add that this is accoding to Keil's documentation which states that only "r", "w" and "a" attributes is supported.