1 / 6

C++ stringstream

C++ stringstream. The I/O problem. Accepting text input from the user is one of the most difficult programming problems in any language Most I/O libraries are capable of reading in all sorts of data But they are less good at dealing with unexpected data

rubyw
Download Presentation

C++ stringstream

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ stringstream

  2. The I/O problem • Accepting text input from the user is one of the most difficult programming problems in any language • Most I/O libraries are capable of reading in all sorts of data • But they are less good at dealing with unexpected data • E.g. character data entered where a number is expected • E.g. Orphan new line characters in input

  3. Using strings • The standard solution to the problem of dealing with unexpected user input is to read in data one full line at a time into a string • e.g. by using getline() functions • Problem is that extracting meaningful data from the string is hard • We lose all the advantages of the input routines provided by C++ stream types

  4. stringstream • In C programming we are able to break up strings using sscanf(), which offers all the features of scanf(), but operates on strings • In C++ the stringstream class provides an interface to manipulate strings as if they were streams • We can use all the usual stream operators on objects of stringstream type • >>, <<, getline, etc.

  5. stringstream • The stringstream class can be used • to extract data from a string • to build up a string of data using the notation for writing to a file

  6. stringstream #include <sstream> int val; stringstream s; s << “120 42 277 6 5 200” for (int i = 0; i < 6; i++ ) { s >> val; cout << val << endl; }

More Related