Question? Leave a message!




Handling Streams in C++

Handling Streams in C++
Dr.MasonHanks Profile Pic
Dr.MasonHanks,Germany,Teacher
Published Date:23-07-2017
Website URL
Comment
CIS 190: C/C++ Programming Lecture 7 C++ Streams 1 Outline • Handling Streams in C++ – Input Control – Output Control – String Streams • Errors in C++ • Header Protection • Homework 2 Using Input Streams • input streams include • istream – like reading in from the terminal • ifstream – like reading in from a file • istringstream – which we’ll cover later today 3 Using Input Streams • there are many ways to use input streams, with varying levels of precision/control – the operator –read() –ignore() –get() –getline() 4 Types of Whitespace • many of the input streams delineate using whitespace – they’ll skip leading whitespace – and stop at the next whitespace • common types of whitespace: – space, tab, newline – carriage return (\r) – can cause problems • sometimes used in Windows and Mac files 5 The Operator • returns a boolean for (un)successful read • just like scanf and fscanf: – skips leading whitespace – stops at the next whitespace (without reading it in) • appends a null terminator to strings read in The Operator: Example cout “Please enter your first ” “and last name separated ” “by a space: ”; cin firstName lastName; cout “Please enter your age: ” cin age; 7 ignore() • istream& ignore (streamsize n = 1, int delim = EOF); • takes in: – an integer (default value: 1) – a character delimiter (default value: EOF) • both arguments are optional 8 ignore() • istream& ignore (streamsize n = 1, int delim = EOF); • ignore extracts characters and discards them until either: –n characters are extracted –delim is reached 9 ignore(): Example • istream& ignore (streamsize n = 1, int delim = EOF); iStream.ignore(); iStream.ignore(‘ ’); iStream.ignore(512); iStream.ignore(512, ‘ ’); 10 read() • istream& read (char s, streamsize n); • takes in: – a character array (a C string) – a size • streamsize is a typedef of a signed integral type 11 read() • istream& read (char s, streamsize n); • copies a block of data of size n characters – stops after n characters, or at EOF – without checking its contents – without appending a NULL terminator – without moving through the input • often used in conjuction with ignore() 12 read(): Example • istream& read (char s, streamsize n); char strArrSIZE; inStream.read(strArr, SIZE-1); / do stuff with strArr / // if you want to move on: inStream.ignore(SIZE-1); 13 get() • istream& get (char &c); • takes in – a pointer to a character • stores a single character – does not skip whitespace cin.get(&character); 14 get() • int get (); • returns a single character – the ASCII value of the character read in character = cin.get(); 15 Multiple Prototypes • get() has two prototypes: int get (); istream& get (char &c); • this is called overloading • many library functions are overloaded – which function is called depends on the arguments • you too can do this in C++ (we’ll cover it soon) 16 getline() • istream& getline (char s, streamsize n); • takes in: – a character array – a size • extracts up to n characters – stops extracting characters upon hitting ‘\n’ – also stops if it hits EOF 17 getline() • istream& getline (char s, streamsize n); • the newline is read in, and discarded – (not stored in the character array) • carriage returns can cause problems, so be aware of the file’s origin and format 18 getline(): Example • istream& getline (char s, streamsize n); char chArray 128; streamIn.getline(chArray, 128-1); / use “128-1” to leave room for the null terminator / 19 istream& ? • istream& ignore (streamsize n = 1, int delim = EOF); • istream& read (char s, streamsize n); • istream& get (char &c); • istream& getline (char s, streamsize n); 20