Download - Cs1123 10 file operations
![Page 1: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/1.jpg)
File Operations(CS1123)
By
Dr. Muhammad Aleem,
Department of Computer Science, Mohammad Ali Jinnah University, Islamabad
2013
![Page 2: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/2.jpg)
What is a File?• A file is a collection of information, usually stored
on a computer’s disk. Information can be saved to files and then later reused.
• All files are assigned a name that is used for identification purposes by the operating system and the user.– E.g.,
MyProg.cpp, Marks.txt, etc.
![Page 3: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/3.jpg)
Basic File Operations• Opening a file• Writing to a file• Reading from a file• Close a file• …
![Page 4: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/4.jpg)
Writing to File
![Page 5: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/5.jpg)
Reading form File
![Page 6: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/6.jpg)
Setting Up a Program for File Input/Output• Before file I/O can be performed, a C++ program must
be set up properly.
• File access requires the inclusion of fstream.h
• A stream is a general name given to a flow of data.
• So far, we’ve used the cin and cout stream objects.
• Different streams are used to represent different kinds of data flow. For example: the ifstream represents data flow from input disk files.
![Page 7: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/7.jpg)
File I/O (files are attached to streams)
• ifstream – input from the file• ofstream – output to the file • fstream -input and output to the file
To use these objects, you should include header file, <fstream.h>
![Page 8: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/8.jpg)
Opening a File• Before data can be written to, or read from a file,
before that the file must be opened.
ifstream inputFile;inputFile.open(“customer.txt”);
Open function opens a file if it exist previously, otherwise it will create that file (in the current directory)
![Page 9: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/9.jpg)
Example (File opening)#include <iostream.h>
#include <fstream.h>
void main()
{
fstream dataFile; //Declare file stream object
char fileName[81];
cout<<"Enter name of file to open or create”; cin.getline(fileName, 81);
dataFile.open(fileName, ios::out );
cout << "The file “<<fileName<<" was opened.";
}File modes
![Page 10: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/10.jpg)
Default File Opening ModesFile Types Default Open Modes
ofstream
File is opened for output only. (Information may be written to file, but not read from the file.)
If the file does not exist, it is created. If the file already exists, its contents are deleted.
ifstream
The file is opened for input only. (Information may be read from the file, but not written to it.) File’s contents will be read from its beginning. If the file does not exist, the open function fails.
![Page 11: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/11.jpg)
File Mode Flag Meaning
ios::app
Append mode. If the file already exists, its contents are preserved and all output is written to the end of the file. By default, this flag causes the file to be created if it does not exist.
ios::ateIf the file already exists, the program goes directly to the end of it. Output may be written anywhere in the file.
ios::binaryBinary mode. When a file is opened in binary mode, information is written to or read from it in pure binary format. (The default mode is text.)
ios::noreplaceIf the file does not already exist, this flag will cause the open function to fail. (The file will not be created.)
![Page 12: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/12.jpg)
File Mode Flag Meaning
ios::inInput mode. Information will be read from the file. If the file does not exist, it will not be created and the open function will fail.
ios::nocreateIf the file does not already exist, this flag will cause the open function to fail. (The file will not be created.)
ios::out
Output mode. Information will be written to the file. By default, the file’s contents will be deleted if it already exists.
ios::trunc
If the file already exists, its contents will be deleted (truncated). This is the default mode used by ios::out.
![Page 13: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/13.jpg)
Opening a File at Declarationfstream dataFile(“names.dat”, ios::in | ios::out);
#include <iostream.h>#include <fstream.h> void main( ){
fstream dataFile("names.txt", ios::in | ios::out);cout << "The file names.txt was opened.\n";
}
![Page 14: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/14.jpg)
Testing for Open Errors
fstream dataFile;dataFile.open(“cust.dat”, ios::in);
if (!dataFile){
cout << “Error opening file.\n”;}
![Page 15: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/15.jpg)
Another way to Test for Open Errors
fstream dataFile;dataFile.open(“cust.dat”, ios::in);
if (dataFile.fail()){
cout << “Error opening file.\n”;}
![Page 16: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/16.jpg)
Closing a File• A file should be closed when a program is finished using it.
void main(void){fstream dataFile;dataFile.open("testfile.txt", ios::out);if (!dataFile){
cout << "File open error!”<<endl;return;
}cout << "File was created successfully\n";cout << "Now closing the file\n";dataFile.close();
}
![Page 17: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/17.jpg)
Using << to Write Information to a File• The stream insertion operator (<<) may be used to
write information to a file.
outputFile<<“I love C++ programming !”;
fstream object
![Page 18: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/18.jpg)
Example – File writing using <<#include <iostream.h>#include <fstream.h>void main(void){
fstream dataFile;dataFile.open("demofile.txt", ios::out);if (!dataFile){
cout << "File open error!" << endl;return;
} cout << "File opened successfully.\n";
cout << "Now writing information to the file.\n";dataFile << "Jones\n";dataFile << "Smith\n";dataFile << "Willis\n";dataFile << "Davis\n";dataFile.close();cout << "Done.\n";
}
![Page 19: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/19.jpg)
Program Screen Output
File opened successfully.Now writing information to the file.Done.
Output to File demofile.txtJonesSmithWillisDavis
![Page 20: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/20.jpg)
File Storage in demofile.txt
![Page 21: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/21.jpg)
Example – Append #include <iostream.h>#include <fstream.h>
void main(void){
fstream dataFile;
dataFile.open("demofile.txt", ios::out); dataFile << "Jones\n"; dataFile << "Smith\n"; dataFile.close(); dataFile.open("demofile.txt", ios::app); dataFile << "Willis\n"; dataFile << "Davis\n"; dataFile.close();
}
![Page 22: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/22.jpg)
JonesSmithWillisDavis
Example – Append (Output)
![Page 23: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/23.jpg)
File Output Formatting• File output may be formatted the same way as
screen output.
![Page 24: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/24.jpg)
File Output Formatting - Example#include <iostream.h>#include <fstream.h>
void main(void){
fstream dataFile;float num = 123.456;dataFile.open("numfile.txt", ios::out);if (!dataFile){
cout << "File open error!" << endl;return;
}
dataFile << num << endl;dataFile.precision(5);dataFile << num << endl;dataFile.precision(4);dataFile << num << endl;dataFile.precision(3);dataFile << num << endl;
}
![Page 25: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/25.jpg)
123.456123.45600123.4560123.456
File Output Formatting Example -Output
![Page 26: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/26.jpg)
Using >> to Read Information from a File
• The stream extraction operator (>>) may be used to read information from a file.
![Page 27: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/27.jpg)
#include <iostream.h> #include < fstream.h>
void main(void){
fstream dataFile;dataFile.open("demofile.txt", ios::in);if (!dataFile){
cout << "File open error!" << endl;return;
}cout << "File opened successfully.\n";cout << "Now reading information from the file.\n\n";
for (int count = 0; count < 4; count++){
dataFile >> name;cout << name << endl;
}dataFile.close();cout << "\nDone.\n"; }
Using >> to Read Information from a File
![Page 28: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/28.jpg)
File opened successfully.Now reading information from the file.
JonesSmithWillisDavis
Done.
Using >> to Read Information from a File (output)
![Page 29: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/29.jpg)
Detecting the End of a File• The eof() member function reports when the
end of a file has been encountered.
if (inFile.eof()) inFile.close();
![Page 30: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/30.jpg)
#include <iostream.h> #include <fstream.h>void main(void){
fstream dataFile;dataFile.open("demofile.txt", ios::in);if (!dataFile){
cout << "File open error!" << endl;return;
}cout << "File opened successfully.\n";cout << "Now reading information from the file.\n\n";
dataFile >> name; // Read first name from the filewhile (!dataFile.eof()){
cout << name << endl;dataFile >> name;
}dataFile.close();cout << "\nDone.\n";
}
Detecting the End of a File
![Page 31: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/31.jpg)
File opened successfully.Now reading information from the file.
JonesSmithWillisDavisDone.
Detecting the End of a File - Output
![Page 32: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/32.jpg)
Note on eof()• In C++, “end of file” doesn’t mean the program is
at the last piece of information in the file, but beyond it.
• The eof() function returns true when there is no more information to be read.
![Page 33: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/33.jpg)
Lecture: 16/12/13
![Page 34: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/34.jpg)
getline Member Function• dataFile.getline(str, 81, ‘\n’);
str – Name of a character array. Information read from file will be stored here.
81 – This number is one greater than the maximum number of characters to be read. In this example, a maximum of 80 characters will be read.
‘\n’ – This is a delimiter character of your choice. If this delimiter is encountered, it will cause the function to stop reading. This argument is optional If it’s left out, ‘\n’ is the default.)
![Page 35: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/35.jpg)
Example Program#include <iostream.h> #include <fstream.h>
void main(void){
fstream nameFile;char input[81];
nameFile.open(“Address.txt", ios::in);if (!nameFile){
cout << "File open error!" << endl;return;
} nameFile.getline(input, 81); // use \n as a delimiter
while (!nameFile.eof()){
cout << input << endl;nameFile.getline(input, 81); // use \n as a delimiter
}nameFile.close();
}
![Page 36: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/36.jpg)
Program Screen OutputMohammad Ai Jinnah UniversityIslamabad Expressway, Zone V, Kahuta RoadIslamabad Pakistan
![Page 37: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/37.jpg)
Program Example// This file shows the getline function with a user-// specified delimiter.
#include <iostream.h> #include <fstream.h>void main(void){
fstream dataFile("names2.txt", ios::in);char input[81];
dataFile.getline(input, 81, '$');while (!dataFile.eof()){
cout << input << endl;dataFile.getline(input, 81, '$');
}dataFile.close();
}
![Page 38: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/38.jpg)
Program OutputJayne Murphy47 Jones CircleAlmond, NC 28702
Bobbie Smith217 Halifax DriveCanton, NC 28716
Bill HammetPO Box 121Springfield, NC 28357
![Page 39: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/39.jpg)
Character IO
![Page 40: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/40.jpg)
The get Member Function
inFile.get(ch); //one character at a time
![Page 41: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/41.jpg)
Example Program// This program asks the user for a file name. The file is // opened and its contents are displayed on the screen.
#include <iostream.h> #include <fstream.h>
void main(void){
fstream file;char ch, fileName[51];cout << "Enter a file name: ";cin >> fileName;file.open(fileName, ios::in);if (!file){
cout << fileName << “ could not be opened.\n";return;
} file.get(ch); // Get a character
while (!file.eof()){
cout << ch;file.get(ch); // Get another character
}file.close();
}
![Page 42: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/42.jpg)
The put Member FunctionoutFile.put(ch);
//Writes or outputs one character at a time to the file
![Page 43: Cs1123 10 file operations](https://reader036.vdocument.in/reader036/viewer/2022062307/554fb037b4c90586258b50c0/html5/thumbnails/43.jpg)
Example Program// This program demonstrates the put member function.#include <iostream.h> #include <fstream.h>
void main(void){
fstream dataFile("sentence.txt", ios::out);char ch;
cout<< "Type a sentence and be sure to end it with a ";cout<< "period.\n";while (1){
cin.get(ch);dataFile.put(ch);if (ch == '.') break;
}dataFile.close();
}