CS161_Topic#14

CS161_Topic#14 - CS161 CS161 Introduction to Computer...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS161 CS161 Introduction to Introduction to Computer Science Computer Science Topic #14
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS161 Topic #14 2 Today in CS161 More on External Files How do we Read from a File? What is “end of file”? Examine a Program using Files Discuss Program #6 What you need to consider Next time: Arrays and Structures
Background image of page 2
CS161 Topic #14 3 The Steps to Reading From External Files To read from an external file, we go through the same steps that we used to write to a file First, make sure you have included the fstream.h file #include <fstream.h> Then, define a variable of type ifstream ifstream in; Now, we are ready to open the file to read. ..
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS161 Topic #14 4 The Steps to Reading From External Files To open a file to read from, we call the open function through our file variable: in.open(“text.dat”); or, char filename[20]; cin >>filename; in.open(filename); Now the file should be open, with the file pointer positioned at the beginning of the file
Background image of page 4
CS161 Topic #14 5 The Steps to Reading From External Files To make sure that the file was properly opened, it is best to double check that the in variable is not zero. If it is, the file was not appropriately opened. in.open(filename); if (!in) cout <<"Error in opening test.dat" <<endl; else //continue with the program. ..
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS161 Topic #14 6 Reading from this Opened File After this statement is executed, we are ready to read from the file. Reading from a file follows all the same rules we use for reading from the keyboard, but instead of using cin. ..we use our input file variable: in.open(filename); if (in) { in >> some_variable; ...
Background image of page 6
CS161 Topic #14 7 A Reminder about Reading. .. in >> some_variable;
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/17/2010 for the course CS 161 taught by Professor Fant during the Winter '08 term at Portland State.

Page1 / 21

CS161_Topic#14 - CS161 CS161 Introduction to Computer...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online