i/o file streams and data files opening and closing files writing to files parsing file input

37
I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input Writing to Binary Files Reading From Binary Files PROGRAMMING PROGRAMMING FUNDAMENTALS FUNDAMENTALS

Upload: kamaria-gyasi

Post on 02-Jan-2016

49 views

Category:

Documents


1 download

DESCRIPTION

PROGRAMMING FUNDAMENTALS. I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input Writing to Binary Files Reading From Binary Files. Objectives. After completing this chapter student should be able to:-. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

I/O File Streams and Data FilesOpening and closing files

Writing to FilesParsing File Input

Writing to Binary Files Reading From Binary Files

PROGRAMMING PROGRAMMING FUNDAMENTALSFUNDAMENTALS

Page 2: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

ObjectivesObjectives

• After completing this chapter student should be able to:-

•Create, read, write and update files ( Sequential file processing &Random-access file processing )

•Use high-performance unformatted I/O operations.

•Differentiate between formatted-data and raw-data file processing.

Page 3: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

File I/OFile I/O

• You can use the C++ I/O system to perform File Input and File Output ( I/O).

• The header <fstream>defines several important classes and values for File Input and File Output.

Page 4: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Opening and Closing a FileOpening and Closing a File

• A file is opened by linking it to a stream. There are three types of streams: – input, – output, and – input/output.

• To open an input stream, you must declare the stream to be of class ifstream.

• To open an output stream, it must be declared as class ofstream.

• A stream that will be performing both input and output operations must be declared as class fstream.

Page 5: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Opening and Closing a File(2)Opening and Closing a File(2)• This fragment creates one input stream,one output

stream, and one stream capable of both input and output:ifstream in; // input

ofstream out; // output

fstream both; // input and output

• Once you have created a stream, one way to associate it with a file is by using open( ).

ofstream out;

out.open("test");

– For short, you can writeofstream out("test");

Page 6: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Opening and Closing a File(3)Opening and Closing a File(3)

• If open( ) fails, the stream will evaluate to false when used in a Boolean expression.

• You can make use of this fact to confirm that the open operation succeeded.

if(!out) {

cout << "Cannot open file.\n";

// handle error

}

• You can also check to see if you have successfully opened a file by using the is_open( ) function,

Page 7: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Opening and Closing a File(4)Opening and Closing a File(4)

if(!out.is_open()) {

cout << "File is not open.\n";

• To close a file, use the member function close( ).out.close();

• The close( ) function takes no parameters and returns no value.

Page 8: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Reading and Writing Text FilesReading and Writing Text Files

// program writes an integer, a floating-point value, and a string to a file test:#include <iostream>#include <fstream>using namespace std;int main(){

ofstream out("test");if(!out) {

cout << "Cannot open file.\n";return 1;

}

cout << 10 << " " << 123.23 << "\n";

cout << "This is a short text file.";

cout.close();

return 0;

}

The easiest way to read from or write to a text file is to use the << and >> operators.

Page 9: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Reading and Writing Text Files(2)Reading and Writing Text Files(2)• The following program reads an integer, a

float, a character, and a string from the file created by the previous program:

Page 10: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

ExerciseExercise

Write a program that prompts user to enter:-

 (1) name (2)age(3)e-mail and (4)address

 

Read the data above and write into a file named biodata.txt as follows.

 

Name : Ahmad Ashraaf

Age: 19

Email: [email protected]

Adress: No 90 Taman Sri Kalongan, Terengganu.

Page 11: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

#include<iostream.h>

#include<fstream.h>

int main(void)

{

char name[20], email[20], address[80];

int age;

ofstream write;

write.open("biodata.txt",ios::app);

if(!write)

{ cout<<"cannot open file"<<endl;

return -1;

}

cout<<"Please enter name:"<<endl;//cin>>name;cin.getline (name,20);cout<<"Please enter age:"<<endl;cin>>age;cout<<"Please enter email:"<<endl;cin>>email;cout<<"Please enter address:"<<endl;cin>>address;

write<<"Name:"<<name<<endl;write<<"Age:"<<age<<endl;write<<"Email:"<<email<<endl;write<<"Address:"<<address<<endl;

write.close();

return 0;}

Page 12: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

//Some modification made to a previous program so that it can display content of any files that it takes as parameter

#include<iostream.h>

#include<fstream.h>

int main(int argc, char *argv[]) while(readfile) {

readfile>>line;

cout<<line<<endl;

{ }

char line[20]; readfile.close();

ifstream readfile; return 0;

readfile.open(argv[1]); }

if(!bacafail)

{

cout<<"cannot open file"<<endl;

return -1;

}

Page 13: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

• Parameter for main function– Consider the following program#include<iostream.h>

int main(int argc, char *argv[])

{

cout<<argc<<endl;

cout<<argv[1];

return 0;

}

