Programming 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

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.
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.
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.
Start. File Streams 03/04/11 Stream as Connection to I/O.
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 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.
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.
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.
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.
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
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
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
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.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
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.
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.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
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.
Basic Input and Output Operations
CSCI 161: Introduction to Programming
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
Quiz Next Monday.
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
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
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.
File I/O in C++ I.
CS150 Introduction to Computer Science 1
CHAPTER 4 File Processing.
Reading from and Writing to Files
Programming with Data Files
File Streams 12/09/13.
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:

Programming File I/O

Files in a personal computer environment Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc. Files in a personal computer environment

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 to a program or receive output data from a program, or both; must reside in Project directory (not necessarily the same directory as the .cpp files) must be opened before it is used.

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).

C++ streams Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc.

Input File-Related Functions #include <fstream> ifstream fsIn; fsIn.open("fname.txt") connects stream fsIn to the external file "fname.txt ". fsIn.close() disconnects the stream and associated file. fsIn >> c; //Behaves just like cin

Output File-Related Functions #include <fstream> ofstream fsOut; fsOut.open("fname.txt") connects stream fsOut to the external file "fname.txt". fsOut.close() disconnects the stream and associated file. fsOut << c; //Behaves just like cout

File I/O: Example 1 // Reads three numbers from the file numbers.dat, // sums the numbers, and writes the sum to the file // sum.dat. #include <iostream> // for cin, cout #include <fstream> // for ifstream, ostream using namespace std; int main(){ // declare the input and output streams ifstream input_stream; ofstream output_stream; // open the input file input_stream.open("numbers.dat"); // open the output file output_stream.open("sum.dat");

File I/O: Example 1 // declare variables and read in the data (3 numbers) int first, second, third; input_stream >> first >> second >> third; // write the data to the output file output_stream << "The sum of the first 3\n" << "numbers in input_file.dat\n" << "is " << (first + second + third) << endl; // close the input and output files input_stream.close(); output_stream.close(); return 0; }

File I/O: Example 1.2 // copy everything from input.txt to output.txt #include <iostream> // for cin, cout #include <fstream> // for ifstream, ostream using namespace std; int main(){ // declare the input and output streams ifstream input_stream; ofstream output_stream; // open the input file input_stream.open(“input.txt"); // open the output file output_stream.open(“output.txt");

File I/O: Example 1.2 input_stream.get(next); char next; input_stream.get(next); while (! input_stream.eof()) { cout << next; output_stream.put(next); } input_stream.close(); output_stream.close(); return 0;

File I/O: Example 1.3 // Reads all the numbers from the file numbers.dat, // sums the numbers, and writes the sum and average to // the file sum.dat. #include <iostream> // for cin, cout #include <fstream> // for ifstream, ostream using namespace std; int main(){ // declare the input and output streams ifstream input_stream; ofstream output_stream; // open the input file input_stream.open("numbers.txt"); // open the output file output_stream.open("sum.dat");

// declare variables and read in the data int first, sum =0, counter= 0; input_stream >> first; while (!input_stream.eof()){ //test if is endoffile sum +=first; counter++; } cout << "The sum of numbers in the file is "<< sum << endl; cout << "The average is " << double(sum)/counter << endl; // write the data to the output file output_stream << "The sum is " << sum << endl; output_stream << "The average is “ << double(sum)/counter << endl; // close the input and output files input_stream.close(); output_stream.close(); return 0;

Example 2 //copies indata.dat to outdata.dat and counts the number of lines. Prints //file to screen too. #include <iostream> #include <fstream> using namespace std; int 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

Example 2 while (ins.get(next)) { if (next=='\n') { count++; } cout << next; outs << next; if (next !='\n') {// when last line not end with ‘/n’ cout << endl; outs << endl; ins.close(); outs.close(); cout << "Number of lines copied: " << count << endl;

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

File I/O: Example 3 // Counts the number of blanks on each line of the file // indata1.txt #include <iostream> #include <fstream> using namespace std; int main(){ ifstream ins; int count; char next; ins.open("indata1.txt"); // open the file ins.get(next); // get the first char

File I/O: Example 3 while(!ins.eof()){ // loop to read each line count = 0; while(next!='\n' ){ cout << next; if(next==' ') count++; ins.get(next); } cout << endl << "Blanks: " << count << endl; ins.close(); return 0;

Summary of Input File-Related Functions #include <fstream> ifstream fsIn; fsIn.open(const char[] fname) connects stream fsIn to the external file fname. fsIn.get(char& c) extracts next character from the input stream fsIn and places it in the character variable c. fsIn.eof() tests for the end-of-file condition. fsIn.close() disconnects the stream and associated file. fsIn >> c; //Behaves just like cin

Summary of Output File-Related Functions #include <fstream> ofstream fsOut; fsOut.open(const char[] fname) connects stream fsOut to the external file fname. fsOut.put(char c) inserts character c to the output stream fsOut. fsOut.eof() tests for the end-of-file condition. fsOut.close() disconnects the stream and associated file. fsOut << c; //Behaves just like cout