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.

Slides:



Advertisements
Similar presentations
File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
Advertisements

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.
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.
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.
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.
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.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
 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.
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 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
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.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
File I/O ifstreams and ofstreams Sections 11.1 &
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.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Structured Programming Instructor: Prof. K. T. Tsang
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.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
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.
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?
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
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.
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.
 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.
Chapter 8 (Part 3) Library Classes for Strings (P.405)
CS-103 COMPUTER PROGRAMMING
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
COMP 2710 Software Construction File I/O
File I/O.
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O.
File I/O in C++ I.
CS150 Introduction to Computer Science 1
Programming File I/O.
CS150 Introduction to Computer Science 1
CHAPTER 4 File Processing.
Reading from and Writing to Files
Programming with Data Files
ifstreams and ofstreams
Reading from and Writing to Files Part 2
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

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 n can be used to provide input data or receive output data, or both n must reside in Project directory (not necessarily the same directory as the.cpp files) n must be opened before reading it

COMP104 Lecture 20 / Slide 3 Using Input/Output Files * stream - a sequence of characters n interactive (iostream)  cin - input stream associated with keyboard  cout - output stream associated with display n file (fstream)  ifstream - defines new input stream (normally associated with a file)  ofstream - defines new output stream (normally associated with a file)

COMP104 Lecture 20 / Slide 4 Two names The external file name: keyboard, screen, something.dat The internal stream (or logical) name: cin, cout, stream-name open

COMP104 Lecture 20 / Slide 5 Basic operations #include ifstream xxx; ofstream yyy; xxx.open(filename1); yyy.open(filename2); xxx >> … >> … yyy << … << … xxx.close(); yyy.close();

COMP104 Lecture 20 / Slide 6 > in file I/O You can read and write integers, doubles, chars, etc. from files just like cin >> and cout << !

COMP104 Lecture 20 / Slide 7 Example 1 file1.dat: (eof) file2.dat: (eof) Write a program which copies file1.dat to file2.dat.

COMP104 Lecture 20 / Slide 8 #include using namespace std; void main(){ ifstream fin; int A[4], r; fin.open("file1.dat");// read data file of four integers for(r=0; r<4; r++)// into array fin >> A[r]; fin.close(); ofstream fout; fout.open("file2.dat"); // write data file for(r=3; r>=0; r--)// with numbers reversed fout << A[r] << ' '; fout.close(); }

COMP104 Lecture 20 / Slide 9 Character I/O #include ifstream xxx; char ch; * xxx.open(fname) connects stream xxx to the external file fname * xxx.get(ch) Gets the next character from the input stream xxx and places it in the character variable ch * xxx.put(ch) Puts the character ch into the output stream xxx * xxx.eof() n tests for end-of-file condition * xxx.close() n disconnects the stream and closes the file Remark: two ‘invisible characters’, ‘\n’ (end of the line) and the end-of-file

COMP104 Lecture 20 / Slide 10 A typical usage: ifstream ins; char ch; ins.get(ch); while (!ins.eof()) { cout << ch; ins.get(ch); } while (ins.get(ch)) { cout << ch; }

COMP104 Lecture 20 / Slide 11 Example 2 indata.dat: a b c top10 methods to count spaces 1 3(eof) outdata.dat: a b c top10 methods to count spaces 1 3(eof) Output to screen: a b c top10 methods to count spaces 1 3 Number of lines copied: 4 Write a program which copies indata.dat to outdata.dat and counts the number of lines. Prints file to screen too.

COMP104 Lecture 20 / Slide 12 Solution to example 2 #include using namespace std; void main(){ ifstream ins; ofstream outs; int count=0; char ch; ins.open("indata.dat");// open the input file outs.open("outdata.dat");// open the output file

COMP104 Lecture 20 / Slide 13 count=0; ins.get(ch); while (!ins.eof()) { if (ch=='\n') { count++; cout << endl; outs << endl;} else { cout << ch; outs << ch;} ins.get(ch); } count++; ins.close(); outs.close(); cout << "Number of lines copied: " << count << endl; } The last line does not have a ‘\n’!

COMP104 Lecture 20 / Slide 14 More compact C++ style count=0; while (ins.get(ch)) { if (ch=='\n') { count++; cout << endl; outs << endl;} else { cout << ch; outs << ch;} } count++; cout << endl; outs << endl;

COMP104 Lecture 20 / Slide 15 Final refined solution! count=0; while (ins.get(next)) { if (next=='\n') { count++; cout << endl; outs << endl;} else { cout << next; outs << next;} } if (next != ‘\n’) { count++; cout << endl; outs << endl; } The previous solutions do not handle the case of empty file. File I/O is messy, use it moderately now!!!

COMP104 Lecture 20 / Slide 16 Example 3 indata.dat: a b c top10 methods to count spaces 1 3(eof) Output to screen: a b c Blanks: 2 top10 methods to count spaces Blanks: 4 Blanks: Blanks: 3 Write a program which counts the number of blanks on each line of indata.dat, outputs each line, and number of blanks on each line.