File I/O.

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.
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.
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.
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?
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.
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.
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 Computer Science IA: Procedural Programming
CS-103 COMPUTER PROGRAMMING
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
CSCI 161: Introduction to Programming
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.
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 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
Chapter 8 (Part 3) Library Classes for Strings (P.405)
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

File I/O

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

Two names The external file name: keyboard, screen, something.dat open The internal stream (or logical) name: cin, cout, stream-name

Basic operations #include <fstream> ifstream xxx; ofstream yyy; xxx.open(filename1); yyy.open(filename2); xxx >> … >> … yyy << … << … xxx.close(); yyy.close();

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

Example 1 Write a program which copies file1.dat to file2.dat. 1 2 3 4(eof) file2.dat: 4 3 2 1 (eof)

#include <iostream> #include <fstream> 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(); }

Character I/O #include <fstream> 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() tests for end-of-file condition xxx.close() disconnects the stream and closes the file EOF: control-D in Unix and control-Z in DOS Remark: two ‘invisible characters’, ‘\n’ (end of the line) and the end-of-file

A typical usage: ifstream ins; char ch; ins.get(ch); while (!ins.eof()) { cout << ch; } while (ins.get(ch)) { cout << ch; }

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

Solution to example 2 void main(){ ifstream ins; ofstream outs; #include <iostream> #include <fstream> 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

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

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

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’) { outs << endl; The previous solutions do not handle the case of empty file. File I/O is messy, use it moderately now!!!

Example 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. indata.dat: a b c top10 methods to count spaces 1 3(eof) Output to screen: Blanks: 2 Blanks: 4 Blanks: 0 1 3 Blanks: 3