Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.

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.
Chapter 14: Overloading and Templates
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
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.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
11 Introduction to Object Oriented Programming (Continued) Cats.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Being Agile! –Part of the RAD – Use Cases. –(If we have time) Review what we did last time and.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Project Wikis will be made public tonight. Presentations week 6 – next week. Slack Chat invites sent.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
11 Introduction to Object Oriented Programming (Continued) Cats.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
1 Introduction to Object Oriented Programming Chapter 10.
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.
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 © 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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (this Friday). Today: –Templates. –STL –Smart Pointers.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
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.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Chapter 4 Abstract Data Types, Classes and Objects.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Chapter 14: Sequential Access Files
ifstreams and ofstreams
C++ Templates.
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
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
CISC/CMPE320 - Prof. McLeod
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Today’s Lecture I/O Streams Tools for File I/O
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC101 Reminders All assignments are now posted.
CMPE212 – Reminders Assignment 3 due next Friday.
Reading Data From and Writing Data To Text Files
CISC101 Reminders All assignments are now posted.
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
ifstreams and ofstreams
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
Lecture 3 More on Flow Control, More on Functions,
File I/O in C++ I.
Presentation transcript:

Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated. Before this week’s tutorial – will talk more about “being agile” and what you need to do next. Today: What else do you need to know for the assignment?

Today & Next Time… From last week: Demo of vector Class. Summary of other STL container classes. –Text File I/O. –Separating declaration from implementation. –As applied to building classes. –Exceptions. Fall 2015CISC/CMPE320 - Prof. McLeod2

vector Class Demo of the vector (a template container class) class from the STL: VectorDemo.cpp Much better than arrays! See CPlusPlus.com for more info and member functions. Fall 2015CISC/CMPE320 - Prof. McLeod3

Other Container Classes The STL contains many other built-in container templates: –See: –array, vector, deque, forward_list, list, stack, queue, priority_queue, set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map, unordered_multimap, valarray, bitset. Fall 2015CISC/CMPE320 - Prof. McLeod4

The STL in Visual Studio Very similar set of template classes. See: Fall 2015CISC/CMPE320 - Prof. McLeod5

Fall 2015CISC/CMPE320 - Prof. McLeod6 ISO C++ Stream Hierarchy istringstream ifstream iostream ofstream ostringstream istreamostream fstream Note that iostream extends both istream and ostream.

Fall 2015CISC/CMPE320 - Prof. McLeod7 File I/O We’ve been including the iostream library for console I/O. For file I/O: #include So, fstream inherits all the functionality that we are used to from iostream and adds additional functions like open() and close().

Fall 2015CISC/CMPE320 - Prof. McLeod8 Text File I/O – Reading Two ways to open a file for reading. Assume filename is a string : ifstream fileIn; fileIn.open(filename.c_str()); Or: ifstream fileInAgain(filename.c_str()); (Or you can supply a char* literal).

Fall 2015CISC/CMPE320 - Prof. McLeod9 Text File I/O – Reading, Cont. The filename can contain a drive letter and/or folder names – a path. If it is a Unix path you are OK, but for a Windows path, remember to use \\ as a folder delimiter. If no path is supplied, the open() function looks in the same folder as your source code (not the *.exe folder…).

Fall 2015CISC/CMPE320 - Prof. McLeod10 Text File I/O – Reading, Cont. Use the fail() function to check to make sure the file has opened OK: if (fileIn.fail()) { cerr << "Unable to open file: " << filename << endl; return 1; }

Fall 2015CISC/CMPE320 - Prof. McLeod11 Text File I/O – Reading, Cont. To read a char use the get() function: fileIn.get(aChar); To read a string, use getline() : string line; getline(fileIn, line); getline() returns false (or 0) when there is nothing to read in.

Fall 2015CISC/CMPE320 - Prof. McLeod12 Text File I/O – Reading, Cont Other ways to check for the end of the file, if you don’t know how much to read in advance: while(!fileIn.eof()) Note that eof() returns true after you have tried to read past the end of the file. Turns out that >> also returns a boolean false if there is nothing to be read: while(fileIn >> aWord)

Fall 2015CISC/CMPE320 - Prof. McLeod13 Text File I/O – Reading, Cont. You can also use the re-direction operators, just as you would with screen input: int aVal; fileIn >> aVal; Reads an int value into aVal. And when you are finished: fileIn.close();

