KEMBAR78
Programming Fundamentals lecture-22.pptx
1
Programming Fundamentals
LECTURE-22
GHULAM ABBAS
abbas.sbcws@gmail.com
2
In the Previous Lecture
 TYPES OF DATA FILES
 FILE MODES
3
Today’s Lecture
TEXT FILE FUNCTIONS
Stream state member functions
File Pointer
File mode:File Open Mode
#include <fstream>
using namespace std;
int main()
{
ofstream outFile("file1.txt", ios::out);
outFile << "create a new file and will write this text!n";
outFile.close();
return 0;
}
If you want to set more than one open mode, just use the OR
operator- |. This way:
ios::ate | ios::binary
TEXT FILE FUNCTIONS
 Reading/writing a single character from/to file :
 get() – read a single character from text file and store in a buffer. e.g
file.get(ch);
 put() - writing a single character in text file. e.g. file.put(ch);
 Reading/writing a line from/to file:
 getline() - read a line of text from text file stored in a buffer. e.g
file.getline(s,80,”n”);
 <<(insertion operator) – write a line to a file. fin<<s;
 Reading/writing a word from/to file:
 char ch[20];
 fin.getline(ch,20, ‘ ‘);
 We can use file>>ch for reading and file<<ch writing a word in text file.
 The>> operator does not accept white spaces so it will stop when it
encounters a space after word and stores that word in ch.
A PROGRAM TO CREATE A TEXT FILE
#include<iostream>
#include<fstream>
using namespace std;
main()
{
ofstream fout("abc.txt");
fout<<"i am creating a new text filen";
fout<<"this text file contains alphabets and numbersn";
fout<<"Ghulam Abbasn";
fout.close();
}
The above program will create a text file “abc.txt” and store
two lines in it. You can store as many lines as you want.
A PROGRAM TO READ A TEXT FILE CHRACTER BY
CHARACTER
The above program will read a text file “abc.txt” one character at a time and display it on the
screen.
#include<iostream>
#include<fstream>
using namespace std;
main()
{
ifstream fin("abc.txt");
char ch;
while(!fin.eof())
{
fin.get(ch);
cout<<ch;
}
fin.close();
}
A PROGRAM TO READ A TEXT FILE WORD BY
WORD
The above program will read a text file “abc.txt” one word at a time and display it on the
screen.
#include<iostream>
#include<fstream>
using namespace std;
main()
{
ifstream fin("abc.txt");
char ch[20];
while(!fin.eof())
{
fin>>ch;
cout<<ch<<"t";
}
fin.close();
}
A PROGRAM TO READ A TEXT FILE LINE BY LINE
The above program will read a text file “abc.txt” one line at a time and display it on the screen.
#include<iostream>
#include<fstream>
using namespace std;
main()
{
ifstream fin("abc.txt");
char ch[80];
while(!fin.eof())
{
fin.getline(ch,80);
cout<<ch<<endl;
}
fin.close();
}
Stream state member functions
 In C++, file stream classes inherit a stream state member from
the ios class, which gives out the information regarding the
status of the stream.
 Example
eof() –used to check the end of file character
fail()- used to check the status of file at opening for I/O
bad()- used to check whether invalid file operations or
unrecoverable error .
good()- used to check whether the previous file operation
has been successful
File Pointer
 Input Pointer (get pointer)
 The input pointer is used for reading contents of a given file location.
 Output Pointer (put pointer)
 The output pointer is used for writing to a given file location.
 We can use these pointers to move through the files while reading or writing.
 The input pointer is used for reading the contents of a given file location and
 The output pointer is used for writing to a given file location.
File Pointer – Default Actions
 When a file opened in read-only mode, the input pointer is automatically set at
the beginning of the file.
 When a file is opened in write-only mode, the existing contents are deleted and
the output pointer is set at the beginning.
 When a file is opened in append mode, the output pointer moves to the end of
