CSC 1060 Week 08 File Read PDF
Document Details
Uploaded by DivineZebra9695
Red Rocks Community College
Tags
Summary
These notes cover file input and output concepts in C++ programming. Topics include file streams, creating and using input and output file streams, and handling file operations. The content also involves reading from and writing to files using C++ programming language methods.
Full Transcript
CSC 1060 REPETITION AND FILE READ OBJECTIVES AGENDA: WEEK 08 To read data from a file using stream 1. Streams objects. 2. Disconnected Model: File Input Read all data from a file into a...
CSC 1060 REPETITION AND FILE READ OBJECTIVES AGENDA: WEEK 08 To read data from a file using stream 1. Streams objects. 2. Disconnected Model: File Input Read all data from a file into a 3. StringStream program. 4. Review Exercises 5. TODO 6. Resources for Help STREAMS: HOW DATA GETS MOVED A stream object represents data flowing either into your program or out from your program. Input (Extraction) >> Keyboard File input – reading to extract data from a file Moves data from source to program The object named std::cin is of class std::istream and represents the keyboard. A std::ifstream is also an input stream, but it represents input from a data file. COMMON INPUT MEMBER FUNCTIONS INPUT STREAMS: ISTREAM & IFSTREAM peek - Peek next character ignore - Extract and discard characters good - Check whether state of stream is good fail - Check whether either failbit or badbit is set operator! - Evaluate stream (not) eof - Check whether eofbit is set clear - Set error state flags – default is to set to goodbit operator>> - Extract formatted input STD::ISTREAM Reading data from the keyboard until whitespace into a variable of the datatypes: int, float, double, long, unsigned, std::string, … std::cin >> var; Reading data from the keyboard until enter key is pressed into a std::string object. std::string str; std::getline(std::cin, str); Keyboard data to Program Variable 5 STEP DISCONNECTED MODEL Reading from a file, is done only when connected to the file and then it should be disconnected (closed) These are the 5 steps that are required in our course: 1. Create the input file stream object of type: std::ifstream 2. Open the file for reading 3. Check the file opened ok 4. Read from the file 5. Close the file stream to disconnect from reading Only flat files, such as text files (.txt), data files (.dat), comma separated files (.csv) files will be read for the CSC1060 course 1.NO drives, directory, or folder paths should be listed! CREATING THE FILE OBJECT: STEP 1 An std::ifstream object must be created #include // library required for file IO int main() { std::ifstream readFile; // file read object OPENING THE FILE: STEP 2 Associating the file with the input file stream object The file MUST exist first, and file names with file extension are case sensitive in most environments. The file name with file extension can either be set as a string literal directly or a string object. readFile.open("fileName.dat"); // open file for reading CHECKING THE FILE STATUS: STEP 3 if( ! readFile ) { // OPTION 1 std::cout > var; // OR std::getline(readFile, str); // initialize while( ! readFile.eof() ) { // test condition std::cout > var; // OR std::getline(readFile, str); // update } CLOSING THE FILE: STEP 5 readFile.close(); // Disconnect from the file! REVIEW FILE STREAM TUTORIALS Complete 15.2 Streams tutorial with engagement activities Click the Next Section arrow > button on the bottom right side Complete 15.3 File Input tutorial with engagement activities Click the Next Section arrow > button on the bottom right side Complete 15.4 File Output tutorial with engagement activities REVIEW EXERCISES Complete the Parsons exercises from thinkcpp Problems 1-10 (Pick minimum of 3 to try) Change the question type to Parsons – you may have to change to "Active Write" and then change back to "Parsons" Parsons questions are drag and drop questions with the code mixed up. STRINGSTREAM FOR PARSING DATA Understanding library and the classes within: std::stringstream std::ostringstream std::istringstream Work through the tutorial from CodeHS: File I/O in C++ https://codehs.com/tutorial/david/file-io-in-c EARN YOUR PRE-WORK GRADE Post your weekly discussion question and research solution to D2L TODO Complete Week 08 Content Module in D2L to 100% WHAT'S COMING UP NEXT...WEEK 09 Midterm QUESTIONS | CLARIFICATIONS | HELP Student Office Hours: Schedule Meeting with Julie o By Appointment o Drop-In Times Available (on-campus) Email: [email protected] RRCC On Campus Tutoring: https://www.rrcc.edu/learning- commons/tutoring 24/7 Online Tutoring: D2L > Content > Resources for Help