Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please explain the difference b 1

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US


Please explain the difference between [tt]ifstream[/tt] and [tt]ofstream[/tt].

What do they each do?
 
ifstream is for file input, and ofstream is for file output(in file stream and out file stream respectively). They allow you to read and write from files.
Code:
ifstream in(filename_in, ios::in);
ofstream out(filename_out, ios::out);
if(!in||!out){
  cerr<<&quot;error: input or output&quot;;
  exit(1);
}
char ch[100];
while(in.getline(ch,100)){
  out<<ch<<endl;
}
in.close();
out.close();

There's a quick example of a segment of code that will copy a file from [filename_in] to [filename_out]. I hope that helped.

p.s. The destructors for ifstream and ofstream automaticaly close the stream, so in.close() and out.close() are a bit redundant, but it's good programming practice to close your streams. There are some cases where data can be lost if you don't. MYenigmaSELF:-9
myenigmaself@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top