JdBP>> cin.getline(name, sizeof name) ; // Note: no EOF checking
JdBP>> if (!strcmp(name, "Cyber Con")) {
JdBP>> cout << "Hello " << name << endl ;
JdBP>> ofstream file("file1.fil", ios::out|ios::trunc) ;
JdBP>> file << name << endl ;
JdBP>> }
RS> Very interesting.... you don't even have to open the file
RS> explicitly.
The constructor opens the file. There are several constructors available for
ofstream objects. The one used above opens the named file.
RS> Isn't it a bit sloppy not closing explicitly?
No. "Sloppy" implies the possibility of a "leak" of some form. This is the
case if one were to use a `FILE *' variable and call fopen() but forget to
call fclose() before the variable went out of scope. This doesn't happen
with iostreams in C++ because by design the destructor cleans up and closes
the underlying file.
¯ JdeBP ®
--- FleetStreet 1.19 NR
---------------
* Origin: JdeBP's point, using Squish (2:440/4.3)
|