Binary file handling using C functions
-
Hi All,
I am used to coding in C and started learning Qt recently. I have seen some posts on binary file handling but to me, it appeared quite cumbersome (also my knowledge on C++ classes and function is pretty limited. I used the old _open, _read, and _write functions and was able to do just what I wanted. However, I am not sure if the above functions are deprecated (I searched but could not really find). If these are not deprecated, I would like to use them as they are pretty straight forward. My question is, is this an accepted method? Does Qfile object offer better leverage in terms of handling the data (generally my binary data is in the form of structures). I read in one post that structure needs to be serialized, etc. Highly appreciate any comments on the above. Thanks. -
There will be real ramp-up time to learn C++ and use it effectivley, even if you already know C. Unless there is a reason to, I would discourage using C/POSIX functions when there are C++ or framework tools that are available. I think you've got a bit of self-instruction to do before jumping right into C++ file coding.
std::C++ streams and file IO
Qt streams and file IO
object serialization
effective class use and inheritancethese are all things you need to teach yourself.
-
Hi Kent-Dorfman,
Thank you very much for your advice. Just for clarification, is there any specific reason to not to use C/POSIX function calls? I await your reply. Thanks once again. -
Hi Kent-Dorfman,
Thank you very much for your advice. Just for clarification, is there any specific reason to not to use C/POSIX function calls? I await your reply. Thanks once again.Fair enough. A best-practice comp-sci paradigm is to use the highest level construct that meets the requirements. The C++ and Qt framework classes abstract the operations to a higher and arguably more intuitive level.
I've written plenty of C++ wrapper classes around POSIX code over the years, but then learned the pure C++ mechanisms and they were better tested and easier for others to understand when they have to look at it.
Also, the higher level constructs will usually be more platform neutral, which reduces rework when you have to port to a different platform.
-
@parthasaradhi
I used to be a C programmer, and could happily use_open/read/write
etc. level functions (or the Cstdio
wrappers to them). However, they are no longer the right level to use for new programs. As @Kent-Dorfman has indicated, you could use either the C++ functions or the Qt ones, either would be acceptable. For file handling they are not hard to learn, spend a few minutes looking through either and you should be good to go. -
Fair enough. A best-practice comp-sci paradigm is to use the highest level construct that meets the requirements. The C++ and Qt framework classes abstract the operations to a higher and arguably more intuitive level.
I've written plenty of C++ wrapper classes around POSIX code over the years, but then learned the pure C++ mechanisms and they were better tested and easier for others to understand when they have to look at it.
Also, the higher level constructs will usually be more platform neutral, which reduces rework when you have to port to a different platform.
@kent-dorfman Thank you for your reply.
-
@jonb Thank you very much for your advice.