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