– Rewrite the program above and named it as arg_main.cpp. Build the program (so it become arg_main.exe) and run the program via command prompt as follows. C:\> arg_main argumen1 argumen2.

Page 14: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Reading and Writing Text Files(3)Reading and Writing Text Files(3)

• When the >> operator is used for reading text files, certain character translations occur. Whitespace characters are omitted.

• To prevent any character translations, you must open a file for binary access.

• Also remember that when >> is used to read a string, input stops when the first whitespace character is encountered.

Page 15: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Unformatted Binary I/OUnformatted Binary I/O• Formatted text files (like those used in the preceding

examples) do not have the flexibility of unformatted binary files.

• C++ supports a number of binary (sometimes called “raw”) file I/O functions that can perform unformatted operations.

• Use the ios::binary mode specifier to perform binary operations.

• There are two ways to write and read unformatted binary data to or from a file.

1. function put( ) to write, function get( ) to read. 2. functions write( ) to write, function read( ) to read.

Page 16: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Unformatted Binary I/O(2)Unformatted Binary I/O(2)

Using get( ) and put( )• The get( ) function reads a single character from the

associated stream and puts that value in ch.• This value will be null if the end of the file is reached.• The put( ) function writes ch to the stream and

returns a reference to the stream.• The following program will display the contents of

any file on the screen. It uses the get( ) function.

Page 17: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

// Display a file using get().

#include <iostream>

#include <fstream>

using namespace std;

int main(int argc, char *argv[])

{

char ch;

if(argc!=2) {

cout << "Usage: PR <filename>\n";

return 1;

}

ifstream in(argv[1], ios::in | ios::binary);if(!in) {cout << "Cannot open file.\n";return 1;}while(in) { // in will be false when eof is reachedin.get(ch);if(in) cout << ch;}in.close();return 0;}

When in reaches the end of the file, it will be false, causing the while loop to stop.

Page 18: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

This program uses put( ) to write a string to a file:// Use put() to write to a file.#include <iostream>#include <fstream>using namespace std;int main(){

char *p = "hello there";ofstream out("test", ios::out | ios::binary);if(!out) {

cout << "Cannot open file.\n";return 1;}

while(*p) out.put(*p++);out.close();return 0;

}

Page 19: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Reading and Writing Blocks of DataReading and Writing Blocks of Data

• To read and write blocks of binary data, use the read( ) and write( ) member functions.

• Their prototypes are shown here:– istream &read(char *buf, streamsize num);– ostream &write(const char *buf, int streamsize num);

• The read( ) function reads num bytes from the associated stream and puts them in the buffer pointed to by buf.

• The write( ) function writes num bytes to the associated stream from the buffer pointed to by buf.

Page 20: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Reading and Writing Blocks of Data(2)Reading and Writing Blocks of Data(2)

• streamsize is defined as some form of integer.• The following program writes and then reads an

array of integers:• Note that the type casts inside the calls to read(

) and write( ) are necessary when operating on a buffer that is not defined as a character array.

Page 21: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

// Use read() and write().#include <iostream>#include <fstream>using namespace std;int main(){int n[5] = {1, 2, 3, 4, 5};register int i; ofstream out("test", ios::out | ios::binary); if(!out) { cout << "Cannot open file.\n";return 1;}

out.write((char *) &n, sizeof n); out.close(); for(i=0; i<5; i++) // clear array n[i] = 0; ifstream in("test", ios::in | ios::binary); if(!in) { cout << "Cannot open file.\n";return 1;}

in.read((char *) &n, sizeof n); for(i=0; i<5; i++) // show values read from file cout << n[i] << " "; in.close();return 0;}

Page 22: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Detecting EOFDetecting EOF

• You can detect when the end of the file is reached by using the member function eof( ), which has this prototype: bool eof( );

• It returns true when the end of the file has been reached; otherwise, it returns false.

• The following program uses eof( ) to display the contents of a file on the screen:

Page 23: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

// Detect end-of-file using eof().#include <iostream>#include <fstream>using namespace std;int main(int argc, char *argv[]){char ch;if(argc!=2) {cout << "Usage: PR <filename>\n";return 1;}ifstream in(argv[1], ios::in | ios::binary);if(!in) {cout << "Cannot open file.\n";return 1;}while(!in.eof()) { // use eof()

in.get(ch);

if(!in.eof()) cout << ch;

}

in.close();

return 0;

}

Page 24: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

A File Comparison ExampleA File Comparison Example

• The following program illustrates the power and simplicity of the C++ file system. It compares two files for equality.

• It does so by using the binary file functions read( ), eof( ), and gcount( ).

• gcount( ) function used to determine precisely how many characters are in the buffers.

• The program first opens the files for binary operations. Next, it reads one buffer at a time from each of the files and compares the contents.

Page 25: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

