Basics On File: Aiub. Spring 2018

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

BASICS ON FILE

AIUB. SPRING 2018

Dr. Mahbubul Syeed


Associate Professor, Department. of CS, AIUB
mahbubul.syeed@aiub.edu
www.msyeed.weebly.com

© Dr. Mahbubul Syeed, Associate Prof., AIUB


FILES IN C++

Files are text files in which can be created and manipulated (writing, reading, editing) using c++ file streams.
¡ ofstream: Stream class to write on files.
¡ ifstream: Stream class to read from files.
¡ fstream: Stream class to both read and write from/to files.

cin ≈ ifstream cout ≈ ofstream

© Dr. Mahbubul Syeed, Associate Prof., AIUB


FILES IN C++:
#include<iostream> - CREATING OR OPENING A FILE
#include<string> - WRITING TO A FILE
#include<fstream> // library for file input output
using namespace std;
int main(){
5
//this array of integers will be written into the file
7
int num[]={5,7,11,13,19};
11
// opening a output strean with the given file name.
// a plysical file with name firstFile.txt will be created.ofstream fout("firstFile.txt");
13
19
//is_open() function checks whether the file is successfully opened or not
if (fout.is_open()) {
//then writing the array in the file
for(int i=0;i<5;i++)
fout << num[i] << endl;
fout.close(); //now close the file stream
}

return 0;

}
© Dr. Mahbubul Syeed, Associate Prof., AIUB
#include<iostream>
FILES IN C++: READING INTEGERS FROM A FILE
#include<string>
#include<fstream> // library for file input output
using namespace std;
int main()
{ // opening a input stream over the file
ifstream fin("firstFile.txt");
int x; 5
7
// if file is successfully open then 11
if (fin.is_open()) 13
{
19
while(1)
{
fin>>x;
if(fin.eof()) break; // Checks if it’s an end of the file
cout<<x<<endl;
}
fin.close();
}
else
cout << "File does not exists!!" << endl;
return 0;
}
© Dr. Mahbubul Syeed, Associate Prof., AIUB
#include<iostream>
FILES IN C++: READING CHARACTERS FROM A FILE
#include<string>
#include<fstream> // library for file input output
using namespace std;
int main()
{ // opening a input stream over the file
I
ifstream fin("firstFile.txt"); L
char x; o
v
In a file firstFile.txt you have following text
// if file is successfully open then e
I Love programming
if (fin.is_open())
p
{
r
while(1)
o
{
fin>>x;
g
if(fin.eof()) break; // Checks if it’s an end of the file r
cout<<x<<endl; a
} m
fin.close(); m
} i
else
n
cout << "File does not exists!!" << endl;
g
return 0;
}
© Dr. Mahbubul Syeed, Associate Prof., AIUB
WRITING PROGRAMS

Write program to read characters from a file named ”alphabet.txt”.


Then write the vowels in one file named ”vowel.txt” and the others in ”consonent.txt”

© Dr. Mahbubul Syeed, Associate Prof., AIUB


#include<iostream>
#include<string>
#include<fstream> // library for file input output
using namespace std;

