TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ROGER SCUDDER
from: JONATHAN DE BOYNE POLLARD
date: 1998-02-27 19:54:00
subject: C++ Saveding stuff

 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)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.