You are on page 1of 2

Writing Output to a File

Chapter 8 -
File I/O Similar to writing to screen
Use object connected to output file
Need the fstream header
#include <fstream>
Open file for writing
Declare object of ofstream class

1 2

Opening Files Writing to Files

 General form
General form
ofstream object_name (“file_name”);
object_name << variable_name;
 Choose object_name like variable name
Use ofstream object to write to file like
 object_name is object of class ofstream
cout was used
 Filename is where output will be stored
ofstream outfile(“C:\\salary.out”);
Can use full pathname “C:\\salary.out ”
outfile << “Salary for week was ” << money;

3 4

Example 1 Example 2
//Appending a file - using the ios::app mode to append a file
//Creating and writing to a file
#include <fstream> #include <fstream>
using namespace std; #include <string>
int main() #include <iostream>
{ using namespace std;
ofstream SaveFile("cpp-home.txt"); //a file is created int main()
//ofstream SaveFile; {
string str = "\This is the appending part";
//SaveFile.open ("cpp-home.txt");
ofstream myFile("cpp-home.txt ", ios::app);
SaveFile << "Hello World!"; //the file is written myFile << str <<endl;
SaveFile.close(); //the file is closed myFile.close();
return 0; return 0;
} }
5 6

Exercise 1 Exercise 1
Create a text file with the following requirement: #include <fstream>
#include <string>
#include <iostream>
1. The file name will be defined by the user. using namespace std;
2. The file should be in “append” mode int main()
{
string file; //char file[20];
With the file created, write your name to the file. cin >> file;
ofstream SaveFile(file.c_str(), ios::app); //ofstream SaveFile(file , ios::app);

SaveFile << “TARC KL”


SaveFile.close();
return 0;
}

7 8

1/2
Closing Files (input and output) Open File for Reading

General form Need fstream header


object_name.close ( ); #include <fstream>
Use C++ library function close Declare object of ifstream
Use both object and function name
Use ifstream object_name(“file_name”);
object_name( file_name );
separated by a period Use ifstream object to read file like cin
Example:
outfile.close( );

9 10

Reading From a File

Use ifstream object to read file like cin money.dat s1 s2 s3


used for keyboard 35 12 35 12 2
2 3.5 6.18 34
ifstream infile(“C:\\money.dat”); s4
infile >> salary1 >> salary2; infile >> s1 >> s2 >> s3; 3.5
C++ looks for whitespace between infile >> s4;
numbers
Newline character treated as whitespace Note: The number of data entries and their data types of variables
Additional reading continues sequentially should read should match the number and order within
the file!

11 12

Exercise 2 Exercise 2 - Solution

A text file named “number.txt” contains the


following input:
1 2 3 4 5 6
7 8 9 10 11 12

Create a file object and read the contents


of the file and assign them into an array.
Display the contents of the array on the
screen in the same row..
13 14

Example 3
Example 4 – Display the file contents

15 16

2/2

You might also like