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.

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

1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
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.
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.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Chapter 5: Loops and Files.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
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.
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.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
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
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Chapter 8 Data File Basics.
CS Class 13 Today  File I/O (reading from and writing to files)  Reading until EOF  Formatting output  Nested Loops Announcements  Programming.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 6: Sequential Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
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.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
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.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
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 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Loops and Files. 5.1 The Increment and Decrement Operators.
Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Starting Out with C++: From Control Structures through Objects
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
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.
Writing to Files and Reading From Files. Writing Data to a File Creating a new file, a.dat or.txt file #include #include // for file writing #include.
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 Processing.
Chapter 6: Sequential Data Files
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
Standard Input/Output Stream
Chapter 3 Input output.
CS150 Introduction to Computer Science 1
Formatted Input, Output & File Input, Output
CS150 Introduction to Computer Science 1
Programming File I/O.
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
Reading from and Writing to Files
Programming with Data Files
Reading from and Writing to Files
Presentation transcript:

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 at start of day Interactive –Real time systems –Ok for smaller programs –Programs that complete quickly

Files Naming –.cpp.dat.out.in.txt How to attach files to the stream –stream object –external file name –internal name –open –close Additional functions as part of class

Files Declare the stream to be processed need to #include ifstreamins;// ins is the internal file name for input mode ofstream outs;// outs is the internal file name for output mode Need to open and close the files ins.open (“input.txt”); //”input.txt” and “output.out” are eternal file names outs.open (“output.out”); ins >> number; outs << number; ins.close(); outs.close ();

Creating a Sequential File Open the file. Specify: –External name: the full name of the file on disk. –Internal name: the name by which the file will be known in the program. –File mode: the purpose of the file: input or output. Write data to the file, or read data from, the file. Close the file. –Saves the file and puts an end-of-file marker after the last record if the file was created in the program. –Closes the file if the file is an input file.

Example If we issue the command ‘Open ‘GRADES” for Output As NewFile’ –GRADES is the external name –NewFile is the internal name –The mode is output If we issue the command ‘Close NewFile’ –Puts the EOF marker. – Closes the file. –Saves the file with the external name ‘GRADES.’

Reading a File To open: ‘Open “GRADES “ for Input As GradeFile’ Read the internal filename and the fields/variables –Read GradeFile, Name, Score Read records within a loop. While Not EOF(GradeFile) Read GradeFile, Name,Score …. End While Close GradeFile

// fileapp.cpp #include #include // necessary for file I/O #include // necessary for string object using namespace std; int main() { string user_name; int age; ofstream outfile; // Declare file stream named outfile. cout << "Enter your name: "; // Get name from user. getline(cin, user_name); cout << "Enter your age: "; // Get age from user. cin >> age; outfile.open("NAME_AGE.TXT",ios::out); // Open file for output. if (outfile) // If no error occurs while opening file/ write the data to the file. {outfile << user_name << endl; // Write the name to the file. outfile << age << endl; // Write the age to the file. outfile.close(); // Close the output file. } else // If error occurred, display message. { cout << "An error occurred while opening the file.\n"; } return 0; }

// numread.cpp #include using namespace std; int main() {float x, sum; int count; ifstream infile; // Declare file stream named infile. infile.open("FLOATS.TXT",ios::in); // Open file for input. sum = 0.0; count = 0; if (infile) // If no error occurred while opening file input the data from the file { cout.setf(ios::fixed); cout << "The numbers in the data file are as follows:\n"<< setprecision(1); do // Read numbers until 0.0 is encountered. { infile >> x; // Get number from file. cout << x << endl; // Print number to screen. sum = sum + x; // Add number to sum. count++; // Increment count of how many numbers read. } while(x != 0.0); // Output sum and average. cout << "The sum of the numbers is " << sum << endl; cout << "The average of the numbers (excluding zero) is " << sum / (count - 1) << endl; } else // If error occurred, display message. { cout << "An error occurred while opening the file.\n"; } infile.close(); // Close the output file. return 0; }

// num_end.cpp #include using namespace std; int main() {double x, sum; int count; ifstream infile; // Declare file stream named infile. infile.open("NUMS.TXT",ios::in); // Open file for input. sum = 0.0; count = 0; if (infile) // If no error occurred while opening file input the data from the file {.cout.setf(ios::fixed); cout << "The numbers in the data file are as follows:\n"<< setprecision(1); do // Read numbers until the end of file is encountered. { infile >> x; // Get number from file. if(!infile.fail()) // If the end of file is not reached... { cout << x << endl; // Print number to screen. sum = sum + x; // Add number to sum. count++; // Increment count of how many numbers read. } } while(!infile.fail()); // Output sum and average. cout << "The sum of the numbers is " << sum << endl; cout << "The average of the numbers is " << sum / count << endl; } else // If error occurred, display message. { cout << "An error occurred while opening the file.\n"; } infile.close(); // Close the output file. return 0; }

// num_end.cpp #include using namespace std; int main() {double x, sum; int count; ifstream infile; // Declare file stream named infile. infile.open("NUMS.TXT",ios::in); // Open file for input. sum = 0.0; count = 0; if (infile) // If no error occurred while opening file input the data from the file {.cout.setf(ios::fixed); cout << "The numbers in the data file are as follows:\n"<< setprecision(1); infile >> x;// Get number from file while ( !infile.fail() ) { cout << x << endl; // Print number to screen. sum = sum + x; // Add number to sum. count++; // Increment count of how many numbers read. infile >> x; // Get number from file. } // Output sum and average. cout << "The sum of the numbers is " << sum << endl; cout << "The average of the numbers is " << sum / count << endl; } else // If error occurred, display message. { cout << "An error occurred while opening the file.\n"; } infile.close(); // Close the output file. return 0; }

Pseudocode Language (Ch 5) In this chapter we added specific data type declarations, some numeric functions and file I/O Declarations:Functions: Int(Variable) Declare Variable As Data TypeRandom(Variable) Sqrt(Variable) File I/O: Open “file” For Input/Output As Name Close Name Read Name, Variable Write Name, Variable EOF(Name)