CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
Advertisements

CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
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.
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.
File streams Chapter , ,
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
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.
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.
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.
Program Input and the Software Design Process ROBERT REAVES.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
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.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output.
File I/O ifstreams and ofstreams Sections 11.1 &
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
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.
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.
Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
1 I/O  C++ has no built-in support for input/output input/output is a library (iostream)  C++ program views input and output as a stream of bytes  Input:
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Data Types Storage Size Domain of all possible values Operations 1.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
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.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
1 Chapter 4 Program Input and the Software Design Process.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Lecture 14 Arguments, Classes and Files. Arguments.
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.
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.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
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.
From lab 1 Using cs lab machines (including remote)
CS 1430: Programming in C++ No time to cover HiC.
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.
Chapter 3: Input/Output
Standard Input/Output Stream
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
File I/O in C++ I.
CS150 Introduction to Computer Science 1
File I/O.
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
File I/O in C++ I.
Presentation transcript:

CS 1430: Programming in C++ 1

File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right? 2

Review: Standard Input/Output Stream // Header file #include int size; float avg; cin >> size; // cin: Standard input stream // >>: input operator cout << "Average is " << avg; // cout: standard output stream // <<: output operator 3

Header File // Input stream class istream { private: // Members: public: bool eof(); void get(char& x); void getline(char s[], int size, char endChar); … }; istream cin; // Variable of istream // connecting cin to keyboard 4

Header File // Output stream class ostream { private: // Members public: bool good(); bool fail(); … }; ostream cout; // Variable of ostream // connecting cout to monitor 5

Standard Error Stream cerr << “Error message.”; // cerr: Standard error stream // always go to monitor // Not used yet 6

Header File for File I/O // File Stream #include // Input File Stream class ifstream { … }; // Output File Stream class ofstream { … }; 7

Class ifstream // File Stream #include class ifstream { private:... public: void open(string fileName); bool good(); bool fail(); void close(); bool eof(); void get(char& x); … }; 8

Class ofstream // File Stream #include class ofstream { private:... public: void open(string fileName); void close(); bool good(); bool fail(); … }; 9

#include // Defines class ifstream and ofstream int main() { // use any identifiers you want ifstream MyInput; ofstream yourOutFile; ifstream p7_File, cin_redirect; ofstream P6Out, cout_redirect;... return 0; } File Input/Output 10

#include int main() { ifstream MyInput; // Open file // void open(string fileName); MyInput.open(“P6.IN"); // P6.IN is in the same folder as the source file MyInput.open(“J:\\P6.IN"); // P6.IN could be any where // Escape char ‘\’ // Do work MyInput.close(); // void close(); // No parameter! return 0; } File Input/Output 11

#include int main() { ifstream MyInput; MyInput.open(“P6.IN"); // Check open operation if (!MyInput.good()) cout << "Error: Cannot open input file"; return 0; } Checking Open Operation 12

#include int main() { ifstream MyInput; MyInput.open(“P6.IN"); // Check open operation if (MyInput.fail()) { cout << "Error: Cannot open input file"; return 1; } // Do work return 0; } Checking Open Operation 13

#include int main() { ifstream MyInput; ofstream yourOutFile; MyInput.open(“P6.IN"); if (MyInput.fail()) { cout << "Error: Cannot open input file!"; return 1; } yourOutFile.open(“p6.out”); if (!yourOutFile.good()) { cout << "Error: Cannot open output file!"; return 1; } … return 0; } File Input/Output 14

#include int main() { ifstream MyInput; ofstream yourOutFile; MyInput.open(“P6.IN"); yourOutFile.open(“P6.out”); // Check open file int x; MyInput >> x; // cin >> x; while (!MyInput.eof()) // cin.eof() { // process data x ++; // output to file yourOutFile << x; // cout << x; // read the next value MyInput >> x; } return 0; } File Input/Output 15

#include int main() { ifstream MyInput; ofstream yourOutFile; int x; MyInput.open(“P6.IN"); yourOutFile.open(“p6.out”); if (MyInput.fail() || !yourOutFile.good()) { cout << "Error: Cannot open files!"; return 1; } MyInput >> x; while (!MyInput.eof()) { x ++; yourOutFile << x; MyInput >> x; } MyInput.close(); yourOutFile.close(); return 0; } File Input/Output 16

// DO_01: Include header file for file I/O int main() { // DO_02: declare variables for file I/O // DO_03: open file // DO_04: Check open operation // DO_05: Read value from a file // DO_06: Fill condition: read until the end of file while ( ) { // DO_07: Output value to a file } // DO_08: Close files } File Input/Output 17

int main() { char x; cin.get(x); while(!cin.eof()) { if (x == ' ') // process a space else if (x == '\n') // process a new line char else // process a regular char cin.get(x); } return 0; } // cin >> x will skip all white spaces. // cin.get() will read in white spaces. Function get() 18

int main() { char x; ifstream MyInput(“P6.in"); MyInput.get(x); while(!MyInput.eof()) { if (x == ' ') // process a space else if (x == '\n') // process a new line char else // process a regular char MyInput.get(x); } return 0; } // MyInput >> x will skip all white spaces. // MyInput.get() will read in white spaces. Function get() 19

int main() { ifstream MyInput; if (!OpenFile(MyInput)) return 1; // Do work return 0; } bool OpenFile(ifstream& inFile) { char file_name[21];// could use C string cin >> file_name; inFile.open(file_name); if (!inFile.good()) { cout << "Error: Cannot open input file"; return false; } else return true; } File Stream as Function Parameter 20

Schedule Quiz9–3 10 PM, Wednesday Lab 11 Available on Tuesday Quiz 3 Wednesday, Dec 2 (week 14) 21

Schedule Test 3 Wednesday (week 15) Final Exam 7:00-8:52 pm, Tuesday, Dec 15, 2015 Velzy Commons 22

Test 3 Classes data members (fields) constructors methods Selection Sorting File I/O Enumeration data type C String (Array of char ) 23