files. 2 stream stream is a sequence of bytes input stream in input operations, the bytes are...

23
Files

Upload: benjamin-hall

Post on 23-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

Files

Page 2: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

2

FilesStreamStream is a sequence of bytesInput streamIn input operations, the bytes are transferred

from a device to the main memoryOutput streamThe bytes are transferred from main memory to a

device such as (display screen, printer, disk, n/w…)

Page 3: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

3

FilesStreamC++ contains different pre defined stream that

automatically opened when a program begins its execution.

Such as cin, cout ….Cin represents input stream connect to input

devices (object of istream)Cout represents output stream connect to output

devices (object of ostream)

Page 4: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

4

Files• Opening and Closing a File• In C++, to open a file by linking it to a stream. • There are three types of streams: input, output, and input/output.• To create an input stream, declare the stream to be of class ifstream. • To create an output stream, declare it as class ofstream. • Streams that will be performing both input and output operations

must be declared as class fstream. • For example, 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 io; // input and output

Page 5: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

5

FilesA file can be opened in two ways1) Using constructor function2) Using the member function open()First method is useful when we se only one file Second method used to manage multiple files

Page 6: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

6

FilesFile opening using constructorConstructor -> used to initialized an object.Here filename is used to initialize the file streamThis can be done1) Create a file stream object to manage the

stream (ie ofstream or from ifstream)2) Initialize the file object with filenameEg: ofstream o (“popo.txt”); open a file named popo.txt for output

Page 7: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

7

Filesofstream o (“popo.txt”);This create o as an ofstream object, to manage

output stream.This statement opene the file popo.txt an attaches

to output stream

Input stream exampleIfstream i(“popo.txt”);

Page 8: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

8

FilesTo close the fileo.close();i.close();

Page 9: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

9

FilesFile opening using constructor#include<iostream.h>#include<conio.h>#include<fstream.h>void main(){

ofstream o("popo.txt");cout<<"Enter";char name[20];cin>>name;o<<name;o.close();ifstream i("popo.txt");i>>name;cout<<name;i.close();

}

Page 10: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

10

Files2) Using the member function open()Open() fun is used to open multiple files SynFile_stream_class stream object;Stream object.open(“file name”);EgOfstream outfile;Outfile.open(“popo.txt”);Outfile.close();Outfile.open(“check.txt”);Outfile.close();

Page 11: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

11

Files• getline( )• Function that performs input is getline() . • It is a member of each input stream class.• getline(char array, streamsize num);• getline(char array, streamsize num, char delim);• The first form reads characters into the array • characters have been read, a newline character has been

found, or the end of the file has been encountered. • The second form reads characters into the array• the character specified by delim has been found, or the end of

the file has been encountered. • If the delimiter character is encountered in the input stream,

it is extracted, but is not put into array.

Page 12: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

12

Files#include<conio.h>#include<iostream.h>#include<fstream.h>void main(){

ofstream fout;fout.open("popo.txt");fout<<"popo";fout<<"peepe";fout.close();fout.open("check.txt");fout<<"looky";fout<<"zappy";fout.close();ifstream fin;fin.open("popo.txt");

char s[20];while(!fin.eof()){

fin.getline(s,20);cout<<s<<"\n";

}fin.close();fin.open(“check.txt");while(!fin.eof()){

fin.getline(s,20);cout<<s<<"\n";

}fin.close();getch();

}

Page 13: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

13

Files Copy one file to another#include<conio.h>#include<iostream.h>#include<fstream.h>void main(){

ofstream fout;ifstream fin;fout.open("popo.txt");char s[20];int i;for(i=1;i<=10;i++){

fout<<"popo"<<i<<"\n";}

fout.close();fout.open("check.txt");

fin.open("popo.txt");while(!fin.eof()){

fin.getline(s,20);fout<<s<<"\n";

}fin.close();fout.close();getch();

}

Page 14: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

14

