File I/O in C++

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 24

File I/O in C++

Using Input/Output Files


A computer file is stored on a secondary storage device (e.g., dis !" is permanent" can #e used to provide input data to a program or receive output data from a program, or #ot$" s$ould reside in %ro&ect directory for easy access" must #e opened #efore it is used.

'eneral File I/O (teps


)eclare a file name varia#le Associate t$e file name varia#le *it$ t$e dis file name Open t$e file Use t$e file Close t$e file

Using Input/Output Files

stream - a sequence of characters interactive (iostream! cin + input stream associated *it$ keyboard. cout + output stream associated *it$ display. file (fstream! ifstream + defines ne* input stream (normally associated *it$ a file!. ofstream + defines ne* output stream (normally associated *it$ a file!.

(tream I/O ,i#rary -eader Files


.ote/ 0$ere is no 1.$2 on standard $eader files / 3fstream4 iostream ++ contains #asic information re5uired for all stream I/O operations iomanip ++ contains information useful for performing formatted I/O *it$ parameteri6ed stream manipulators fstream ++ contains information for performing file I/O operations strstream ++ contains information for performing in+memory I/O operations (i.e., into or from strings in memory!

Classes for (tream I/O in C++

ios is the base class. istream and ostream inherit from ios ifstream inherits from istream (and ios) ofstream inherits from ostream (and ios) iostream inherits from istream and ostream (& ios) fstream inherits from ifstream, iostream, and ofstream

C++ streams
#include <fstream> int main (void) { //Local declarations ifstream fs n! ofstream fs"ut! . . . return #! $

O#&ect and 7em#er Functions


Stream handle Name Member Function Name

input_stream.open("numbers.dat" )
Calling Object Dot Operator File Name

File I/O 89ample/ :riting


;include 3fstream4 using namespace std" int main(void! < ofstream outFile(1fout.t9t=!" outFile 33 =-ello :orld>=" outFile.close(!" return ?" @

File I/O 89ample/ :riting


;include 3fstream4 using namespace std" int main(void! < ofstream outFile" outFile.open(1fout.t9t2!" outFile 33 1First line2" //#e$ave &ust li e cout outFile.close(!" outFile331Anot$er line233endl" //AA return ?" @

File I/O 89ample/ Beading


;include 3iostream4 ;include 3fstream4 int main(void! < ifstream openFile(1data.t9t=!" //open a te9t file data.t9t c$ar c$" *$ile(>OpenFile.eof(!! < OpenFile.get(c$!" cout 33 c$" @ OpenFile.close(!" return ?" @

File I/O 89ample/ Beading


;include 3iostream4 ;include 3fstream4 ;include 3string4 int main(void! < ifstream openFile(1data.t9t=!" //)eclare and open a te9t file string line" *$ile(>openFile.eof(!! < getline(openFile,line!"//fetc$ line from data.t9t and put it in a string cout 33 line" @ openFile.close(!" return ?" @

File I/O 89ample/ Beading


#include iostream! #include fstream! #include string! int main"#oid$ % ifstream openFile"&data.t't($) **open a te't file data.t't string line) if"openFile.is+open"$$% ** ,hile"-openFile.eof"$$% getline"openFile.line$)**read a line from data.t't and put it in a string cout line) / else% cout &File does not e'ist-0 endl) e'it"1$)/ / openFile.close"$) return 2) /

7ore Input File+Belated Functions


ifstream fsin) fs3n.open"const char45 fname$ connects stream fs3n to t$e e9ternal file fname. fs3n.get"char6 character$ e9tracts ne9t c$aracter from t$e input stream fs3n and places it in t$e c$aracter varia#le character. fs3n.eof"$ tests for t$e end+of+file condition.

7ore Output File+Belated Functions


ofstream fsOut) fsOut.open"const char45 fname$ connects stream fsOut to t$e e9ternal file fname. fsOut.put"char character$ inserts c$aracter character to t$e output stream fsOut. fsOut.eof"$ tests for t$e end+of+file condition.

File Open 7ode


.ame ios//in ios//out ios//app ios//ate ios//trunc ios//nocreate ios//noreplace ios//#inary Open file to read Open file to *rite All t$e date you *rite, is put at t$e end of t$e file. It calls ios//out All t$e date you *rite, is put at t$e end of t$e file. It does not call ios//out )eletes all previous content in t$e file. (empties t$e file! If t$e file does not e9ists, opening it *it$ t$e open(! function gets impossi#le. If t$e file e9ists, trying to open it *it$ t$e open(! function, returns an error. Opens t$e file in #inary mode. )escription

File Open 7ode


#include <fstream> int main(void) { ofstream out%ile(&file'.t(t&, ios))out)! out%ile << &*hat+s ne,-.n&! out%ile.close()! /eturn #! $
f 0ou ,ant to set more than one o1en mode, 2ust use the OR o1erator- |. *his ,a0) ios))ate 3 ios))binar0

Dealing ,ith 7inary files

%unctions for binar0 file handlin4 4et()) read a b0te and 1oint to the ne(t b0te to read 1ut()) ,rite a b0te and 1oint to the ne(t location for ,rite read()) bloc5 readin4 ,rite()) bloc5 ,ritin4

Dealing ,ith 7inary files

6ome useful functions see54())Go to a specific position when reading see51())Go to a specific position when writing tell4()) Retunrs an int type, that shows the current position of
the inside-pointer. This one works only when you read a file.

tell1()) The same as tellg() but used when we write in a file. flush())Sa e data from the buffer to the output file.

Cinary File I/O 89amples


//!"ample #$ %sing get() and put() #include <iostream> #include <fstream> void main() { fstream %ile(&test7file&,ios))out 3 ios))in 3 ios))binar0)! char ch! ch8+o+! %ile.1ut(ch)! //1ut the content of ch to the file %ile.see54(ios))be4)! //4o to the be4innin4 of the file %ile.4et(ch)! //read one character cout << ch << endl! //dis1la0 it %ile.close()! $

Cinary File I/O 89amples


&&!"ample '$ %sing read() and write() #include <fstream.h> #include <strin4.h> void main() { fstream %ile(&test7file.t(t&,ios))out 3 ios))in 3 ios))binar0)! char arr9':;! strc10(arr,&<ello =orld-&)! //1ut <ello =orld- into the arra0 %ile.,rite(arr,>)! //1ut the first > s0mbols into the file- &<ello& %ile.see54(ios))be4)! //4o to the be4innin4 of the file static char read7arra09'#;! // ,ill 1ut the read data, here %ile.read(read7arra0,:)! //read the first : s0mbols- &<el& cout << read7arra0 << endl! //dis1la0 them %ile.close()! $

7ore Cinary File I/O 89amples


#include <fstream> void main() {
//if ,e have &<ello& in test7file.t(t

ifstream %ile(&test7file.t(t&)! char arr9'#;! %ile.read(arr,'#)!


//this should return >, as <ello is > characters lon4

cout << %ile.tell4() << endl! %ile.close()!

(ummary of Input File+Belated Functions


#include <fstream> ifstream fsIn; fsIn.open(const char[] fname) ? connects stream fsIn to the e(ternal file fname. fsIn.get(char& c) ? e(tracts ne(t character from the in1ut stream fsIn and 1laces it in the character variable c. fsIn.eof() ? tests for the end-of-file condition. fsIn.close() ? disconnects the stream and associated file. fsIn >> c; //@ehaves 2ust li5e cin

(ummary of Output File+Belated Functions


#include <fstream> ofstream fsOut; fsOut.open(const char[] fname) ? connects stream fsOut to the e(ternal file fname. fsOut.put(char c) ? inserts character c to the out1ut stream fsOut. fsOut.eof() ? tests for the end-of-file condition. fsOut.close() ? disconnects the stream and associated file. fsOut << c; //@ehaves 2ust li5e cout

You might also like