newbie C++ question

This is a discussion on newbie C++ question within the C++ Programming forums, part of the General Programming Boards category; Hi I'm just trying out some code from a teach-yourself book. What I can't understand is why when I use ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    3

    newbie C++ question

    Hi

    I'm just trying out some code from a teach-yourself book. What I can't understand is why when I use just the header:

    Code:
    #include <fstream.h>
    the code won't compile, whereas if I use:

    Code:
    #include <fstream.h>
    #include <iostream.h>
    it *will*. This doesn't make any sense to me, since the line

    Code:
    #include <iostream.h>
    is in the file "fstream.h". It mentions this in my book, noting that "you don't need to include iostream explicitly".

    Here's the errors I get:

    cd /home/iu/C++/FileIO/
    g++ file_io.cxx -o fileio
    file_io.cxx: In function `int main()':
    file_io.cxx:7: `cout' undeclared in namespace `std'
    file_io.cxx:8: `cin' undeclared (first use this function)
    file_io.cxx:8: (Each undeclared identifier is reported only once for each
    function it appears in.)
    file_io.cxx:10: `ofstream' undeclared (first use this function)
    file_io.cxx:10: parse error before `(' token
    file_io.cxx:11: `fout' undeclared (first use this function)
    file_io.cxx:12: `cout' undeclared (first use this function)

    Compilation exited abnormally with code 1 at Mon Jan 12 23:19:45

    And here's the code:

    Code:
    #include <fstream.h>
    
    int main()
    {
      char fileName[80];
      char buffer[255]; //for user input
      cout << "File name: ";
      cin >> fileName;
    
      ofstream fout(fileName); //open for writing
      fout << "This line written directly to the file...\n";
      cout << "Enter text for the file: ";
      cin.ignore(1,'\n'); //eat the new line after the file name
      cin.getline(buffer,255); //get the user's input
      fout << buffer << "\n"; //and write it to the file
      fout.close(); //close the file, ready for reopen
      return 0;
    }
    As you see, I'm using g++ (3.2.2) on linux.

    Cheers

    Ian

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    If you need cout, #include <iostream> (note the lack of ".h"). If you need file streams, #include <fstream>.

    Thanks to preprocessor guards, it's impossible to include a library twice, so you really don't have to worry if one of those libraries includes the other or something like that.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    3
    OK. I can get the code to compile if I include both fstream and iostream manually, but why doesn't it read in

    Code:
    #include <iostream.h>
    from fstream if I include only that file?

    I have another question. I can use the header

    Code:
    #include <iostream>
    #include <fstream>
    but only if I also add the line

    Code:
    using namespace std;
    I read that this is bad practice. How can I do this more professionally?

    Cheers

    Ian

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Using the standard header files is better practice than using the old ones (with the .h), so its good that you are doing that. Putting "using namespace std;" works and is legal, but I prefer putting "std::" in front of everything I use that is part of the std namespace. You could also put "using std::cout;" or something similar for whatever you are using, and place it after the #includes or inside a code block.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using std::cout;
    
    int main()
    {
        {
            using std::cin;
            std::string fileName;
            cout << "File name: ";
            cin >> fileName;
        }
    
        std::ofstream fout(fileName); //open for writing
        fout << "This line written directly to the file...\n";
        cout << "Enter text for the file: " << std::endl;
        std::cin.ignore(1,'\n'); //eat the new line after the file name
        std::string buffer;
        std::getline(std::cin, buffer); //get the user's input
        fout << buffer << std::endl; //and write it to the file
        fout.close(); //close the file, ready for reopen
    }
    Also, your book is old if it teaches the old headers, so its not surprising it tells you to do the not so smart thing of only including fstream.h to get iostream.h also. In general, it is better to explicitly include every header file that you are using, since as joshdick stated it can't be included twice and because you can't count on any implementation nesting includes in any particular way (as you have found out).

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    3
    Ok. Thanks jlou. It's always good to know when you've been given some dumb advice! (The book is Sam's C++ for Linux, btw, published in 2000, but just a partial re-write of an older book, as far as I can tell.)

    I

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 05:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 10:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21