Tech Blog   »   Home   »   Coders Forum   »   Computer Directory   »   Math Calculators   »   RSS:Directory|Forum 

Programs - Source Code C++ fstream file object

To copy code, highlight with mouse, copy, and paste.



File Streams: use <fstream> library for file objects.
// - Write out to a file.
#include <iostream>
#include <fstream> // Step # 1
using namespace std;
int main()
{
	int num1 = 11;
	int num2 = 22;
	int num3 = 33;

	// Step #2 - Declare an output file object file.
	ofstream outFile;

	// Step #3 - Open the output file destination.
	outFile.open("c:\\x\\data.txt");

	// Check for success.
	if (outFile.fail())
	{
		cout << "Error opening \"data.txt.\" for output.\n";
		return 1;
	}

	// Step #4 - Write to file.
	outFile << num1 << endl << num2 << endl << num3 << endl;

	// Print to screen.
	cout << num1 << endl << num2 << endl << num3 << "\n\n";

	// Successful message.
	cout << "The data has been written to the file.\n\n";

	// Step #5 - Close the output file object.
	outFile.close();

	return 0;
}
/*==================[output]============================================
11
22
33

The data has been written to the file.

Press any key to continue . . .
========================================================================*/



// - Get a file - inFile.
#include <iostream>
#include <fstream>  // Step # 1
using namespace std;
int main()
{
	int num1 = 0;
	int num2 = 0;
	int num3 = 0;

	// Step #2 - Declare an input file object file.
	ifstream inFile;

	// Step #3 - Open the input file object.
	inFile.open("c:\\x\\data.txt");

	// Check for success.
	if (inFile.fail())
	{
		cout << "Error opening \"data.txt.\" for input.\n";
		return 1;
	}

	// Step #4 - Read the inFile.
	inFile >> num1 >> num2 >> num3;

	// Print to screen.
	cout << num1 << endl << num2 << endl << num3 << endl << endl;

	// Successful message.
	cout << "The data has been read from the \"data.txt\" file.\n\n";

	// Step #5 - Close the input file object.
	inFile.close();

	return 0;
}
/*==================[output]============================================
11
22
33

The data has been read from the "data.txt" file.

Press any key to continue . . .
========================================================================*/



// - Read an entire file.
// - Create folder "x" on the "c" drive and put the .txt file in folder "x".
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	char character;
	ifstream inFile;

	inFile.open("c://x//kennedy.txt");

	if (inFile.fail())
	{
		cout << "Error opening file.\n";
		exit(1);
	}

	while (!inFile.eof())
	{
		inFile.get(character);
		cout << character;
	}

	inFile.close();

	return 0;
}
/*=====================[output]===========================================
Information on Kennedy's assination:

The assassination of John F. Kennedy, the thirty-fifth President
of the United States, took place on Friday, November 22, 1963,
in Dallas, Texas, at 12:30 p.m. Central Standard Time (18:30 UTC)
in Dealey Plaza. Kennedy was fatally shot while riding with his wife
Jacqueline in a Presidential motorcade.

Press any key to continue . . .
=========================================================================*/

C++ Links


Code Examples:
Variables
Strings
Arrays

Functions:
Return Values
Pass by Value
Pass by Reference

Loops:
While loop
Do-while
For loops

Arrays:
Basic Array






More C++ source code

Forum Area maSpinner

The forum contains more programming examples, registration is free!





Validated with no errors:

Valid XHTML 1.0 Transitional     Valid CSS!


Site Map