Chapter 6 File Input and Output
Chapter 6 File Input and Output
Programming
Chapter 6
File Input and Output
Tesfaye B
1
Outlines
Creating a file
Opening and Closing a file
Reading and writing text files
2
Introduction
Storage of data in memory is temporary.
retention of data.
sequential files
random-access files.
3
Introduction cont…
using the standard C++ library called fstream.
Can crate, open, and modify text files.
Data types for fstream:
ifstream
to read information from files.
ofstream
to create files and write information to the files.
fstream
to create files,
write information to files, and
read information from files.
4
Creating a file
Steps :
Include the header file fstream
#include<fstream>
Declare file stream object
fstream Obje_name;
fstream exampleFile;
Open the file with the file stream object
Obje_name.open(“filename.text”);
exampleFile.open(“examplefile.text”);
Use the file stream object with >>, <<
Obje_name << string_variables of the message
Close the files.
obje_name.close()
5
Example: Create text file named “example1.text” with
message “file creation example”
#include <iostream>
#include <fstream>// include fstream standard file
using namespace std;
int main()
{
ofstream fdec;// declaration of object with type ofstream
fdec.open("example1.txt");// crate and open file
return 0;
}
6
Output:
Text file named “example” will be created
Opening the file we can see the message “ file creation
example”
7
Open and modify
#include <iostream>
#include <fstream>// include fstream standard file
using namespace std;
int main()
{
ofstream fdec;// declaration of object with type ofstream
fdec.open("example1.txt");// crate and open file
string str ="file creation example";//message
fdec<< str;//Write string to the file
fdec.close();
//open and modify
fdec.open("example1.txt");
fdec <<"additional message";//replace the previous message
fdec.close();
return 0;
8 }
Output
9
Opening and reading file
A file must be opened before you can read from it or write to it.
ofstream or fstream object
void open(const char *filename, ios::openmode mode);
the first argument specifies the name and location of the file to be
opened and
the second argument of the open() member function defines the
mode in which the file should be opened.
10
Mode
ios::app
Append mode. All output to that file to be appended to the end.
ios::ate
Open a file for output and move the read/write control to the end of
the file.
ios::in
Open a file for reading.
ios::out
Open a file for writing.
ios::trunc
If the file already exists, its contents will be truncated before opening
the file.
11
Opening and reading file steps
Steps :
Include the header file fstream
#include<fstream>
Declare file stream object
fstream Obje_name;
fstream exampleFile;
Open the file with the file stream object
Obje_name.open(“filename.text”);
exampleFile.open(“examplefile.text”);
Close the files.
obje_name.close()
exampleFile.close
12
Example: open and read from
example1file.
// open and read
ifstream readObj; // declare object to read
readObj.open("example1.txt"); // open the file
string readMessage; // variable to store the message
readObj >> readMessage; // reading message
cout<< readMessage; // display the message
Output
additional
13
More Examples
Program to write in a text file
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outObj;// create object
outObj.open("fileOut.txt");// open fileOut.text
outObj << "hello world!";// write on the file
outObj.close();// close the file
return 0;
}
14
Program to read from text file and display it
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream readObj;
//open file from C:\Users\user\Desktop\New folder\example3
readObj.open("C:\\Users\\user\\Desktop\\New folder\\example3\\fileOut.txt");
char ch;
while(!readObj.eof())//check end of file
{
readObj.get(ch);// get each character
cout << ch;// display characters
} Output
readObj.close();// close the file
return 0; hello world!
}
15
Characters counter
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream readObj; //open file from C:\Users\user\Desktop\New folder\example3
readObj.open("C:\\Users\\user\\Desktop\\New folder\\example3\\fileOut.txt");
char ch;
int counter =0;
while(!readObj.eof())//check end of file
{
readObj.get(ch);// get each character
counter ++; // increment
}
cout<<" number of character in the message is : "<<counter;
readObj.close();// close the file
return 0; Output
}
16 number of character in the message is : 13
Program to copy contents of file to another file.
int main()
{
//open file from C:\Users\user\Desktop\New folder\example3
ifstream readObj;
readObj.open("C:\\Users\\user\\Desktop\\New folder\\example3\\fileOut.txt");
char ch;
while(!readObj.eof())//check end of file
{
readObj.get(ch);// get each character from fileOut.txt
newObj << ch; // paste on newfile.txt
}
readObj.close();// close the file
newObj.close();
return 0;
}
17
Assignment (10%)
Program to learn how to use function, file and structure
together
Question
Write a c++ program which will store student information and
grading
Create c++ structure to store
Student name, Id, Faculty,Year of registration
Student course information for one semester
o Include: course code, course No, credit Hour
Students marks and Grade for each courses
o Quize1 Quize2, Quize3,Assignment, lab, finalexam, and Grade
18
Assignment (10%) cont…
Functions:
Use function for the implementation
o Function to get student info
o Function to get course registration
o Function to get student mark and grade
o Function to generate report and store it on .text file
File application
Create file to store all information
o Student info file
o Student registered course info file
o Student grade file
19
Reference
How to program in C++, Deitel and PJ.Deitel, chapter14
20