Incomplete Documentation of SD Card 'open' parameters.

I have been struggling with the SD Card functions for months and have only just realised that the documentation doesn't include most of the opening modes - especially the one I really need to use.

There is also no mention that opening without specifying a mode defaults to READ ONLY, or even a hint that the file position pointer is set to EITHER the start or end of the file, depending on the opening mode.

All I want to do is over-write one of eight files with a new set of 32 values in the same locations in the file, to allow them to be used as alternative startup parameters for the machine I've built.

Looking in the library files tells me that there are lots of modes, but I can't find any examples of the syntax to use in my code.

At startup, I open a file and transfer the contents into a memory array. That works OK using with no parameters.

Subsequently I can edit the parameters, and I may need the again, so I want to save them.

I have set up eight files, to allow a variety of settings to be stored, but I can currently only do it if I create a new file every time, as the data gets appended to the existing contents.

Using seek(0) only resets the write pointer to the end of the existing data - i.e. the append mode is enforced.

Any help will be gratefully received.

1 Like


 File dataFile ="datalog.txt",O_TRUNC|O_WRITE);

Thanks, I'll try that, but it's still working in the dark.

I need to be able to read from the file after the contents change, so can I use RDWR?

What I really want is a full description of the syntax to use and a description of the actual modes where it should be - in the documentation...

1 Like

Given that O_TRUNC and O_WRITE are known to the library, I suspect (without actually reading the code) that it's just a wrapper for plain old C file operations so you could probably get by with that documentation rather than the Arduino specific stuff.

There's an understandable tendency here to avoid documenting things that can be found elsewhere, particularly when they relate to C/C++ features that appear in a gazzilion tutorials.

I appreciate your comments and have now tried changing my code to use the O-TRUNC | O-WRITE syntax and the compiler doesn't recognise either of those commands. ( or any of the others with an O_ prefix )

The code which works uses FILE_WRITE, which works fine.

Is my SD card library file corrupted in some way?

My entire set of Arduino files are less than six months old - that's when I first started my project.

It should be O_TRUNC | O_WRITE if it's supported.

Given that in SD.h is the line:


What error messages exactly do you get when you try using O_READ? - unless sterrejte has already resolved your problem.

Sorry if I have confused you all, I'm used to seeing the text colours in the IDE represent the type of element I'm writing.

My code worked, but not the way I want it, so the original suggestion of O_WRITE, etc. sounded like a fix, but when I tried it there was no colour change to tell me what I had typed was valid, so I assumed it was a fatal error.

As there is NO documentation to tell you that there are a set of more basic functions available in the SD library, I had no idea where to go next, so I started reading the .h and .cpp files and began to understand things.

When I used FILE_WRITE and FILE_READ, both turned blue, as expected, but none of the O_ prefixed instructions do. That made me think they weren't being recognised, so I deleted them and went back to the original code.

I gather that they aren't included in the colour mapping file because they pre-date Arduino, but it's still a deficiency in the documentation that they aren't even mentioned as existing. ( bear in mind that I have no prior knowledge of C or C++, having always written previously in Assembly Language )

I have now re-coded all the sections of my code which use the SD Card functions and get no compiler errors, so When I next get access to the machine which runs the code, I'll upload the new version and test it.

Thanks for your help, Graham.

Ignore the colours. They are quite useless.