hello everyone,
getting straight to the point can we use file handling in header files? meaning can i include fstream.h in a header file and use it?
hello everyone,
getting straight to the point can we use file handling in header files? meaning can i include fstream.h in a header file and use it?
You can include header files in other header files... but you should only do so if there is something in the header file that requires the other header file to be included. What do you mean by "use it"?
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Header files can include other header files - yes.
Now what do you mean by "use"?
Because code in header files is almost always a really bad thing.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Why would you think you can't include it? What leads you to that strange assumption? You only have to look at files in your compiler include folder to see the sort of things that are in good quality headers, however incomprehensible it may look, I can remeber trying to work out how to use OpenGL and other things by examining the gl.h and glut headers, hopeless idea as I had little knowledge at that time of anything i was looking at and how openGL might even work or what was needed to write in it or make things appear on screen with it, I just knew it was for graphics, but it certainly pushed me to try and make sense of what I was looking at. Be inqusitive. It is routine for a header file to include other header files, if that was not possible then the preprocessor lines in your source files would be much longer for one thing.
Why do you especially think that fstream cannot be included in a header? Or is it as Salem suggests that you are wishing to code your file i/o in your header?
In general use headers for class declarations declare funtion prototypes etc, the definition (working code) is implemented in your associated source file. So you might strictly adhere to 'one source file' 'one header file' which has its merits, eg in a statemachine MenuState.cpp has a header MenuState.h
Last edited by rogster001; 12-20-2011 at 03:38 PM. Reason: Typo on Class in header advice
Thought for the day:FLTK: "The most fun you can have with your clothes on.""Are you sure your sanity chip is fully screwed in sir?" (Kryten)
Stroustrup:
"If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"