FilesThe open() function to create new file/ open existing fileOpen() fun can take two arguments file name and file modeOpen(“filename”,mode);• filename is the name of the file; it can include a path specifier. • The value of mode determines how the file is opened. • It must be one or more of the following• ios::app• ios::ate• ios::binary• ios::in• ios::out• ios::trunccombine two or more of these values by ORing them together

Page 15: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

15

Files

• ofstream out;• out.open("test", ios::out);

Parameter Meaning

iios::app Append to EOF

iios::ate goto EOF on opening

iios::binary Binary file

iios::in open file for reading only

iios::out open file for writing only

ios::trunc delete the contents of the file if it exist

iios::nocreate open fails if the file does not exist

iios::noreplace open fails if the file already exist

Page 16: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

16

FilesFile pointersEach files having 2 pointers 1) Input pointer (get pointer)2) Output pointer (put pointer)Get pointer used to reading contents of given

file locationOutput pointer for writing to a given file locationBoth pointer increments automatically while

reading and writing

Page 17: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

17

FilesWhen a file open in read mode get pointer

points to BOFWhen a file open in write mode put pointer

points to BOF and delete existing contentsWhen a file open in append mode put pointer

points to the EOFThe file stream class support different functions to

control the movements of the get and put pointers

Page 18: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

18

Files

Ofstream fout;Fout.open(“check.txt”,ios::app);Cout<<“put pointer position”<<fout.tellp();Seekg()& Seekp()

Seekg() Moves get pointer to a specified locationSeekp() Moves put pointer to a specified locationTellg() Gives the current position of get pointerTellp() Gives the current position of put pointer

Seekg(offset,position);Seekp(offset,position);

Page 19: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

19

FilesOffset no of bytes to movePosition takes 3 values

Eg:fout.seekg(0,ios::beg);Goto StartFout.seekg(2,ios::cur);move 2 bytes forward

from current positionFout.seekg(-2,ios::end);move 2 bytes backword

from EOF

ios::beg from BOFios::cur From current positionios::end From EOF

Page 20: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

20

Files Seekg()#include<conio.h>#include<iostream.h>#include<fstream.h>void main(){

ofstream fout;ifstream fin;fout.open("popo.txt",ios::out);char s[20];int i;for(i=1;i<=10;i++)

fout<<"popo"<<i<<"\n";fout.close();fin.open("popo.txt",ios::in);while(!fin.eof()){

fin.seekg(2,ios::cur);fin.getline(s,20);cout<<s<<"\n";

}fin.close();getch();

}

Page 21: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

21

Filesput( ) and get( )• get() willread a character and • put() will write a character.• #include<conio.h>• #include<iostream.h>• #include<fstream.h>• void main()• {• fstream file;• file.open("p.txt",ios::out|ios::in);• int i;char s[20]="popo and peepe",c;• for(i=0;i<=10;i++)• file.put(s[i]);• file.seekg(0,ios::beg);• while(!file.eof())• { file.get(c);• cout<<c;• }• file.close();• getch();• }

Page 22: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

22

FilesRead() and write()For reading and writing block of data SynInfile.read((char *) & v,sizeof(v));outfile.write((char *) & v,sizeof(v));

Page 23: Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output stream

23

Files#include<conio.h>#include<iostream.h>#include<fstream.h>class check{

char name[20];int m1,m2,m3,tot;public:void getdata(){

cout<<"Enter name and 3 marks";cin>>name>>m1>>m2>>m3;tot=m1+m2+m3;

}void putdata(){

cout<<"\n"<<name<<" "<<m1<<" "<<m2<<" "<<m3;}

};

void main(){

clrscr();check c[20];int i,n;cout<<"enter limit ";cin>>n;fstream file("popo.txt",ios::in|ios::out);for(i=0;i<n;i++){

c[i].getdata();file.write((char*)& c[i],sizeof(c[i]));

}file.seekg(0,ios::beg);for(i=0;i<n;i++){

file.read((char *) & c[i],sizeof(c[i]));c[i].putdata();

}file.close();getch();

}