// Compare files.#include <iostream>#include <fstream>using namespace std;int main(int argc, char *argv[]){register int i;unsigned char buf1[1024], buf2[1024];if(argc!=3) {cout << "Usage: compfiles <file1> <file2>\n";return 1;}ifstream f1(argv[1], ios::in | ios::binary);if(!f1) {cout << "Cannot open first file.\n";return 1;}ifstream f2(argv[2], ios::in | ios::binary);if(!f2) {cout << "Cannot open second file.\n";return 1;}

Page 26: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

cout << "Comparing files...\n";do {f1.read((char *) buf1, sizeof buf1);f2.read((char *) buf2, sizeof buf2);if(f1.gcount() != f2.gcount()) {cout << "Files are of differing sizes.\n";f1.close();f2.close();return 0;}

// compare contents of buffersfor(i=0; i<f1.gcount(); i++)if(buf1[i] != buf2[i]) {cout << "Files differ.\n";f1.close();f2.close();return 0;}

} while(!f1.eof() && !f2.eof());cout << "Files are the same.\n";f1.close();f2.close();return 0;}

Page 27: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Random AccessRandom Access

• So far, files have always been read or written sequentially.

• You can also access a file in random order.• you perform random access using the seekg( ) and

seekp( ) functions. Their most common forms are shown here: – istream &seekg(off_type offset, seekdir origin);

– ostream &seekp(off_type offset, seekdir origin);

• off_type is an integer type defined by ios that is capable of containing the largest valid value that offset can have.

Page 28: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Random Access(2)Random Access(2)

• seekdir is an enumeration that has these values:

• The C++ I/O system manages two pointers associated with a file.

Page 29: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Random Access(3)Random Access(3)

• One is the get pointer, which specifies where in the file the next input operation will occur.

• The other is the put pointer, which specifies where in the file the next output operation will occur.

• Using the seekg( ) and seekp( ) functions, it is possible to access the file in a non-sequential fashion.

• Generally, random access I/O should be performed only on those files opened for binary operations.

Page 30: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Random Access(4)Random Access(4)• The following program demonstrates the

seekp( ) function.// Demonstrate random access.#include <iostream>#include <fstream>#include <cstdlib>using namespace std;int main(int argc, char *argv[]) {

if(argc!=3) {cout << "Usage: CHANGE <filename> <byte>\n";

return 1;}fstream out(argv[1], ios::in | ios::out | ios::binary);

if(!out) {cout << "Cannot open file.\n";

return 1;}

out.seekp(atoi(argv[2]), ios::beg);

out.put('X');

out.close();

return 0;

}

Page 31: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

• The next program uses seekg( ). // Display a file from a given starting point.

#include <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])

{

char ch;

if(argc!=3) {

cout << "Usage: NAME <filename> <starting location>\n";

return 1;

}

Page 32: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

ifstream in(argv[1], ios::in | ios::binary);

if(!in) {

cout << "Cannot open file.\n";

return 1;

}

in.seekg(atoi(argv[2]), ios::beg);

while(in.get(ch))

cout << ch;

return 0;

}

Page 33: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

• You can determine the current position of each file pointer by using these functions:

• pos_type tellg( );• pos_type tellp( );

Page 34: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

SummarySummary

• C++ views each file as a sequential stream of bytes.• Each file ends either with an end-of-file marker or at a

specific byte number recorded in a system maintained, administrative data structure.

• Header <fstream> includes the definitions for the stream class templates basic_ifstream (for file input), basic_ofstream (for file output) and basic_fstream (for file input and output).

• For an ofstream object, the file-open mode can be either ios::out to output data to a file or ios::app to append data to the end of a file (without modifying any data already in the file).

Page 35: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Summary(2)Summary(2)

• The file-open mode ios::ate opens a file for output and moves to the end of the file. This is normally used to append data to a file, but data can be written anywhere in the file.

• Existing files opened with mode ios::out are truncated (i.e., all data in the file is discarded).

• By default, ofstream objects are opened for output.

• The ofstream member function open opens a file and attaches it to an existing ofstream object.

Page 36: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Summary(3)Summary(3)

• You can use the ofstream member function close to close the ofstream object explicitly.

• Both istream and ostream provide member functions for repositioning the file-position pointer (the byte number of the next byte in the file to be read or written). These member functions are seekg ("seek get") for istream and seekp ("seek put") for ostream.

• The seek direction can be ios::beg (the default) for positioning relative to the beginning of a stream, ios::cur for positioning relative to the current position in a stream or ios::end for positioning relative to the end of a stream.

Page 37: I/O File Streams and Data Files Opening and closing files Writing to Files Parsing File Input

Summary(4)Summary(4)

• Member functions tellg and tellp are provided to return the current locations of the "get" and "put" pointers, respectively.

• Individual records of a random-access file can be accessed directly (and quickly) without the need to search other records.