- 0
- 0
- 约1.38万字
- 约 37页
- 2021-11-30 发布于安徽
- 举报
String Stream Processing I/O of strings to and from memory Called in-memory I/O or string stream processing Classes istringstream // input from string ostringstream // output to a string stringstream( string ) // most useful Requires sstream and iostream headers Use string formatting to save data to memory allows a string to be used as an internal file useful for buffering input and output Serves as a conduit to an anonymous string which can be read with the built-in oss.str() function that is bound to the oss object ostringstream oss; int n = 44; float x = 3.14; oss Hello!\t n \t x; string s = oss.str(); cout endl s endl; Output String Stream All extractions from iss will come from the contents of buffer, as if it were an external file. const string buffer = oss.str(); istringstream iss(buffer); string word; int m; float y; iss word m y; s = iss.str(); cout endl s endl; cout word = word endl; cout m = m endl; cout y = y endl; iss is defined and bound to buffer Contents of buffer can be accessed as elements of a string, or by formatted input through the iss object. Input String Stream #include iostream #include fstream #include iomanip #include string #include sstream using namespace std; int main(){ string s1(mydata.txt); ifstream in( s1.c_str() ); char buffer[1024]; while( in.getline( buffer, 1024 ) ){ string stemp( buffer ); cout Line is: stemp endl; if( stemp[0] != # ){ stringstream stris( stemp ); double d1, d2; stris d1 d2; cout d1 , d2 endl; } cout endl; } in.close(); return 0; } Using string example Input file: 1.0 2.0 1.1 2.4 1.8 2.8 #1.34 2.99 1.4 8.99 Example Output: Line is:1.0 2.0 1,2 Line is:1.1 2.4 1.1,2.4 Line is:1.8 2.8 1.8,2.8 Line is:#1.34 2.99 Line is:1.4 8.99 1.4,8.99 #include iostream #include fstream #include iomanip #include string #include sst
原创力文档

文档评论(0)