Presentation is loading. Please wait.

Presentation is loading. Please wait.

Namespaces, I/O streams. Namespaces Namespace – what’s this? Namespace – what’s this?namespace When do we need the namespace? When do we need the namespace?

Similar presentations


Presentation on theme: "Namespaces, I/O streams. Namespaces Namespace – what’s this? Namespace – what’s this?namespace When do we need the namespace? When do we need the namespace?"— Presentation transcript:

1 Namespaces, I/O streams

2 Namespaces Namespace – what’s this? Namespace – what’s this?namespace When do we need the namespace? When do we need the namespace?

3 Namespaces – using namespace mine { void printf(int); class file; void fprintf(const file &, int); } mine::printf(44); mine::file infile; fprintf(infile, 31415323);// if at least one function argument comes from // the „mine” namespace, then the function // fprintf() is looked for in this namespace first // (Koenig search rule)

4 Namespaces – using Namespaces may be extended/joined Namespaces may be extended/joined in a single source file in a single source file in many files in many files Any given name (or all the names) may be moved from namespace scope to the global (or local) scope Any given name (or all the names) may be moved from namespace scope to the global (or local) scope using mine::file; using mine::file; using namespace mine; using namespace mine; Avoid using „using” in the header files! Avoid using „using” in the header files! why? why? use the scope operator instead. use the scope operator instead.

5 Namespace std The C++ standard library is defined in the std namespace. The C++ standard library is defined in the std namespace. stream classes stream classes string classes string classes STL STL headers „with.h” and „without.h” headers „with.h” and „without.h” interesting fact: „*.hpp” and „*.hxx” are considered obsolete and non-standard interesting fact: „*.hpp” and „*.hxx” are considered obsolete and non-standard

6 Standard C++ I/O streams IOStream library (iostream) IOStream library (iostream) it’s the oldest part of the standard library it’s the oldest part of the standard library developed by AT&T developed by AT&T support for internationalization support for internationalization string types supported by library string types supported by library char * (compatibility with earlier routines) char * (compatibility with earlier routines) string string iostream is a library of templates, iostream is a library of templates, iostream uses exception mechanism iostream uses exception mechanism

7 IOStream library – basics classes classes input stream istream (from template: basic_istream ) input stream istream (from template: basic_istream ) output stream ostream (from template: basic_ostream ) output stream ostream (from template: basic_ostream )

8 IOStream library – basics objects (header file: ) objects (header file: ) istream cin; //stdin, buffered istream cin; //stdin, buffered ostream cout; //stdout, buffered ostream cout; //stdout, buffered ostream cerr; //stderr, not buffered ostream cerr; //stderr, not buffered //by default outputs to console ostream clog; //no C equivalent, buffered ostream clog; //no C equivalent, buffered //by default outputs to console //by default outputs to console

9 IOStream library – basics operators > operators > overloaded for fundamental types, including char *, void *, bool, overloaded for fundamental types, including char *, void *, bool, to be overloaded for classes requiring stream i/o to be overloaded for classes requiring stream i/o istream is; ostream os; int i; double d; os >i; os >d; os >i>>d; cout >i>>d; cout<< "\ni is " << i << " and d is " << d << " \n";

10 IOStream library – basics Manipulators Manipulators this are objects used to modify default behaviour of a stream (e.g. formatting), output „end of line” etc. this are objects used to modify default behaviour of a stream (e.g. formatting), output „end of line” etc. programmer may define his own operators programmer may define his own operators endl //outputs end of line and flushes the stream buffer cout<< "\ni is " << i << " and d is " << d << endl; ends// "\0"; ws// for istream stream read (and skip) white spaces flush dec, hex, oct

11 IOStream library – details ios_base basic_streambuf<> basic_streambuf<>basic_ios<> virtual virtual basic_istream<> basic_ostream<> basic_iostream<> basic_iostream<>

12 IOStream library – details ios_base common interface for stream classes independent on the class/type of the stream elements common interface for stream classes independent on the class/type of the stream elements current condition of the stream current condition of the stream formatting of the data being processed formatting of the data being processed

13 IOStream library – details basic_ios<> common interface for stream classes in extent dependent on the class/type of the stream elements common interface for stream classes in extent dependent on the class/type of the stream elements definition of the stream buffer (class derived from template basic_streambuf<> for specific class/type of the stream element, definitions of methods actually reading/writting data) definition of the stream buffer (class derived from template basic_streambuf<> for specific class/type of the stream element, definitions of methods actually reading/writting data)