int main(){
// opening file in input mode to read char
ifstream fin("alphabet.txt");
//open two more file to store vowel and consonent respectively
ofstream fvowOut("vowel.txt");
ofstream fconsonentOut("consonent.txt");

char x; // store each char

// if all files are successfully open then


if (fin.is_open() && fvowOut.is_open() && fconsonentOut.is_open()) {
// read each char and process
while(1) {
fin>>x;
if(fin.eof()) break; // Checks if it’s an end of the file

if(x == 'a' || x == 'e' ||x == 'i' ||x == 'o' ||x == 'u' || x == 'A' || x == 'E' ||x == 'I' ||x == 'O' ||x == 'U’)
fvowOut << x;
else
fconsonentOut << x;
}
fin.close();
fvowOut.close();
fconsonentOut.close();
}
else
cout << "File does not exists!!" << endl;
© Dr. Mahbubul Syeed, Associate Prof., AIUB
return 0;
}
OTHER CODES FOR PRACTICE
FILE

© Dr. Mahbubul Syeed, Associate Prof., AIUB


#include<iostream> FILES IN C++:
#include<string> - CREATING OR OPENING A FILE
#include<fstream> // library for file input output - WRITING TO A FILE
using namespace std;
int main()
{
string text = "Writing following array: \n";
Writing following array:
int num[]={5,7,11,13,19}; 5
7
// opening a output strean with the given file name. 11
// a plysical file with name firstFile.txt will be created. 13
ofstream fout("firstFile.txt"); 19
if (fout.is_open()) {
fout << text; // writing the string to the file

//then writing the array in the file


for(int i=0;i<5;i++)
fout << num[i] << endl;

fout.close(); //now close the file


}
return 0;
} © Dr. Mahbubul Syeed, Associate Prof., AIUB
int main() FILES IN C++: READING FROM A FILE (2)
{
// opening a input stream over the file
ifstream fin("firstFile.txt");
string line;
Writing following array:
// if file is successfully open then 5
if (fin.is_open()) 7
{ 11
while ( getline (fin,line) ) 13
{ 19
cout << line << '\n';
}
fin.close();
}
else
cout << "File does not exists!!" << endl;
return 0;
}
© Dr. Mahbubul Syeed, Associate Prof., AIUB
FILES IN C++ : ERROR HANDLING FUNCTIONS

bad()
Returns true if a reading or writing operation fails. For example, in the case that we try to write to a file that is not open for writing
or if the device where we try to write has no space left.

fail()
Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is
extracted when we are trying to read an integer number.

eof()
Returns true if a file open for reading has reached the end.

good()
It is the most generic state flag: it returns false in the same cases in which calling any of the previous functions would return true.
Note that good and bad are not exact opposites (good checks more state flags at once).

© Dr. Mahbubul Syeed, Associate Prof., AIUB


FILES IN C++: PUT AND GET POINTER

get pointer: should move as we take input from a file


put pointer: should move as we write to a file.

But practically, get & put pointers move together.

© Dr. Mahbubul Syeed, Associate Prof., AIUB


FILES IN C++: FUNCTIONS RELATED TO PUT AND GET

1. seekg: Moves the get pointer to a specified byte.


2. seekp: Moves the put pointer to a specified byte.
3. tellg: Tells the present position (byte no.) of get pointer.
4. tellp: Tells the present position (byte no.) of put pointer.

In file, counting of bytes starts from zero (0).


Here, present position means next byte to read or write.
In a single file, at a time more than one file object/pointer should not work.

© Dr. Mahbubul Syeed, Associate Prof., AIUB


int main()
FILES IN C++: FUNCTIONS RELATED TO PUT AND GET
{
// opening a input stream over the file
ifstream fin("firstFile.txt");
char c;
if (fin.is_open()) // if file is successfully open then
{ W
fin >> c; r
cout<<c<<endl; 8
fin.seekg(1); 5
fin>>c;
cout<<c<<endl;
cout<<fin.tellg()<<endl;
fin.seekg(24);
fin >> c; Writing following array:
cout << c << endl; 5
fin.close(); 7
}
11
else
13
cout << "File does not exists!!" << endl;
19
return 0;
}
© Dr. Mahbubul Syeed, Associate Prof., AIUB
int main()
{
ofstream fout("test2.txt");
fout<<10<<endl;
fout.close();
ifstream fin("test2.txt"); Output:
int x;
fin>>x;
cout<<x<<endl; 10
fin.seekg(1); 0
fin>>x;
cout<<x<<endl; 2
cout<<fin.tellg()<<endl;
return 0;
}

© Dr. Mahbubul Syeed, Associate Prof., AIUB

You might also like