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.

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.
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.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
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.
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.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
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.
ITEC 320 C++ Examples.
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.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
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.
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.
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.
© 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.
Chapter 8 (Part 3) Library Classes for Strings (P.405)
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
Copyright © 2003 Pearson Education, Inc.
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.
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.
CHAPTER 4 File Processing.
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:

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 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 File I/O / 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 File I/O / Slide 4 File-Related Functions #include * 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

COMP104 File I/O / Slide 5 >: Example 1  You can read and write integers, doubles, chars, etc. from files just like cin >> and cout << : #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 File I/O / Slide 6 File I/O: Example 1 file1.dat: (eof) file2.dat: (eof)

COMP104 File I/O / Slide 7 File I/O: Example 2 // Copies indata.dat to outdata.dat // and counts the number of lines. // Prints file to screen too. #include using namespace std; void main(){ ifstream ins; ofstream outs; int count=0; char next; ins.open("indata.dat");// open the input file outs.open("outdata.dat");// open the output file

COMP104 File I/O / Slide 8 File I/O: Example 2 while(true){ // loop for each line while(true){ // loop to read each char on line ins.get(next); if(ins.eof() || next== '\n') break; cout << next; outs << next; } count++; cout << endl; if(ins.eof()) break; outs << endl; } ins.close(); outs.close(); cout << "Number of lines copied: " << count << endl; }

COMP104 File I/O / Slide 9 File I/O: 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

COMP104 File I/O / Slide 10 File I/O: Example 3 // Counts the number of blanks on each line of indata.dat. // Outputs each line, and number of blanks on each line. #include using namespace std; void main(){ ifstream ins; int count; char next; ins.open("indata.dat");// open the file

COMP104 File I/O / Slide 11 File I/O: Example 3 while(true){ // loop to read each line count = 0; while(true){ // loop to count spaces on line ins.get(next); // read next character if(ins.eof() || next== '\n') // line done? break; // go to next line if line done cout << next; // otherwise output character if(next==' ') // increment if blank count++; } cout << endl; cout << "Blanks: " << count << endl; if( ins.eof()) // done? break; } ins.close(); }

COMP104 File I/O / Slide 12 File I/O: 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