14 IOStream library – details basic_istream<>basic_ostream<> templates for classes of read-only/write-only streams templates for classes of read-only/write-only streams they inherit virtually template basic_ios<> they inherit virtually template basic_ios<> for the char template argument they’re derived from istream i ostream respectively for the char template argument they’re derived from istream i ostream respectively headers: and headers: and

15 IOStream library – details basic_iostream<> templates of stream classes capable of reading and writting templates of stream classes capable of reading and writting header header

16 IOStream library – details result of stream operations result of stream operations status of the stream (within scope of ios_base, constants of type iostate) status of the stream (within scope of ios_base, constants of type iostate) goodbit// everything ok goodbit// everything ok // bits described below are zeroes // bits described below are zeroes eofbit// end of file reached eofbit// end of file reached failbit// last operation failed failbit// last operation failed // following operations will fail // following operations will fail // until failbit gets set to 0 // until failbit gets set to 0 badbit// failure; stream destroyed badbit// failure; stream destroyed

17 IOStream library – details result of stream operations - methods result of stream operations - methods bool good()// everything ok bool good()// everything ok bool eof() // end of file reached bool eof() // end of file reached bool fail() // last operation failed bool fail() // last operation failed bool bad() // failure; stream destroyed bool bad() // failure; stream destroyed rdstate()// read the stream state rdstate()// read the stream state clear()// set the state to goodbit clear()// set the state to goodbit clear(state)// set state to state clear(state)// set state to state setstate(state)//equivalent of setstate(state)//equivalent of // clear(rdstate() | state)

18 IOStream library – details result of stream operations – conversion operators result of stream operations – conversion operators operator void* ()// equivalent of !fail() operator void* ()// equivalent of !fail() if (cin>>x) // or: while(cin>>x) { // x read without an error } operator ! () // equivalent of fail() operator ! () // equivalent of fail() if ( !!(cin>>x)) // or: while( !!(cin>>x)) { // x read without an error }

19 IOStream library – details result of stream operations – exceptions result of stream operations – exceptions to define when to throw exceptions (method of stream class): to define when to throw exceptions (method of stream class):exceptions(flags) to check what may cause throwing exceptions (method of stream class): to check what may cause throwing exceptions (method of stream class):exceptions() if it returns goodbit, then exceptions are never thrown

20 IOStream library – details i/o – formatted and unformatted i/o – formatted and unformatted operators > by default do formatted i/o (specific precision, skipping white spaces etc.) operators > by default do formatted i/o (specific precision, skipping white spaces etc.) methods get*/put*/read*/write* etc. are for unformatted i/o only methods get*/put*/read*/write* etc. are for unformatted i/o only

21 IOStream library – details formatted i/o formatted i/o methods for format flags (ios::fmtflags) methods for format flags (ios::fmtflags) setf(flags);// set flags setf(flags);// set flags setiosflags(flags);// equivalent to setf(flags); setiosflags(flags);// equivalent to setf(flags); setf(flags, mask);// set flags within the mask group only setf(flags, mask);// set flags within the mask group only resetiosflags(mask);// equivalent to setiosflags(0, mask); resetiosflags(mask);// equivalent to setiosflags(0, mask); unsetf(flags);// clear flags unsetf(flags);// clear flags flags();// retrn flags currently set flags();// retrn flags currently set flags(flags);// set flags, clear all the remaining flags(flags);// set flags, clear all the remaining copyfmt(stream)// copy flags from stream copyfmt(stream)// copy flags from stream

22 IOStream library – details formatted i/o formatted i/o flags and masks flags and masks boolalpha// flag: type bool numeric (0/1) or descriptive boolalpha// flag: type bool numeric (0/1) or descriptive // true/false // true/false // there are also manipulators: boolalpha and noboolalpha adjustfield// mask: alignment (left, right, internal) adjustfield// mask: alignment (left, right, internal) left// flag: align to left left// flag: align to left right// flag: align to right right// flag: align to right internal// flag: for digits align sign to left internal// flag: for digits align sign to left // remaining characters to right // remaining characters to right // there are also manipulators: left, right and internal

