{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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
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...
Image of page 3

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

View Full Document Right 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
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...
Image of page 5

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

View Full Document Right 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; ...
Image of page 6
CS161 Topic #14 7 A Reminder about Reading...
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern