lect08 filddde
TRANSCRIPT
-
7/23/2019 Lect08 Filddde
1/24
File I/O
COMP1117 ComputerProgramming
-
7/23/2019 Lect08 Filddde
2/24
Content
File Output StreamsFile Input Streams
Character I/O
String Streams
-
7/23/2019 Lect08 Filddde
3/24
Streams
In C++, a streamis a sequence of data
input stream
sourcefrom hich dataare readone !" onecin# ta$e input from $e"!oard, e%g% cin >> x >> y
output stream
destinationto hich dataare rittenone !" one
cout# send output to the screen, e%g% cout
-
7/23/2019 Lect08 Filddde
4/24
File IO &ia streams
cin and coutOsmall/short interactions ith users
'large num!ers of inputs or outputs
'inputs and outputs that need permanent storage
e%g% "our homeor$
File streams can help ith these
(ead from or rite to )les
*eha&e li$e cin/cout
-
7/23/2019 Lect08 Filddde
5/24
File streams
)le stream is associated ith a )leinput, output
o create a file stream
-eclarea variableith the )le stream t"peConnect the variableto a )le
Input/output "our datafrom/to the stream
same as using cin/cout
-isconnect the variablefrom the)lein the end
filestream
user
-
7/23/2019 Lect08 Filddde
6/24
File output stream
o create a file output stream-eclarea variableith the t"peofstream
Connect the variableto a )le
Output "our datato the stream.same as using
cout)
-isconnect the variablefrom the)lein the end
ofstreamis de)ned in the li!rar"
file
user
ofstream
-
7/23/2019 Lect08 Filddde
7/24
#include #include using namespace std;int main(){ ofstream target; int n; target.open("myresult.txt"); cin >> n;
for(int i = 0; i < n; i){ for(int ! = 0; ! < n; !) target n; for(int i = 0; i < n; i){ for(int ! = 0; ! < n; !) target
-
7/23/2019 Lect08 Filddde
8/24
File output stream
If output )le does not e2ist in the currentdirector"
a ne )le ill !e created
3lse
the contents of the e2isting )le ill !e erased .!e
careful4
See output$file.ccas an e2ample
-
7/23/2019 Lect08 Filddde
9/24
File input stream
o create a file input stream-eclarea variableith the t"peifstream
Connect the variableto a )le
(ead datafrom the streamas using cin
-isconnect the variablefrom the)lein the end%
ifstreamis de)ned in the li!rar"
file user
ifstream
-
7/23/2019 Lect08 Filddde
10/24
File input stream
read a series of num!ers from a )le and calculate thesum
#include #include using namespace std;int main(){ ifstream myfile;
myfile.open("myfile.dat"); ...
#include #include using namespace std;int main(){
ifstream myfile;
myfile.open("myfile.dat"); ...
Include theli!rar"-eclare the )le
output stream&aria!le
Connect the&aria!le to a)le
-
7/23/2019 Lect08 Filddde
11/24
File input stream
errorill occur if open a)le thatdoes note2ist
myfile.fail()
return trueif the )le cannot !e opened, otherise
false
int main(){ ifstream my%ar; my%ar.open("myfile.dat");
if(my%ar.fail()){ cout
-
7/23/2019 Lect08 Filddde
12/24
File input stream
5o to readthe data from input )le6e can use a while loop, same as using cin
ifstream myinfile;int x; int total =0;myinfile.open("myfile.dat");*ile(+++){ myinfile >> x;
total = x; ...
ifstream myinfile;int x; int total =0;
myinfile.open("myfile.dat");*ile(+++){ myinfile >> x;
total = x; ...
hat is the condition6
5o to chec$ if it is the end of a
)le6
-
7/23/2019 Lect08 Filddde
13/24
myinfile >> x;myinfile >> x;
File input stream
implicit mar$er maintained !" the s"stemto indicate the starting positionof the ne2te2traction%
Once the )le is opened, the mar$erpoints to the
)rst !"te
8n1 9 : 1 ; ;< = 8n = 8t 3OF%%%
fter e2tracting the )rst integer, the mar$ermo&esto the character folloing the integer%
8n1 9 : 1 ; ;< = 8n = 8t 3OF%%%
-
7/23/2019 Lect08 Filddde
14/24
File input stream
fter e2tracting the second integer>
myinfile >> x;myinfile >> x;
8n1 9 : 1 ; ;< = 8t = 8n 3OF%%%
If e $eep e2tracting inputs from the )le, themar$ere&entuall" reaches the end of the )le%
8n1 9 : 1 ; ;< = 8t = 8n 3OF%%%
5o to chec$ the end of the )le6
-
7/23/2019 Lect08 Filddde
15/24
File input stream
he end of )lecan !e found out !" chec$ingthe &alue of the folloing e2pression
myinfile >> xmyinfile >> x
ction of the e2pression
reada &alue from the )le, and
assignthe &alue to variable x
?alue of the e2pression .upon completion4
reading is successful, its &alue is m"in)le,otherise @
m"in)le AA 2 AA "
-
7/23/2019 Lect08 Filddde
16/24
File input stream
ifstream myinput;int x; int total = 0;
myinput.open("myfile.dat");*ile(myinput >> x) total = x;cout
-
7/23/2019 Lect08 Filddde
17/24
File input stream
nother a" to chec$ the end of )le#mem!er functionmyinput.eof()
ifstream myinput;int x; int total = 0;
myinput.open("myfile.dat");*ile(true){ myinput >> x; if(myinput.eof())
rea; total = x;cout
-
7/23/2019 Lect08 Filddde
18/24
e npu s reaminitiali0ation
If e ant to read the )le namefrom user, eneed to ta$e the c-style string&ersion of the%ariale%
ifstream myfile;string name;cin >> name;
myfile.open(name.cstr());
ifstream myfile;string name;cin >> name;
myfile.open(name.cstr());
e can com!ine thedeclarationand the )leopening statementsinto one statement%
ifstream myfile;myfile.open("myfile.txt")
;
ifstream myfile;myfile.open("myfile.txt")
;
ifstreammyfile("myfile.txt");
ifstreammyfile("myfile.txt");
-
7/23/2019 Lect08 Filddde
19/24
t"pe%cpp
ifstream input.Bfoo%t2t4Dinput AA 2 AA "D
.input AA 24 AA "D
If there is more items left in input
input AA 2 ill ha&e &alue input
input AA "
3lse
input AA 2 ill ha&e &alue @
@ AA "
-
7/23/2019 Lect08 Filddde
20/24
t an" moment, a )le can onl" !e connectedto one file stream.either inputor output4
)le must !e )rst disconnected.closed4 to astream !efore it is reEconnected
hen "ou open a )le for riting, if the )lee2ists, the s"stem ill )rst erase its contents%
ou can open a )le for appending, i%e% add ne
data at the end of an e2isting )le, !" openingit as#
ofstream myfile;myfile.open("myfile.txt"1 ios--app);
ofstream myfile;myfile.open("myfile.txt"1 ios--app);
-
7/23/2019 Lect08 Filddde
21/24
String stream
reat strings as streams
similar to cin/cout and )les
Gseful to do man" con&ersion operations
!eteen string and other data t"pes, such asint, dou!le, etc%
o use stringstream, "ou ha&e to
Hinclude sstreamA
-
7/23/2019 Lect08 Filddde
22/24
String output stream
Gse ostringstreamD see outputEtoEstring%cc
he contents of a string stream can !e
e2tracted using the str.4 mem!er function%
ostringstream des; // create an output string streamdes
-
7/23/2019 Lect08 Filddde
23/24
32ample
Open a )le test222%t2t, here 222 isdetermined !" an integer &aria!le .see
more)le%cc4ofstream outfile; int i=2;
ostringstream fname;fname.str(34); //set stringstream to emptyfname
-
7/23/2019 Lect08 Filddde
24/24
Input string stream
istringstream
Mem!er function B%eof.4 indicates end of string
// an input string stream wit initial contentsstring data!src = tmcan 1" 4# $ % "& &0';istringstream src(data!src);
string login;int a1 a" a3 a4 a# a$;src login a1 a" a3 a4 a# a$;
// an input string stream wit initial contentsstring data!src = tmcan 1" 4# $ % "& &0';istringstreamsrc(data!src);string login;
int a1 a" a3 a4 a# a$;src login a1 a" a3 a4 a# a$;