reference informa9on
play

Reference informa9on Consult reference for complete - PDF document

2014-10-13 Reference informa9on Consult reference for complete informa9on! UNIX man-pages (available on exam): Lecture 3 man topic man istream


  1. 2014-­‑10-­‑13 ¡ Reference ¡informa9on ¡ • Consult ¡reference ¡for ¡complete ¡informa9on! ¡ • UNIX ¡man-­‑pages ¡(available ¡on ¡exam): ¡ Lecture ¡3 ¡ man ¡topic ¡ man ¡istream ¡ man ¡ostream ¡ Stream ¡I/O ¡ ios, ¡basic_string, ¡stringstream, ¡ctype, ¡numeric_limits ¡ • Online ¡(not ¡available ¡on ¡exam): ¡ www.cplusplus.com/reference/ ¡ File ¡separa9on ¡ Genera9ng ¡errors ¡ • Related ¡(cohesive) ¡func9ons ¡can ¡be ¡gathered ¡in ¡one ¡file ¡to ¡form ¡a ¡ • Your ¡goal ¡should ¡be ¡to ¡ no-ce ¡the ¡program ¡user ¡in ¡a ¡clear ¡ package. ¡ and ¡understandable ¡way ¡and ¡then ¡ recover . ¡ • A ¡package ¡can ¡be ¡compiled ¡separately, ¡and ¡do ¡not ¡need ¡ • We ¡will ¡cover ¡the ¡ recover ¡part ¡later ¡in ¡the ¡course. ¡ recompila9on ¡unless ¡you ¡change ¡a ¡package ¡source ¡file. ¡ ¡ • C ¡ways ¡ • Public ¡declara9ons ¡are ¡place ¡in ¡a ¡header ¡file ¡*.h ¡ – Message ¡in ¡program ¡or ¡system ¡log ¡(good) ¡ • Defini9ons ¡are ¡placed ¡in ¡a ¡implementa9on ¡file ¡*.cc ¡ – Message ¡to ¡standard ¡error ¡(poor) ¡ • Header ¡and ¡implementa9on ¡files ¡should ¡have ¡the ¡same ¡name, ¡ except ¡for ¡the ¡extension ¡ – Message ¡to ¡standard ¡output ¡(bad) ¡ • Header ¡file ¡must ¡have ¡a ¡ preprocessor ¡guard ¡to ¡protect ¡from ¡ – Exit ¡program ¡(bad) ¡ mul9ple ¡inclusion ¡ – Return ¡error ¡code ¡from ¡func9on ¡(bad) ¡ #ifndef _FILE_NAME_H_ • C++ ¡ways ¡ #define _FILE_NAME_H_ – Throw ¡anonymous ¡error ¡(bad) ¡ // public declarations – Throw ¡specific ¡error ¡(good) ¡ #endif Generate ¡error ¡the ¡C++ ¡way ¡ Stream ¡concept ¡ • An ¡ordered ¡stream ¡of ¡bytes ¡ #include <exception> string msg = “error message” • One ¡source ¡generates ¡bytes ¡ throw invalid_argument(msg); • One ¡des9na9on ¡consumes ¡bytes ¡ throw logic_error(“bad bool”); • Not ¡possible ¡to ¡break ¡the ¡given ¡order ¡ throw domain_error(“bad luck”); • Des9na9on ¡can ¡not ¡receive ¡again! ¡ • Blind: ¡can ¡not ¡see ¡future! ¡ #include <iostream> ¡ throw ios::failure(“bad file”); 1 ¡

  2. 2014-­‑10-­‑13 ¡ Think ¡of ¡speech! ¡ Hey! ¡What ¡if... ¡ • A ¡sequence ¡of ¡words. ¡ • I ¡have ¡a ¡different ¡source? ¡iPad, ¡Compact ¡Disc ¡ – Sure, ¡some ¡specific ¡sources ¡have ¡ addi-onal ¡ • Have ¡a ¡source: ¡mouth ¡(brain?) ¡ capabili9es, ¡more ¡than ¡the ¡standard ¡stream. ¡ • Have ¡a ¡des9na9on: ¡ear ¡(brain?) ¡ • I ¡ask ¡the ¡stream ¡to ¡replay? ¡ • You ¡can ¡not ¡choose ¡to ¡hear ¡later ¡words ¡before ¡ – NO. ¡Will ¡ refuse ¡to ¡replay. ¡Some9mes ¡it ¡can ¡let ¡you ¡ earlier. ¡ peek ¡on ¡what’s ¡coming, ¡but ¡don’t ¡depend ¡on ¡it. ¡ • You ¡can ¡not ¡choose ¡to ¡hear ¡a ¡word ¡again. ¡ • I ¡record ¡the ¡speech? ¡ – Sure, ¡you ¡can ¡record ¡any ¡stream ¡(the ¡sound), ¡but ¡you ¡ • It’s ¡a ¡stream! ¡ can ¡not ¡copy ¡the ¡stream ¡itself ¡(the ¡one ¡who ¡talk)! ¡ C++ ¡streams ¡ File ¡stream ¡input ¡ Must ¡be ¡connected ¡to ¡a ¡file ¡on ¡disk ¡before ¡use. ¡ • Variables ¡of ¡composed, ¡object, ¡type ¡ #include <fstream> • One ¡type ¡fits ¡all ¡ – istream ¡(in ¡to ¡program, ¡program ¡is ¡des9na9on) ¡ ifstream infile; // create stream object infile.open(”data.txt”); // connect stream – ostream ¡(out ¡from ¡program, ¡program ¡is ¡source) ¡ // object • Three ¡kinds ¡of ¡specific ¡streams ¡ ifstream in(”data.txt”); // create and // connect – General ¡I/O: ¡cin, ¡cout, ¡cerr, ¡clog ¡ int a, b; – File ¡streams ¡(file ¡on ¡disk ¡act ¡as ¡source ¡or ¡des9na9on) ¡ in >> a; // read from ’in’ – S9ng ¡streams ¡(string ¡variable ¡in ¡memory ¡act ¡as ¡source ¡ infile >> b; // what do you get?? or ¡des9na9on) ¡ in.close(); // disconnect File ¡stream ¡output ¡ String ¡stream ¡input ¡ Must ¡be ¡connected ¡to ¡a ¡file ¡on ¡disk ¡before ¡use. ¡ Must ¡be ¡connected ¡to ¡a ¡string ¡variable ¡before ¡use. ¡ #include <fstream> #include <sstream> ofstream ofile; // create stream object string data{”4711 512”}; ofile.open(”data.txt”); // connect stream // object istringstream instr; // create stream ofstream out(”data.txt”); // create and instr.str(data); // connect stream // connect istringstream in(data); // create, connect int a{4711}, b{512}; out << a; // write to ’out’ int a, b; ofile << b; // what is in the file?? in >> a; // read from ’in’ out.close(); // disconnect instr >> b; // what do you get?? 2 ¡

  3. 2014-­‑10-­‑13 ¡ String ¡stream ¡output ¡ Stream ¡error ¡sources ¡ • Every ¡stream ¡will ¡ SILENTLY ¡REFUSE ¡further ¡ #include <sstream> opera9on ¡afer ¡ ANY ¡failure: ¡ ostringstream outstr; // create stream • End ¡of ¡file ¡ – A ¡file ¡stream ¡reach ¡end ¡of ¡file ¡ int a{4711}, b{512}; – A ¡string ¡stream ¡reach ¡end ¡of ¡file ¡(string) ¡ – User ¡press ¡Ctrl-­‑D ¡to ¡signal ¡end ¡of ¡input ¡ outstr << a << b; • Logic ¡failure ¡ – Conversion ¡from ¡characters ¡to ¡number ¡fail ¡ string str{outstr.str()}; • Serious ¡error ¡ – Like ¡hardware ¡failure ¡ // what is in ’str’? Error ¡checking ¡ Peculiari9es ¡(that ¡make ¡sense) ¡ • Must ¡be ¡performed ¡ just ¡aCer ¡each ¡opera9on ¡ • bad() ¡and ¡good() ¡are ¡not ¡opposites ¡ • Provide ¡possibility ¡to ¡ detect ¡error ¡ – A ¡stream ¡may ¡be ¡!good() ¡even ¡if ¡it’s ¡!bad() ¡ • Almost ¡never ¡used ¡explicitly! ¡ • Four ¡opera9ons ¡available: ¡ • fail() ¡and ¡good() ¡are ¡not ¡opposites ¡ – A ¡stream ¡may ¡be ¡!good() ¡even ¡if ¡it’s ¡!fail() ¡ bool eof(); // slammed into eof? bool fail(); // conversion failed? bool bad(); // unrecoverable? • eof() ¡can ¡be ¡set ¡when ¡fail() ¡is ¡not ¡set ¡ bool good(); // no error set? – Input ¡can ¡succeed ¡despite ¡slamming ¡into ¡eof! ¡ Error ¡checking ¡”as ¡usual” ¡ Error ¡clearing ¡ int a, b; • Every ¡stream ¡will ¡ REFUSE ¡further ¡opera9on ¡ cin >> a >> b; afer ¡ ANY ¡failure! ¡ if (cin) • Errors ¡must ¡be ¡ cleared ¡before ¡stream ¡can ¡be ¡ { operated ¡again! ¡ // formatted input did not fail } • Clearing ¡an ¡error ¡will ¡ NOT ¡EVER ¡fix ¡the ¡ while (cin >> a) problem! ¡ { // formatted input did not fail // clear all errorflags } void clear(); 3 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend