Thread: File handling and header files?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    File handling and header files?

    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?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    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:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    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'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compiling source files and header file at once?
    By 1jackjack in forum C Programming
    Replies: 10
    Last Post: 05-04-2007, 11:06 AM
  2. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  3. Error Handling and Header Files
    By the pooper in forum C Programming
    Replies: 10
    Last Post: 01-03-2005, 01:40 AM
  4. #define Header files. End Of File Errors.
    By bartybasher in forum C Programming
    Replies: 8
    Last Post: 07-28-2003, 03:03 PM
  5. Does anyone have a bitmap handling header file
    By genghis in forum Windows Programming
    Replies: 2
    Last Post: 01-17-2002, 11:11 PM