Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Essential C++    (ISBN: 0201485184)


 

 List Price: $33.95
 Our Price: $33.95
 Used Price: $12.00

 Release Date: 26 October, 1999
 Manufacturer: Addison-Wesley Pub Co (Paperback)
 Sales Rank: 69,295

 Author: Stanley B. Lippman









More Info

 Output to File 2002-01-31 16:15:10
  cpp 
Category: source:cpp:files
Description: Shows how to write text to a file in c++ using append or normal out
Platform: unix/win
Author: detour
Viewed: 2867
Rating: 4.3/5 (10 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
outfile.cpp
/* outfile.cpp by detour@metalshell.com
 *
 * Simple example of writing text to a file, by
 * using append or standard out.
 *
 * http://www.metalshell.com/
 */

#include <iostream>
#include <fstream.h>

/* for append type = 1
   for normal out type = 0 */

#define type 0

int main() {
	char outLine[50];

	ofstream outFile;
	/* open the file */
	if(type)
		outFile.open("output.txt", ios::app);
	else
		outFile.open("output.txt", ios::out);

	/* if opening didn't fail */
	if(!outFile.fail()) {
		cout << "Enter some text: ";
		cin >> outLine;

		/* write the text to the file */
		outFile << outLine << endl;
		outFile.close();
	} else 
		cout << "Error Opening File.";

	return 0;
}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?