1 CS161 Introduction to Computer Science Topic #13.

Slides:



Advertisements
Similar presentations
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Advertisements

CPS120: Introduction to Computer Science Lecture 15 B Data Files.
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
Chapter 5: Loops and Files.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Chapter 3: Input/Output
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Program Input and the Software Design Process ROBERT REAVES.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Chapter 8 Data File Basics.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
FILE HANDLING IN C++.
1 firstlastnextprevioushome richard jones COMP103A Introduction to Computer Science 1 Richard Jones G2.04
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Chapter 3: Input/Output
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Loops and Files. 5.1 The Increment and Decrement Operators.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 14: Sequential Access Files
ifstreams and ofstreams
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Copyright © 2003 Pearson Education, Inc.
File I/O.
Interactive I/O Input from keyboard Must prompt user User friendly
Basic File I/O and Stream Objects
Today’s Lecture I/O Streams Tools for File I/O
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Chapter 3: Input/Output
CS150 Introduction to Computer Science 1
File I/O in C++ I.
CS150 Introduction to Computer Science 1
CHAPTER 4 File Processing.
Reading from and Writing to Files
Reading Data From and Writing Data To Text Files
ifstreams and ofstreams
Chapter 1 c++ structure C++ Input / Output
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

1 CS161 Introduction to Computer Science Topic #13

CS161 Topic #132 Today in CS161 External Files –What is an external file? –How do we save data in a file? Discuss Program #5 –Questions? Next time: –How do we read data from a file?

CS161 Topic #133 External Files So far, all of our programs have used main memory to temporarily store information. In addition, all input and output has been done with standard-in and standard-out devices –this includes input from the keyboard and output to our terminal's screen for prompts, echoing data, and displaying results Now it is time to work with secondary storage!

CS161 Topic #134 External Files Typically, secondary storage consists of a hard disk....however, –some of you may end up using a floppy as your secondary storage. –With ODIN, we will be using a hard disk. –Those of you using PCs, will use a combination of hard disks and floppies.

CS161 Topic #135 External Files There is one big difference between main memory and secondary storage. Remember with main memory, each time we run our program the value of our variables is lost and we start from scratch (i.e., we don't remember what the values of the variables were when we last ran the program). Also, when we power down the computer all of the data stored in main memory goes away.

CS161 Topic #136 External Files: Secondary Storage With files, when you finish executing a program, the data no longer goes away! It stays around so you can access it the next time you run your program. Plus, when your computer is powered down, information stored in secondary storage is not lost (unless you have a disk crash or failure!). Therefore, secondary storage can be used to store data for as long as it is needed.

CS161 Topic #137 External Files: Secondary Storage We can write information to secondary storage by creating a file which consists of a collection of data. We then name this file, so that we can store different types of information all in one directory or on one disk. We will be using text files in this class (and in CS162)

CS161 Topic #138 External Files: Secondary Storage A text file contains the same kind of data that we are used to seeing on the screen of our terminals. What this means is that a text file is a stream of characters: line by line. Therefore, these are files of characters. Lines in our files can be separated by end-of-lines ('\n'). This defines how many characters there are on a line and how many lines there are in the text file.

CS161 Topic #139 External Files: Secondary Storage To use a text file to store information, –we first need to include a new library of I/O functions, –declare input/output stream variables to be used instead of cin and cout, and –attach a C++ input or output stream to that file.

CS161 Topic #1310 External Files: Secondary Storage Text files all have names, called filenames. If you do a directory (ls on ODIN), –you will the filenames for all of your programs. Text files are just one of these, commonly with the extension.txt. We must specify a filename when we want to attach our input or output stream so that we can access the correct file!

CS161 Topic #1311 The Steps to Using External Files In your program when using C++ I/O stream files, you need to include the header file for a new library: #include This library already includes the iostream library, so you don’t need to include both the fstream library and the iostream library.

CS161 Topic #1312 The Steps to Using External Files The next step is to define a set of variables that will be used to read or write to the stream for a particular file. Let's backup a moment. Remember we use cin to read from standard-in and cout to write to standard-out. Well, cin and cout are really just variables. –cin is a variable of type istream (input stream) and –cout is a variable of type ostream (output stream).

CS161 Topic #1313 The Steps to Using External Files Therefore, to work with files we need to create our own variables; –for input our variable will be of type ifstream (input file stream) and –for output our variable will be of type ofstream (output file stream). Once these variables are defined, we can attach these input or output streams to the corresponding file in our directory.

CS161 Topic #1314 The Steps to Using External Files For input (i.e., reading data from a file), we can define a variable of type ifstream: ifstream in; For output (i.e., writing data to a file), we can define a variable of type ofstream: ofstream out;

CS161 Topic #1315 The Steps to Using External Files The next step after this is to attach these streams to our files. We do this by opening files. To open a file to enable us to write into it, we must tie the out variable in our program/function with the filename in our directory. The filename is specified as either a constant, a literal string, or even as an array of characters read in from standard-input.

CS161 Topic #1316 Opening Files Let's look at all three approaches of opening files for writing (i.e., output): –Using a constant filename: #include const char outfile[] = "outdata.txt";...//later in a function ofstream out; out.open(outfile);

CS161 Topic #1317 Opening Files –Using a literal string as the filename: #include...//later in a function ofstream out; out.open("outdata.txt");

CS161 Topic #1318 Opening Files –Using an array of characters as the filename, from standard in: #include... //later in a function char filename[12]; ofstream out; cout << "Please enter filename: "; cin >> filename; out.open(filename);

CS161 Topic #1319 The Steps to Using External Files To make sure that the file was properly opened, –it is best to double check that the out variable is not zero. –If it is, the file was not appropriately opened. out.open("mydata.txt"); if (!out) cout << "Error " << endl; else //continue with the program...

CS161 Topic #1320 Writing to an Open File After this statement is executed, we are ready to write to the file. Writing to a file follows all the same rules we use for writing to the screen, but instead of using cout...we use our output file variable: out.open(filename); out << "hello world"; out.put('!'); out << endl;

CS161 Topic #1321 Writing to an Open File Using open (eg., out.open) always opens the file so that we begin writing at the beginning of the file, as if we had a blank file, like a clean screen or a blank piece of paper. If in another program we had previously written information to this file, it is lost as soon as we invoke the open function. If we have never written information to this file before, then the open function will create a new file for us.

CS161 Topic #1322 Appending to a File It is possible to append information to an existing file...by specifying ios::app when we open a file: ofstream out; out.open(filename, ios::app); if (out) { out << data << endl;...

CS161 Topic #1323 Closing a File when done.... Once we have named the file, and opened it for writing, we can then write information to it. Just think of using the new stream variable (eg., out) instead of cout as directing your output from getting displayed on the terminal to being saved in the corresponding file. Once done writing to the file, close the currently open file. We do that with another function included in our fstream library: out.close();//parens are necessary!

24 CS161 Introduction to Computer Science Questions?