0% found this document useful (0 votes)
25 views

2-File IO

Here are some tips for the assignments: 1. Use fstream to open file in read mode. Use getline() to read line by line. Use stringstream to extract data from each line. 2. Use fstream to open file in write mode. Use cout to input data. Use << to write to file. Close file after reading/writing. Handle file opening errors. Let me know if you have any other questions!

Uploaded by

Hai Kim Sreng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

2-File IO

Here are some tips for the assignments: 1. Use fstream to open file in read mode. Use getline() to read line by line. Use stringstream to extract data from each line. 2. Use fstream to open file in write mode. Use cout to input data. Use << to write to file. Close file after reading/writing. Handle file opening errors. Let me know if you have any other questions!

Uploaded by

Hai Kim Sreng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Algorithm and

Programming II
File IO (Input/Output) in C++

Program

1
Outline

▪ What is File IO? File extensions?

▪ File Operations
▪ Read data from file

▪ Write data to file

▪ Examples

“File IO refers to the transfer of data


either to or from a storage medium.” 2
File IO
❑ What?
▪ “File IO refers to the transfer of data either to or from a storage
medium.”

write

Program

read

3
File IO
❑ What?

▪ iostream library provides cin and cout methods for reading from keyboard
and writing/displaying on screen

▪ fstream library is used for writing and reading file


▪ ofstream : only for wring data to file

▪ ifstream : only for reading data from file

▪ fstream : can write/read data to/from file

#include <iostream>
#include <fstream>
2:45pm
4
File IO
❑ Opening a file
open(filename)
▪ To read or write file, we have to open a file first.
▪ We can use either ofstream or fstream open(filename, mode)

Mode Description ofstream file;


ios::app Append mode. Data is added more file.open(“filename.dat”)
ios::in Open file for reading.
ios::out Open file for writing.
fstream file;
If file does not exist, create a new file.
If file exists, content is overridden file.open(“filename.dat”, ios::out)

fstream file;
file.open(“filename.dat”, ios::in)
5
File IO
❑ Closing a file

▪ We should close file before terminate the program file.close( );

fstream file;
file.open(“filename.dat”, ios::out)
…………………..
//read/write code here
…………………..

file.close( );

6
File IO
❑ Writing data to a file
▪ We can use ofstream or fstream ofstream file;
for creating file variable file.open(“MyFile.dat”);
▪ Then use << to write data
▪ file<<data1<<data2<<endl;
fstream file;
file.open(“MyFile.data”, ios::out);
Functions for write data to file
Function Description fstream file;
file<<word; Write one data in word to file string filename=“MyFile.dat”;
file<<word1<<“\t” Write two data (word1 and word2) //file.open(filename, ios::out); //error
<<word2; separated by a tab to file
file.open(filename.c_str( ), ios::out);
Remark: You can write data to file just similar way you
display data using cout 7
Writing data to a file #include <fstream>
#include <iostream>
#include <fstream> using namespace std;
❑ Example
#include <iostream>
using namespace std; int main () {
string data; //char data[100];
int main () { // open a file in write mode.
string data; fstream file;
// open a file in write mode. file.open("filename.txt", ios::out);
ofstream file; //file.open("filename.txt", ios::app); //append
file.open("filename.txt");
//file.open(“filename.txt", ios::app); //append cout<<"Writing data to the file"<< endl;
cout<<"Enter your name: ";
cout<<"Writing data to the file"<< endl; ciin>>data;
cout<<"Enter your name: "; file<<data<<endl;
cin>>data;
file<<data<<endl; cout<<"Enter your age: ";
cin>>data;
cout<<"Enter your age: "; file<<data<<endl;
cin>>data;
file<<data<<endl; file.close();
}
file.close();
}
8
File IO
❑ Reading from a file
▪ We can use ifstream or fstream ifstream file;
for creating file variable file.open(“filename.dat”);

▪ Then use >> to read data


fstream file;
file.open(“filename.dat”, ios::in);
Functions for reading data from file
Mode Description
file>>word Read data from file one word at a time
file.eof( ) Return true when reach end of file (data has
been read till the end). Otherwise, return
false
file.get(ch) Read data from file one character at a time.
getline(file, line) Read data from file one line at a time. Return
false when no data to read 9
Reading from a file: One word at a time #include <fstream>
#include <fstream> #include <iostream>
❑ Example
#include <iostream> using namespace std;
using namespace std;
int main () {
int main () { string data;
string data;
// open a file in read mode.
// open a file in read mode. fstream file;
ifstream file; file.open("filename.txt", ios::in);
file.open("filename.txt");
if(!file){
if(!file){ cout<<"Error opening file OR file does not
cout<<"Error opening file OR file does not exist"<<endl; exist"<<endl;
}else{ }else{
cout << "Reading from the file" << endl; cout << "Reading from the file" << endl;
file >> data; file >> data;
cout << data << endl; cout << data << endl;
file >> data; file >> data;
cout << data << endl; cout << data << endl;

file.close(); file.close();
} }
} } 10
File IO
❑ Examples

▪ Read and write data from/to file

11
Write data to file

12
Write data from user input to file

13
Data all data from file

14
Write data from user input to file

15
Q&A
16
Deadline: 15th November, 11:59pm
Assignment 2 Submit to Ms. Team
What to submit: code.cpp, screenshot output

1-Design a program that read data from a


file called Employee List. The list has 25
employees. Each employee has name, age,
gender, salary, email.
Deadline: 15th November, 11:59pm
Assignment 2 Submit to Ms. Team
What to submit: Ex1.cpp, Ex2, cpp, screenshot output

2-Design a program that write data


program into file. The program ask
information of 10 employees then store in
file. Each employee has name, age,
gender, salary, email.

You might also like