CISC/CMPE320 - Prof. McLeod

Slides:



Advertisements
Similar presentations
File I/O Finished off Colin Cherry standing in for Dr. Lin.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
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.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
File I/O ifstreams and ofstreams Sections 11.1 &
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
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 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
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.
Learners Support Publications Working with Files.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
Lecture 14 Arguments, Classes and Files. Arguments.
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.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Programming with ANSI C ++
C++ Templates.
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
C ++ MULTIPLE CHOICE QUESTION
COMP 2710 Software Construction File I/O
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
FILE HANDLING IN C++.
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
files Dr. Bhargavi Goswami Department of Computer Science
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 Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Today’s Lecture I/O Streams Tools for File I/O
Topics Input and Output Streams More Detailed Error Testing
CISC/CMPE320 - Prof. McLeod
Chapter 12: Advanced File Operations.
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
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.
CPS120: Introduction to Computer Science
CMPE212 – Reminders Assignment 3 due next Friday.
C++ Programming: chapter 6 – iostream
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
File I/O in C++ II.
CMPE212 – Reminders Assignment 2 due next Friday.
File I/O in C++ I.
Presentation transcript:

CISC/CMPE320 - Prof. McLeod Winter 2013 CISC/CMPE320 2/16/2019 CISC/CMPE320 All Jira accounts have been created – if you did not get an email from Jira – let me know. Project accounts have been created and students have been assigned to the projects. Jira should be ready to go. Confluence “spaces” will also be created – one per team. All pages for a team should be placed (and organized!) within this space. Pages could include the RAD, the SDD, diaries and meeting notes. I still need to setup Slack and Bitbucket. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

CISC/CMPE320 - Prof. McLeod Today vector class from the STL: demo File I/O – Text, Random and Binary. A Text File I/O Demo. Re-Structure the Demo: Separate Declaration and Implementation. Create a Simple Class. (Assn 1 requires the creation of a class and file I/O.) Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod As Promised, Cont. VectorDemo.cpp Much better than arrays! Fall 2018 CISC/CMPE320 - Prof. McLeod

Other Container Classes The STL contains many other built-in container templates: See: http://www.cplusplus.com/reference/stl/ 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 2018 CISC/CMPE320 - Prof. McLeod

ISO C++ Stream Hierarchy From http://www.cplusplus.com/reference/iolibrary/ Not a class diagram: Note that iostream extends both istream and ostream. Fall 2018 CISC/CMPE320 - Prof. McLeod

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

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Random File Access Text I/O is all sequential access – unidirectional. To open a file for input and output at the same time and to read any position in any order, you need to use random I/O. Same library: fstream. Fall 2018 CISC/CMPE320 - Prof. McLeod

Random File Access, Cont. To open for input and output, for example: fstream rwStream; rwStream.open(“stuff.txt”, ios::in | ios::out); Last time we used ifstream for input and ofstream for output. fstream extends both of these classes, allowing you to do input and output at the same time. Fall 2018 CISC/CMPE320 - Prof. McLeod

Random File Access, Cont. File open modes: Combine modes using the binary “or” |. Mode Meaning ios::app Append to end of file and you cannot move anywhere else. ios::ate Starts like append, but then you can move anywhere in the file. ios::in Open for input. ios::out Open for output. ios::trunc Discard contents (default behavior for ios::out). ios::binary Open for binary I/O. Fall 2018 CISC/CMPE320 - Prof. McLeod

Binary Random File Access Imagine two pointers in the file - a read pointer (“get”) and a write pointer (“put”). To read or write you first have to position the pointer at a certain byte position. For example: rwStream.seekp(1000, ios::beg); Positions the write pointer 1000 bytes from the beginning of the file. Fall 2018 CISC/CMPE320 - Prof. McLeod

Binary Random File Access, Cont. rwStream.seekg(1000, ios::end); Positions the read pointer 1000 bytes from the end of the file. You can also use ios::cur to position the pointer in relative terms. After the seek, you read and write the normal way. But how to know where to position the “pointers”? Fall 2018 CISC/CMPE320 - Prof. McLeod

Binary Random File Access, Cont. This only works when you have a structured file. You need to know what is stored, in which order it is stored and how big each item is. Remember that you can use the sizeof() operator to get the size of anything – primitive types or objects. This way you could write an entire object to the file and read it back the same way. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Text File I/O – Example See TextIODemo.cpp Stuff that also snuck into this demo: Use of vector and string classes. Function prototypes. Passing by constant reference. We will also look at an OOP version of this program. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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. Next, the declarations will be placed in a separate file from the implementation. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod

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 considered poor structure (and poor style). Fall 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

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

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod

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 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod

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. Fall 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod 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 2018 CISC/CMPE320 - Prof. McLeod