¡¡¡¡½ÓÏÂÀ´ÎÒÃǼÌÐø¿´Ò»ÏÂC++·ç¸ñµÄ´®Á÷¿ØÖÆ£¬C++ÒýÈëÁËostringstream¡¢istringstream¡¢stringstreamÕâÈý¸öÀ࣬ҪʹÓÃËûÃÇ´´½¨¶ÔÏó¾Í±ØÐë°üº¬sstream.hÍ·Îļþ¡£
¡¡¡¡istringstreamÀàÓÃÓÚÖ´ÐÐC++·ç¸ñµÄ´®Á÷µÄÊäÈë²Ù×÷¡£ ¡¡¡¡ostringstreamÀàÓÃÓÚÖ´ÐÐC·ç¸ñµÄ´®Á÷µÄÊä³ö²Ù×÷¡£ ¡¡¡¡strstreamÀàͬʱ¿ÉÒÔÖ§³ÖC·ç¸ñµÄ´®Á÷µÄÊäÈëÊä³ö²Ù×÷¡£
¡¡¡¡istringstreamÀàÊÇ´Óistream£¨ÊäÈëÁ÷ÀࣩºÍstringstreambase£¨c++×Ö·û´®Á÷»ùÀࣩÅÉÉú¶øÀ´£¬ostringstreamÊÇ´Óostream£¨Êä³öÁ÷ÀࣩºÍstringstreambase£¨c++×Ö·û´®Á÷»ùÀࣩÅÉÉú¶øÀ´£¬stringstreamÔòÊÇ´Óiostream(ÊäÈëÊä³öÁ÷Àà)ºÍºÍstringstreambase£¨c++×Ö·û´®Á÷»ùÀࣩÅÉÉú¶øÀ´¡£
¡¡¡¡ËûÃǵļ̳йØϵÈçÏÂͼËùʾ:
¡¡¡¡istringstreamÊÇÓÉÒ»¸östring¶ÔÏó¹¹Ôì¶øÀ´£¬istringstreamÀà´ÓÒ»¸östring¶ÔÏó¶ÁÈ¡×Ö·û¡£ ¡¡¡¡istringstreamµÄ¹¹Ô캯ÊýÔÐÎÈçÏ£º ¡¡¡¡istringstream::istringstream(string str);
//³ÌÐò×÷Õß:¹ÜÄþ //Õ¾µã:www.cndev-lab.com //ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß #include <iostream> #include <sstream> using namespace std; int main() { istringstream istr; istr.str("1 56.7",); //ÉÏÊöÁ½¸ö¹ý³Ì¿ÉÒÔ¼òµ¥Ð´³É istringstream istr("1 56.7"); cout << istr.str()<<endl; int a; float b; istr>>a; cout<<a<<endl; istr>>b; cout<<b<<endl; system("pause"); }
¡¡¡¡ÉÏÀýÖУ¬¹¹Ôì×Ö·û´®Á÷µÄʱºò£¬¿Õ¸ñ»á³ÉΪ×Ö·û´®²ÎÊýµÄÄÚ²¿·Ö½ç£¬Àý×ÓÖжÔa,b¶ÔÏóµÄÊäÈë"¸³Öµ"²Ù×÷Ö¤Ã÷ÁËÕâÒ»µã£¬×Ö·û´®µÄ¿Õ¸ñ³ÉΪÁËÕûÐÍÊý¾ÝÓ븡µãÐÍÊý¾ÝµÄ·Ö½âµã£¬ÀûÓ÷ֽç»ñÈ¡µÄ·½·¨ÎÒÃÇÊÂʵÉÏÍê³ÉÁË×Ö·û´®µ½ÕûÐͶÔÏóÓ븡µãÐͶÔÏóµÄ²ð·Öת»»¹ý³Ì¡£
¡¡¡¡str()³ÉÔ±º¯ÊýµÄʹÓÿÉÒÔÈÃistringstream¶ÔÏó·µ»ØÒ»¸östring×Ö·û´®£¨ÀýÈç±¾ÀýÖеÄÊä³ö²Ù×÷(cout<<istr.str();£©¡£
¡¡¡¡ostringstreamͬÑùÊÇÓÉÒ»¸östring¶ÔÏó¹¹Ôì¶øÀ´£¬ostringstreamÀàÏòÒ»¸östring²åÈë×Ö·û¡£ ¡¡¡¡ostringstreamµÄ¹¹Ô캯ÊýÔÐÎÈçÏ£º ¡¡¡¡ostringstream::ostringstream(string str);
¡¡¡¡Ê¾Àý´úÂëÈçÏ£º
//³ÌÐò×÷Õß:¹ÜÄþ //Õ¾µã:www.cndev-lab.com //ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß #include <iostream> #include <sstream> #include <string> using namespace std; int main() { ostringstream ostr; //ostr.str("abc");//Èç¹û¹¹ÔìµÄʱºòÉèÖÃÁË×Ö·û´®²ÎÊý,ÄÇôÔö³¤²Ù×÷µÄʱºò²»»á´Ó½áβ¿ªÊ¼Ôö¼Ó,¶øÊÇÐÞ¸ÄÔÓÐÊý¾Ý,³¬³öµÄ²¿·ÖÔö³¤ ostr.put('d'); ostr.put('e'); ostr<<"fg"; string gstr = ostr.str(); cout<<gstr; system("pause"); }
¡¡¡¡ÔÚÉÏÀý´úÂëÖУ¬ÎÒÃÇͨ¹ýput()»òÕß×óÒƲÙ×÷·û¿ÉÒÔ²»¶ÏÏòostr²åÈëµ¥¸ö×Ö·û»òÕßÊÇ×Ö·û´®£¬Í¨¹ýstr()º¯Êý·µ»ØÔö³¤¹ýºóµÄÍêÕû×Ö·û´®Êý¾Ý£¬µ«ÖµµÃ×¢ÒâµÄÒ»µãÊÇ£¬µ±¹¹ÔìµÄʱºò¶ÔÏóÄÚÒѾ´æÔÚ×Ö·û´®Êý¾ÝµÄʱºò£¬ÄÇôÔö³¤²Ù×÷µÄʱºò²»»á´Ó½áβ¿ªÊ¼Ôö¼Ó,¶øÊÇÐÞ¸ÄÔÓÐÊý¾Ý,³¬³öµÄ²¿·ÖÔö³¤¡£
¡¡¡¡¶ÔÓÚstringstreamÁËÀ´Ëµ£¬²»ÓÃÎÒ¶à˵£¬´ó¼ÒÒ²ÒѾ֪µÀËüÊÇÓÃÓÚC++·ç¸ñµÄ×Ö·û´®µÄÊäÈëÊä³öµÄ¡£ ¡¡¡¡stringstreamµÄ¹¹Ô캯ÊýÔÐÎÈçÏ£º
¡¡¡¡stringstream::stringstream(string str);
¡¡¡¡Ê¾Àý´úÂëÈçÏÂ:
//³ÌÐò×÷Õß:¹ÜÄþ //Õ¾µã:www.cndev-lab.com //ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß #include <iostream> #include <sstream> #include <string> using namespace std; int main() { stringstream ostr("ccc"); ostr.put('d'); ostr.put('e'); ostr<<"fg"; string gstr = ostr.str(); cout<<gstr<<endl; char a; ostr>>a; cout<<a system("pause"); }
¡¡¡¡³ý´Ë¶øÍ⣬stringstreamÀàµÄ¶ÔÏóÎÒÃÇ»¹³£ÓÃËü½øÐÐstringÓë¸÷ÖÖÄÚÖÃÀàÐÍÊý¾ÝÖ®¼äµÄת»»¡£
¡¡¡¡Ê¾Àý´úÂëÈçÏ£º
//³ÌÐò×÷Õß:¹ÜÄþ //Õ¾µã:www.cndev-lab.com //ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß #include <iostream> #include <sstream> #include <string> using namespace std; int main() { stringstream sstr; //--------intתstring----------- int a=100; string str; sstr<<a; sstr>>str; cout<<str<<endl; //--------stringתchar[]-------- sstr.clear();//Èç¹ûÄãÏëͨ¹ýʹÓÃͬһstringstream¶ÔÏóʵÏÖ¶àÖÖÀàÐ͵Äת»»£¬Çë×¢ÒâÔÚÿһ´Îת»»Ö®ºó¶¼±ØÐëµ÷ÓÃclear()³ÉÔ±º¯Êý¡£ string name = "colinguan"; char cname[200]; sstr<<name; sstr>>cname; cout<<cname; system("pause"); }
¡¡¡¡½ÓÏÂÀ´ÎÒÃÇÀ´Ñ§Ï°Ò»ÏÂÊäÈë/Êä³öµÄ״̬±êÖ¾µÄÏà¹Ø֪ʶ£¬C++ÖиºÔðµÄÊäÈë/Êä³öµÄϵͳ°üÀ¨Á˹ØÓÚÿһ¸öÊäÈë/Êä³ö²Ù×÷µÄ½á¹ûµÄ¼Ç¼ÐÅÏ¢¡£ÕâЩµ±Ç°µÄ״̬ÐÅÏ¢±»°üº¬ÔÚio_stateÀàÐ͵ĶÔÏóÖС£io_stateÊÇÒ»¸öö¾ÙÀàÐÍ£¨¾ÍÏñopen_modeÒ»Ñù£©£¬ÒÔϱãÊÇËü°üº¬µÄÖµ¡£
goodbit ÎÞ´íÎó
Eofbit Òѵ½´ïÎļþβ
failbit ·ÇÖÂÃüµÄÊäÈë/Êä³ö´íÎ󣬿ÉÍì»Ø
badbit¡¡ÖÂÃüµÄÊäÈë/Êä³ö´íÎó,ÎÞ·¨Íì»Ø
¡¡¡¡ÓÐÁ½ÖÖ·½·¨¿ÉÒÔ»ñµÃÊäÈë/Êä³öµÄ״̬ÐÅÏ¢¡£Ò»ÖÖ·½·¨ÊÇͨ¹ýµ÷ÓÃrdstate()º¯Êý£¬Ëü½«·µ»Øµ±Ç°×´Ì¬µÄ´íÎó±ê¼Ç¡£ÀýÈ磬¼ÙÈçûÓÐÈκδíÎó£¬Ôòrdstate()»á·µ»Øgoodbit.
¡¡¡¡ÏÂÀýʾÀý£¬±íʾ³öÁËrdstate()µÄÓ÷¨£º
//³ÌÐò×÷Õß:¹ÜÄþ //Õ¾µã:www.cndev-lab.com //ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß #include <iostream> using namespace std; int main() { int a; cin>>a; cout<<cin.rdstate()<<endl; if(cin.rdstate() == ios::goodbit) { cout<<"ÊäÈëÊý¾ÝµÄÀàÐÍÕýÈ·£¬ÎÞ´íÎó£¡"<<endl; } if(cin.rdstate() == ios_base::failbit) { cout<<"ÊäÈëÊý¾ÝÀàÐÍ´íÎ󣬷ÇÖÂÃü´íÎ󣬿ÉÇå³ýÊäÈ뻺³åÇøÍì»Ø£¡"<<endl; } system("pause"); }
¡¡¡¡ÁíÒ»ÖÖ·½·¨ÔòÊÇʹÓÃÏÂÃæÈκÎÒ»¸öº¯ÊýÀ´¼ì²âÏàÓ¦µÄÊäÈë/Êä³ö״̬£º
bool bad();
bool eof();
bool fail();
bool good();
¡¡¡¡ÏÂÀýʾÀý£¬±íʾ³öÁËÉÏÃæ¸÷³ÉÔ±º¯ÊýµÄÓ÷¨£º
//³ÌÐò×÷Õß:¹ÜÄþ //Õ¾µã:www.cndev-lab.com //ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß #include <iostream> using namespace std; int main() { int a; cin>>a; cout<<cin.rdstate()<<endl; if(cin.good()) { cout<<"ÊäÈëÊý¾ÝµÄÀàÐÍÕýÈ·£¬ÎÞ´íÎó£¡"<<endl; } if(cin.fail()) { cout<<"ÊäÈëÊý¾ÝÀàÐÍ´íÎ󣬷ÇÖÂÃü´íÎ󣬿ÉÇå³ýÊäÈ뻺³åÇøÍì»Ø£¡"<<endl; } system("pause"); }
¡¡¡¡Èç¹û´íÎó·¢Éú£¬ÄÇôÁ÷״̬¼È±»±ê¼ÇΪ´íÎó£¬Äã±ØÐëÇå³ýÕâЩ´íÎó״̬£¬ÒÔʹÄãµÄ³ÌÐòÄÜÕýÈ·Êʵ±µØ¼ÌÐøÔËÐС£ÒªÇå³ý´íÎó״̬£¬ÐèʹÓÃclear()º¯Êý¡£´Ëº¯Êý´øÒ»¸ö²ÎÊý£¬ËüÊÇÄ㽫ҪÉèΪµ±Ç°×´Ì¬µÄ±êÖ¾Öµ¡££¬Ö»Òª½«ios::goodbit×÷Ϊʵ²Î¡£
¡¡¡¡Ê¾Àý´úÂëÈçÏ£º
//³ÌÐò×÷Õß:¹ÜÄþ //Õ¾µã:www.cndev-lab.com //ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß #include <iostream> using namespace std; int main() { int a; cin>>a; cout<<cin.rdstate()<<endl; cin.clear(ios::goodbit); cout<<cin.rdstate()<<endl; system("pause"); }
¡¡¡¡Í¨³£µ±ÎÒÃÇ·¢ÏÖÊäÈëÓдíÓÖÐèÒª¸ÄÕýµÄʱºò£¬Ê¹ÓÃclear()¸ü¸Ä±ê¼ÇΪÕýÈ·ºó£¬Í¬Ê±Ò²ÐèҪʹÓÃget()³ÉÔ±º¯ÊýÇå³ýÊäÈ뻺³åÇø£¬ÒÔ´ïµ½Öظ´ÊäÈëµÄÄ¿µÄ¡£
¡¡¡¡Ê¾Àý´úÂëÈçÏ£º
//³ÌÐò×÷Õß:¹ÜÄþ //Õ¾µã:www.cndev-lab.com //ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß #include <iostream> using namespace std; int main() { int a; while(1) { cin>>a; if(!cin)//Ìõ¼þ¿É¸ÄдΪcin.fail() { cout<<"ÊäÈëÓдí!ÇëÖØÐÂÊäÈë"<<endl; cin.clear(); cin.get(); } else { cout<<a; break; } } system("pause"); }
¡¡¡¡×îºóÔÙ¸ø³öÒ»¸ö¶ÔÎļþÁ÷´íÎó±ê¼Ç´¦ÀíµÄÀý×Ó£¬¹®¹Ìѧϰ£¬´úÂëÈçÏ£º
//³ÌÐò×÷Õß:¹ÜÄþ //Õ¾µã:www.cndev-lab.com //ËùÓиå¼þ¾ùÓаæȨ,ÈçҪתÔØ,ÇëÎñ±ØÖøÃû³ö´¦ºÍ×÷Õß #include <iostream> #include <fstream> using namespace std; int main() { ifstream myfile("c:\\1.txt",ios_base::in,0); if(myfile.fail()) { cout<<"Îļþ¶Áȡʧ°Ü»òÖ¸¶¨Îļþ²»´æÔÚ!"<<endl; } else { char ch; while(myfile.get(ch)) { cout<<ch; } if(myfile.eof()) { cout<<"ÎļþÄÚÈÝÒѾȫ²¿¶ÁÍê"<<endl; } while(myfile.get(ch)) { cout<<ch; } } system("pause"); }
|