0% found this document useful (0 votes)
13 views17 pages

03 File IO

The document outlines the concept of File Input/Output (IO) in C++, detailing file operations such as reading from and writing to files using the iostream and fstream libraries. It explains how to open, close, read, and write data to files, including the use of different modes for file handling. Additionally, it provides examples of reading and writing data, including handling CSV files.

Uploaded by

monycc2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

03 File IO

The document outlines the concept of File Input/Output (IO) in C++, detailing file operations such as reading from and writing to files using the iostream and fstream libraries. It explains how to open, close, read, and write data to files, including the use of different modes for file handling. Additionally, it provides examples of reading and writing data, including handling CSV files.

Uploaded by

monycc2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Data structure and

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

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 Data txt
▪ 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>
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
Write data to file

8
Read data from file

9
Write data from user input to file

10
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 11
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();
} }
} } 12
StudentList.txt
ID Name Age
B101 Sok 17
B102 Sao 20
B109 Dara 18
B110 Seyha 22

Read all data from file

13
File IO
❑ Examples

▪ Read and write data from/to file

14
Q&A
15
Read CSV data

Mydata.csv

A C++ program to read file CSV data


using getline and stringstream 16
17

You might also like