23 IOStream library – details formatted i/o formatted i/o flags and masks flags and masks showpos// output „+” for positive numbers showpos// output „+” for positive numbers // there are also manipulators: showpos and noshowpos uppercase// print (upper case) hexadecimal digits uppercase// print (upper case) hexadecimal digits // there are also manipulators: uppercase and nouppercase basefield// mask: for bases of numerical system (see below) basefield// mask: for bases of numerical system (see below) dec, hex oct// flags for different numerical systems dec, hex oct// flags for different numerical systems (empty)// flag: output as dec, input according to (empty)// flag: output as dec, input according to // actual prefix 0x – hex, 0 – oct, else: dec // actual prefix 0x – hex, 0 – oct, else: dec // there are also manipulators: dec hex and oct

24 IOStream library – details formatted i/o formatted i/o flags and masks flags and masks showbase// precede numbers with prefix of numerical system showbase// precede numbers with prefix of numerical system // there are also manipulators: showbase and noshowbase floatfield// mask: format of the floating point floatfield// mask: format of the floating point fixed// decimal fraction (eg. 3.14159265323) fixed// decimal fraction (eg. 3.14159265323) scientific// use exponent (eg. 3.14e+0) scientific// use exponent (eg. 3.14e+0) (empty)// pick best one depending on value being oputput (empty)// pick best one depending on value being oputput // there are also manipulators: fixed and scientific showpoint// always output the dot (3. 1415) showpoint// always output the dot (3. 1415) // for the floating point // for the floating point // there are also manipulators: showpoint and noshowpoint

25 IOStream library – details formatted i/o formatted i/o precision()// return the precision precision()// return the precision // for the floating point, // by default 6 digits after dot precision(p)// return the precision precision(p)// return the precision // for the floating point // there is also manipulator: setprecision(w) // there is also manipulator: setprecision(w)

26 IOStream library – details formatted i/o formatted i/o flags flags skipws// skip white spaces skipws// skip white spaces // there are also manipulators: skipws and noskipws unitbuf// flush the output buffer unitbuf// flush the output buffer // after each << // after each << // there are also manipulators: unitbuf and nounitbuf

27 IOStream library – details formatted i/o formatted i/o width()// return width of the filed (number of characters) width()// return width of the filed (number of characters) // interpreted as: not less than // interpreted as: not less than width(w)// set the width of the filed (number of characters) width(w)// set the width of the filed (number of characters) // interpreted as: not less than // interpreted as: not less than // there is also manipulator: setw(w) char buf[80]; cin >> setw(sizeof(buffer)) >> buffer; fill() // return character that is used for filling the field fill() // return character that is used for filling the field fill(c) // set this character fill(c) // set this character // there is also manipulator: setfill(c)

28 IOStream library – details formatted i/o formatted i/o methods for internationalization, different character sets, etc. methods for internationalization, different character sets, etc. described in detail in the library reference ;-) described in detail in the library reference ;-)

29 IOStream library – details unformatted i/o unformatted i/o method get method get int get();// read one character, or EOF int get();// read one character, or EOF // equivalent to getchar()/getc() // equivalent to getchar()/getc() istream& get(char &c);// no EOF, instead test the stream state istream& get(char &c);// no EOF, instead test the stream state istream& get(char *pc, streamsize cnt) istream& get(char *pc, streamsize cnt) istream& get(char *pc, streamsize cnt, char delim) istream& get(char *pc, streamsize cnt, char delim) // read to pc buffer, stop after having read // read to pc buffer, stop after having read // cnt-1 characters, or after (just before) the delim character // cnt-1 characters, or after (just before) the delim character // do not input delim, append ‘\0’ // do not input delim, append ‘\0’ istream& ignore(streamsize cnt, char delim) istream& ignore(streamsize cnt, char delim) // couple variants, read, but do not store // couple variants, read, but do not store

30 IOStream library – details unformatted i/o unformatted i/o method getline method getline istream& getline(char *pc, streamsize cnt) istream& getline(char *pc, streamsize cnt) istream& getline(char *pc, streamsize cnt, char delim) istream& getline(char *pc, streamsize cnt, char delim) // read to pc buffer, stop after having read // cnt-1 characters, or after (just before) the delim // cnt-1 characters, or after (just before) the delim // or after reading end of line // delim is being read from the stream // if number of read characters is less than cnt // then set failbit state

