-
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
-
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.
-
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
-
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).
-
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