file.
Functions for Manipulations of File Pointers
 seekg( Moves get pointer (input) to a specified location.
 seekp( ) Moves put pointer(output) to a specified location.
 tellg( ) Gives the current position of the get pointer.
 tellp( ) Gives the current position of the put pointer.
Seek Function with Absolute Position
 infile.seekg(10);
 Moves the file pointer to the byte number 10.
 The bytes in a file are numbered beginning from zero. Therefore, the pointer pointing to
the 11th byte in the file.
Seek Function with Specifying the Offset
 seekg( offset, refposition);
 seekp( offset, refposition);
 The parameter offset represents the number of bytes the file pointer is to be moved from the
location specified by the parameter refposition.
 The refposition takes one of the following three constants defined in the ios class:
 ios : : beg start of the file
 ios : : cur current position of the pointer
 ios : : end end of the file
Example: Seek Function with Specifying the Offset
fout.seekg(0, ios : : beg); Go to start
fout.seekg(0, ios : : cur); Stay at the current position
fout.seekg(0, ios : : end); Go to the end of file
fout.seekg(m, ios : : beg); Move to (m+1)th byte in the file
fout.seekg(m, ios : : cur); Go forward by m bytes from the current position
fout.seekg(-m, ios : : cur); Go backward by m bytes from the current position
fout.seekg(-m, ios : : end) Go backward by m bytes from the end
Example: Seek Function with Specifying the Offset
#include <fstream>
#include <string.h >
using namespace std;
int main()
{
char str[80];
cout<<"enter a string ";
cin>>str;
int len=strlen(str);
fstream fs;
fs.open("abc.txt", ios::in |
ios::out);
for(int i=0; i<len;i++)
{
fs.put(str[i]);
}
fs.seekg(20,ios::beg);
char ch;
while(fs)
{
fs.get(ch);
cout<<ch;
}
return 0;
}
Example: Seek Function with Specifying the Offset
#include <iostream>
#include <fstream>
#include<iomanip>//for setw
using namespace std;
int main()
{
ofstream out_stream;
double d1, d2;
char file_name[20]; // cstring
cout << "Enter output file name: ";
cin >> file_name;
out_stream.open(file_name);
if (out_stream.fail())
{
cout << "Output file could not be opened.n";
exit(1);
}
out_stream.setf(ios::fixed);
//out_stream.setf(ios::showpoint);
cout << "Enter two numbers: ";
cin >> d1 >> d2;
out_stream << setprecision(3) << setw(10) << d1 <<
setw(10) << d1 * d1 << endl;
out_stream << setprecision(3) << setw(10) << d2
<<setw (10) << d2 * d2 << endl;
out_stream.close();
}

Programming Fundamentals lecture-22.pptx

  • 1.
  • 2.
    2 In the PreviousLecture  TYPES OF DATA FILES  FILE MODES
  • 3.
    3 Today’s Lecture TEXT FILEFUNCTIONS Stream state member functions File Pointer
  • 4.
    File mode:File OpenMode #include <fstream> using namespace std; int main() { ofstream outFile("file1.txt", ios::out); outFile << "create a new file and will write this text!n"; outFile.close(); return 0; } If you want to set more than one open mode, just use the OR operator- |. This way: ios::ate | ios::binary
  • 5.
    TEXT FILE FUNCTIONS Reading/writing a single character from/to file :  get() – read a single character from text file and store in a buffer. e.g file.get(ch);  put() - writing a single character in text file. e.g. file.put(ch);  Reading/writing a line from/to file:  getline() - read a line of text from text file stored in a buffer. e.g file.getline(s,80,”n”);  <<(insertion operator) – write a line to a file. fin<<s;  Reading/writing a word from/to file:  char ch[20];  fin.getline(ch,20, ‘ ‘);  We can use file>>ch for reading and file<<ch writing a word in text file.  The>> operator does not accept white spaces so it will stop when it encounters a space after word and stores that word in ch.
  • 6.
    A PROGRAM TOCREATE A TEXT FILE #include<iostream> #include<fstream> using namespace std; main() { ofstream fout("abc.txt"); fout<<"i am creating a new text filen"; fout<<"this text file contains alphabets and numbersn"; fout<<"Ghulam Abbasn"; fout.close(); } The above program will create a text file “abc.txt” and store two lines in it. You can store as many lines as you want.
  • 7.
    A PROGRAM TOREAD A TEXT FILE CHRACTER BY CHARACTER The above program will read a text file “abc.txt” one character at a time and display it on the screen. #include<iostream> #include<fstream> using namespace std; main() { ifstream fin("abc.txt"); char ch; while(!fin.eof()) { fin.get(ch); cout<<ch; } fin.close(); }
  • 8.
    A PROGRAM TOREAD A TEXT FILE WORD BY WORD The above program will read a text file “abc.txt” one word at a time and display it on the screen. #include<iostream> #include<fstream> using namespace std; main() { ifstream fin("abc.txt"); char ch[20]; while(!fin.eof()) { fin>>ch; cout<<ch<<"t"; } fin.close(); }
  • 9.
    A PROGRAM TOREAD A TEXT FILE LINE BY LINE The above program will read a text file “abc.txt” one line at a time and display it on the screen. #include<iostream> #include<fstream> using namespace std; main() { ifstream fin("abc.txt"); char ch[80]; while(!fin.eof()) { fin.getline(ch,80); cout<<ch<<endl; } fin.close(); }
  • 10.
    Stream state memberfunctions  In C++, file stream classes inherit a stream state member from the ios class, which gives out the information regarding the status of the stream.  Example eof() –used to check the end of file character fail()- used to check the status of file at opening for I/O bad()- used to check whether invalid file operations or unrecoverable error . good()- used to check whether the previous file operation has been successful
  • 11.
    File Pointer  InputPointer (get pointer)  The input pointer is used for reading contents of a given file location.  Output Pointer (put pointer)  The output pointer is used for writing to a given file location.  We can use these pointers to move through the files while reading or writing.  The input pointer is used for reading the contents of a given file location and  The output pointer is used for writing to a given file location.
  • 12.
    File Pointer –Default Actions  When a file opened in read-only mode, the input pointer is automatically set at the beginning of the file.  When a file is opened in write-only mode, the existing contents are deleted and the output pointer is set at the beginning.  When a file is opened in append mode, the output pointer moves to the end of file.
  • 13.
    Functions for Manipulationsof File Pointers  seekg( Moves get pointer (input) to a specified location.  seekp( ) Moves put pointer(output) to a specified location.  tellg( ) Gives the current position of the get pointer.  tellp( ) Gives the current position of the put pointer.
  • 14.
    Seek Function withAbsolute Position  infile.seekg(10);  Moves the file pointer to the byte number 10.  The bytes in a file are numbered beginning from zero. Therefore, the pointer pointing to the 11th byte in the file.
  • 15.
    Seek Function withSpecifying the Offset  seekg( offset, refposition);  seekp( offset, refposition);  The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter refposition.  The refposition takes one of the following three constants defined in the ios class:  ios : : beg start of the file  ios : : cur current position of the pointer  ios : : end end of the file
  • 16.
    Example: Seek Functionwith Specifying the Offset fout.seekg(0, ios : : beg); Go to start fout.seekg(0, ios : : cur); Stay at the current position fout.seekg(0, ios : : end); Go to the end of file fout.seekg(m, ios : : beg); Move to (m+1)th byte in the file fout.seekg(m, ios : : cur); Go forward by m bytes from the current position fout.seekg(-m, ios : : cur); Go backward by m bytes from the current position fout.seekg(-m, ios : : end) Go backward by m bytes from the end
  • 17.
    Example: Seek Functionwith Specifying the Offset #include <fstream> #include <string.h > using namespace std; int main() { char str[80]; cout<<"enter a string "; cin>>str; int len=strlen(str); fstream fs; fs.open("abc.txt", ios::in | ios::out); for(int i=0; i<len;i++) { fs.put(str[i]); } fs.seekg(20,ios::beg); char ch; while(fs) { fs.get(ch); cout<<ch; } return 0; }
  • 18.
    Example: Seek Functionwith Specifying the Offset #include <iostream> #include <fstream> #include<iomanip>//for setw using namespace std; int main() { ofstream out_stream; double d1, d2; char file_name[20]; // cstring cout << "Enter output file name: "; cin >> file_name; out_stream.open(file_name); if (out_stream.fail()) { cout << "Output file could not be opened.n"; exit(1); } out_stream.setf(ios::fixed); //out_stream.setf(ios::showpoint); cout << "Enter two numbers: "; cin >> d1 >> d2; out_stream << setprecision(3) << setw(10) << d1 << setw(10) << d1 * d1 << endl; out_stream << setprecision(3) << setw(10) << d2 <<setw (10) << d2 * d2 << endl; out_stream.close(); }