31 IOStream library – details unformatted i/o unformatted i/o methods read and readsome – some aspects methods read and readsome – some aspects istream& read(char *pc, streamsize cnt) istream& read(char *pc, streamsize cnt) // read cnt characters // read cnt characters streamsize readsome(char *pc, streamsize cnt) streamsize readsome(char *pc, streamsize cnt) // return number of characters read, // return number of characters read, // read no more than cnt-1 characters // read no more than cnt-1 characters // input only the characters, that already are // input only the characters, that already are // in the input buffer (not destructive)

32 IOStream library – details unformatted i/o unformatted i/o int peek(char *pc, streamsize cnt) int peek(char *pc, streamsize cnt) // read next character, leave it in the buffer // read next character, leave it in the buffer istream& unget() istream& unget() // return to the stream one most recently read character // return to the stream one most recently read character // depending on the implementation, you may call it // depending on the implementation, you may call it // 1 or more times in a row // if it fails than set badbit istream& putback(char c) istream& putback(char c) // as above, but if c is not the most recently read character // as above, but if c is not the most recently read character // then set badbit

33 IOStream library – details unformatted i/o unformatted i/o ostream& put(char c) ostream& put(char c) // output c to the stream // output c to the stream ostream& write(const char *pc, streamsize cnt) ostream& write(const char *pc, streamsize cnt) // output cnt characters from pc to the stream // output cnt characters from pc to the stream // do not append the \0 // do not append the \0 ostream& flush() ostream& flush()

34 IOStream library – basics File stream classes File stream classes input stream ifstream (derived from template: basic_ifstream /derived from basic_istream /) input stream ifstream (derived from template: basic_ifstream /derived from basic_istream /) output stream ofstream (derived from template: basic_ofstream / derived from basic_ostream /) output stream ofstream (derived from template: basic_ofstream / derived from basic_ostream /) i/o stream fstream (derived from template: basic_fstream / derived from basic_iostream /) i/o stream fstream (derived from template: basic_fstream / derived from basic_iostream /)

35 IOStream library – basics int i; ifstream infile(„file.txt”); if (! infile) cout<<„file not opened”; cout<<„file not opened”;else infile >>i; infile >>i;

36 IOStream library – basics Flags for file streams (from the scope std::ios) Flags for file streams (from the scope std::ios) constructor arguments: fstream(name, flags=def) constructor arguments: fstream(name, flags=def) in// flag : input stream, default for ifstream in// flag : input stream, default for ifstream out// flag : output stream, default for ofstream out// flag : output stream, default for ofstream app// flag : for input stream, set the position to end of file app// flag : for input stream, set the position to end of file ate// flag : set the position to end of file ate// flag : set the position to end of file trunc// flag : remove the file contents (if not already empty) trunc// flag : remove the file contents (if not already empty) binary// flag : binary mode (do not interpret CR LF) binary// flag : binary mode (do not interpret CR LF) ifstream ibin(„data.bin”, std::ios::in | std::ios::binary);

37 IOStream library – basics Methods for file streams Methods for file streams open// ( file_name [, flags] ) open// ( file_name [, flags] ) close// () close// () is_open// is the file opened? is_open// is the file opened? tellg/tellp// get actual reading/writting(p) position (offset) tellg/tellp// get actual reading/writting(p) position (offset) seekg/seekp// seek for reading/writting seekg/seekp// seek for reading/writting // overloaded variants: relative/absolute position // overloaded variants: relative/absolute position ifstream ibin(„data.bin”, std::ios::in | std::ios::binary);

38 IOStream library – details Details on file stream classes Details on file stream classes described in detail in the library reference ;-) described in detail in the library reference ;-)

39 IOStream library – basics Classes for memory streams (string streams) Classes for memory streams (string streams) input stream istringstream (derived from template: basic_istringstream /derived from basic_istream /) input stream istringstream (derived from template: basic_istringstream /derived from basic_istream /) output stream ostringstream (derived from template: basic_ostringstream / derived from basic_ostream /) output stream ostringstream (derived from template: basic_ostringstream / derived from basic_ostream /) i/o stream stringstream (derived from template: basic_stringstream / derived from basic_iostream /) i/o stream stringstream (derived from template: basic_stringstream / derived from basic_iostream /)

40 IOStream library – details Details on memory stream classes Details on memory stream classes described in detail in the library reference ;-) described in detail in the library reference ;-)


Download ppt "Namespaces, I/O streams. Namespaces Namespace – what’s this? Namespace – what’s this?namespace When do we need the namespace? When do we need the namespace?"

Similar presentations


Ads by Google