Fall 2015CISC/CMPE320 - Prof. McLeod14 Text File I/O – Reading, Cont. Note that the >> operator reads what is expected by examining the type of the variable supplied. It ignores whitespace (tabs, spaces and linefeeds). So, if you wish to read whitespace, you will need to use a “get…” method instead.

Fall 2015CISC/CMPE320 - Prof. McLeod15 Text File I/O – Writing To create the stream: ofstream outFile("TestOut.txt"); Use the << operator to write to outFile and then close() the file, when done. To append to a file: ofstream outFile("TestOut.txt", ios::app); ios::app is a constant defined in the iostream library, so no new includes are required.

Fall 2015CISC/CMPE320 - Prof. McLeod16 Text File I/O – Writing, Cont. You can also use the fail() function to see if you have had problems opening the file (no write privilige, bad folder, etc.). And, remember the stuff you did in part 3 of exercise 1 (stream manipulators)? This is how you can format things for output.

Fall 2015CISC/CMPE320 - Prof. McLeod17 Text File I/O – Example See TextIODemo.cpp Also shows function prototypes, and Passing by constant reference. Next, we will look at an OOP version of this program.

Function Prototypes Allow you to implement functions in any order, since the function names have already been declared. Easier to see the structure of what you are coding. Parameters must be typed – optionally supply the parameter name for style. Eventually the declarations will be in a separate file from the implementation. Fall 2015CISC/CMPE320 - Prof. McLeod18

File I/O Demo as a Class Restrucure TextIODemo.cpp by separating out a header file and an implementation file. Might as well make a simple class out of the result. See: –textfileio.h –textfileio.cpp –TestFileIOClass.cpp Fall 2015CISC/CMPE320 - Prof. McLeod19

Header File - textfileio.h This header file only contains the declaration of the TextFileIO class, but it could also have contained: –enums –structs –non-member function prototypes –other class declarations –constants –documentation Putting any implementation in the header file is poor structure (and poor style). Fall 2015CISC/CMPE320 - Prof. McLeod20

Declaration in Separate File Now, the implementation can be completely hidden from anyone using your class. You should be able to use the class without knowing anything about the implementation! Fall 2015CISC/CMPE320 - Prof. McLeod21

textfileio.h, Cont. Note the use of const in the member function declaration: vector readFile() const; This contract promises that the member function will not change member variables (attributes). Optional, but good programming practice, particularly for accessors. Fall 2015CISC/CMPE320 - Prof. McLeod22

textfileio.h, Cont. #pragma once ensures that the declarations in this file will only be made once. The *.h file will be included in any file that is going to use this class using: #include "textfileio.h" You can have as many of these #includes as you want in a project without worry! Fall 2015CISC/CMPE320 - Prof. McLeod23

Class Declaration The public: and private: sections control access to class members from instantiations. As you would expect, encapsulation dictates that your attributes are declared in the private: section! Fall 2015CISC/CMPE320 - Prof. McLeod24

Implementation File – textfileio.cpp Has the same name (different extension) as the header file, by convention. Implements member and non – member functions. Few comments, or just comments for the developer. Users are not going to see this file. Fall 2015CISC/CMPE320 - Prof. McLeod25

textfileio.cpp, Cont. The constructor: TextFileIO::TextFileIO(const string& fname) : filename(fname) {} Note the “shortcut” notation in the initialization section. You can still do things the old-fashioned way, especially if you are going to check the arguments for legality. Fall 2015CISC/CMPE320 - Prof. McLeod26

textfileio.cpp, Cont. Also note the membership operator :: It allows you to associate the member with the class. You can implement members or non-member functions for any header file that you have included. Fall 2015CISC/CMPE320 - Prof. McLeod27

Fall 2015CISC/CMPE320 - Prof. McLeod28 Aside - private Members Member function definitions and their implementations can access private members – even if this is in a different file. Non-member functions cannot access private members, only public ones.

TestFileIOClass.cpp Some class in your project must have a main function, or your application will not run. (But only one main function per project!) TextFileIO is instantiated on the run-time stack (more about this later) in the usual way. You can only access public: members from here. Fall 2015CISC/CMPE320 - Prof. McLeod29

Default Constructors Invoked without parameters. If we had one for TextFileIO it would be invoked as: TextFileIO test; No round brackets! What does: TextFileIO test(); look like to you? Fall 2015CISC/CMPE320 - Prof. McLeod30