Using Input/Output FilesAcomputerfileisstored onasecondarystoragedevice (e.g., disk)is permanentcanbe used to provide input dataor receive output data,or bothmust reside in Project directory (not necessarily thesamedirectory asthe.cppfiles)mustbe opened beforereading it
Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk) is permanent can be used to provide input data or receive output data, or both must reside in Project directory (not necessarily the same directory as the .cpp files) must be opened before reading it
Using Input/Output Filesstream - a sequence of charactersinteractive(iostream).cin-input streamassociated withkeyboard.cout -output stream associated with displayfile (fstream).ifstream-defines new input stream (normallyassociatedwithafile).ofstream-defines new output stream (normallyassociated withafile)
Using Input/Output Files stream - a sequence of characters interactive (iostream) • cin - input stream associated with keyboard • cout - output stream associated with display file (fstream) • ifstream - defines new input stream (normally associated with a file) • ofstream - defines new output stream (normally associated with a file)
Two namesTheexternalfilename:keyboard,screen,something.datopenTheinternalstream (orlogical)name: cin,cout,stream-name
Two names The external file name: keyboard, screen, something.dat The internal stream (or logical) name: cin, cout, stream-name open
Basic operations#include <fstream>IGHifstream xxx;ofstream YYYixxx.open(filenamel);YYY.open(filename2);>>XxxYYY<<xxx.close();YYY.close();
Basic operations #include <fstream> ifstream xxx; ofstream yyy; xxx.open(filename1); yyy.open(filename2); xxx >> . >> . yyy << . << . xxx.close(); yyy.close();
and >>inOverloaded<file I/oYou can read and write integers,doubleschars,etc.from files just like cin >> andcout << !
Overloaded << and >> in file I/O You can read and write integers, doubles, chars, etc. from files just like cin >> and cout << !