Hi all,
I have a C function that writes some data to a text file. The data consists of floats, ints and strings. it looks something like this:
writeAsTextFile( mystruct_t* myStructWithIntsFloatsAndStrings , const char* fileName);
for doing this I use calls to fprintf;
Now I would like to write the same data but as binary. I could write a second function writeAsBinaryFile and use calls to fwrite instead. But then everytime I will make change the design of mystruct_t I will have to modify both writeAsTextFile and writeAsBinaryFile. And Of course the corresponding readAsTextFile and readAsBinaryFile. On top of this this will increase codesize. Therefore I would like to have one single generic function with one bin-or-text argument that would look like this:
writeToFile( mystruct_t* myStructWithIntsFloatsAndStrings , const char* fileName, myEnumType_t eOption)
where option would be an enum eBin = 0 and eTxt =1 for instance. Depending on eOption, the function would write binary or text data.
I am not sure what would be the best way to achieve this. Should I use fwrite also for writing as text, Should I try to use macros? (I have seen use of the ## directive somewhere but never used it ), or switch/ifs statements everywhere I need to write to file? Or shall I write a generic function like
myWriteFunction( void *data, char const type, myEnumType_t eOption)
that would be called by writeToFile?
I am not very familiar with using fread/fwrite and macros so any best practice comments, ideas etc is welcome,
Thanks
Baba