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.

Slides:



Advertisements
Similar presentations
File I/O in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.
Advertisements

Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
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.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
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.
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.
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
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.
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.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
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
1 Streams 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.
1 CS161 Introduction to Computer Science Topic #13.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
Programming Principles II Lecture Notes 7 Files Andreas Savva.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
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.
C++ FILE I/O.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
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.
COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring Fall 2016 Set 10: Input/Output Streams 1 Based on slides created.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
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.
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS Computer Science IA: Procedural Programming
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
Data Streams.
File I/O.
File I/O.
File I/O with Records Lesson xx
Basic File I/O and Stream Objects
Programming with Data Files
when need to keep permanently
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.
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O.
CS150 Introduction to Computer Science 1
File I/O in C++ I.
CS150 Introduction to Computer Science 1
Programming File I/O.
Reading from and Writing to Files
Programming with Data Files
ifstreams and ofstreams
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

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 Learning, File I/O (Ch. 7)

Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk) is permanent until overwritten 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 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)

Declaring ifstream and ofstream Input File: (ifstream) ifstream fin; // declare input stream fin.open("data.txt");// open the file fin >> varName;// reads a data from file // into variable “varName” // variable matters! fin.close(); // closes the file Output File: (ofstream) ofstream fout; // declare output stream fout.open("data.txt");// open the file fout << varName;// writes the data in variable // “varName” to file. // variable matters! fout.close(); // closes the file

Example 1 – input from file You can read and write integers, doubles, chars, etc. from files just like cin >> and cout << : #include void main(void) { int data; ifstream fin; fin.open("data.txt"); fin >> data; while (fin)// fin==false means no more data { cout << data << endl; fin >> data; } fin.close(); } Open the file before you use it Close the file after using it

File I/O: Example 1 If you want to use a DOS path name, you must put TWO backslashes fin.open(“d:\\MyData\\data.txt"); [EOF] data.txt  [EOF] is a special character: End Of File  When the file opens, the reading position is at the beginning  Every time you read a number, the reading position advances  When you read [EOF], fin becomes false

Example 2: output to file In the same way you can write data to a file #include void main(void) { int i; ofstream fout; fout.open(“data2.txt"); fout << “Compute x^2” << endl; for (i=1; i<=3; i++) fout << i << “ “ << i*i << endl; fout.close(); } Compute x^2 [CR] 1 1 [CR] 2 4 [CR] 3 9 [CR] [EOF] data2.txt Open with NotePad Compute x^ Inserted by the OS

File I/O: Example 3 - input #include void main(void) { int i, size; int array[100] = {0}; ifstream fin; fin.open("data.txt"); fin >> size; // read in number of data pts for (i=0; i<size; i++) fin >> array[i]; // read in elements fin.close(); // use the array // calculate average, etc... } Input an 1-D array of int The first element of the file is the size of the array, and then, the actual data. Example: data.txt

Checking for errors.fail() After opening a file It is a good idea to make sure it was opened properly. We can do this using the “.fail()” method Example: #include #include // input from file Void main() { ifstream fin; // input file name is fin fin.open(“test.dat”); if (fin.fail()) // check if the open failed! { cout << “Error opening file \n”; }

Common Mistakes to Avoid Remeber to open the file! Common mistake ifstream fin; fin >> size; // error!!!! didn’t open the file Correct ifstream fin; fin.open(“name-of-file”); fin >> size; // error!!!! didn’t open the file Close file only after done read/writing out data (not before) ofstream fout; fout.open(“name-of-file”); fout << size; // OK fout.close(); // Closed file... fout << more_data;// error!!!! file has been closed!