C++ϵͳѧϰ֮°Ë£ºIO¿â
2018-08-26 17:16:26À´Ô´£º²©¿ÍÔ° ÔĶÁ ()
¡¡¡¡ÐµÄC++±ê×¼ÖÐÓÐÈý·ÖÖ®¶þµÄÄÚÈݶ¼ÊÇÃèÊö±ê×¼¿â¡£½ÓÏÂÀ´ÖصãѧϰÆäÖм¸ÖÖºËÐÄ¿âÉèÊ©£¬ÕâЩÊÇÓ¦¸ÃÊìÁ·ÕÆÎյġ£
¡¡¡¡±ê×¼¿âµÄºËÐÄÊǺܶàÈÝÆ÷Àࣨ˳ÐòÈÝÆ÷ºÍ¹ØÁªÈÝÆ÷µÈ£©ºÍÒ»´Ø·ºÐÍËã·¨£¨¸ÃÀàË㷨ͨ³£ÔÚ˳ÐòÈÝÆ÷Ò»¶¨·¶Î§ÄÚµÄÔªËØÉÏ»òÆäËûÀàÐ͵ÄÐòÁÐÉϽøÐвÙ×÷£©¡£
¡¡¡¡¸ÃƪÖ÷ҪѧϰIO¿â¡£
¡¡¡¡C++ÓïÑÔ²»Ö±½Ó´¦ÀíÊäÈëÊä³ö£¬¶øÊÇͨ¹ýÒ»´Ø±ê×¼¿âÖеÄIO¿âÀ´´¦Àí¡£IO¿â¶¨ÒåÁ˶ÁдÄÚÖÃÀàÐÍÖµµÄ²Ù×÷¡£
1¡¢IOÀà
¡¡¡¡ÎªÁË´¦Àí²»Í¬µÄÖÖÀàµÄIO²Ù×÷£¬¶¨ÒåÁË£ºiostream(ÓÃÓÚ¶ÁдÁ÷µÄ»ù±¾ÀàÐÍ)¡¢fstream£¨¶ÁдÃüÃûÎļþµÄÀàÐÍ£©¡¢sstream£¨¶ÁдÄÚ´æstring¶ÔÏóµÄÀàÐÍ£©¡£
ÆäÖкóÃæÒÔw¿ªÍ·µÄÊÇ¿í×Ö·û°æ±¾¡£
IOÀàÐͼäµÄ¹Øϵ
¼Ì³Ð»úÖÆ£ºifstreamºÍistringstream¶¼ÊǼ̳Ð×Ôistream£¬ÉÏÊöËùÓеÄIO¿âÀàÐÍÖ®¼ä¶¼´æÔÚÕâÖּ̳йØϵ¡£
ÏÂÃæ½éÉܵıê×¼¿âÁ÷ÌØÐÔ¶¼¿ÉÒÔÎÞ²î±ðµØÓ¦ÓÃÓÚÆÕͨÁ÷¡¢ÎļþÁ÷ºÍstringÁ÷£¬ÒÔ¼°ËüÃǵĿí×Ö·û°æ±¾¡£
1.1 IO¶ÔÏóÎÞ¿½±´»ò¸³Öµ
²»ÄÜ¿½±´»ò¶ÔIO¶ÔÏó¸³Öµ
ofstream out1,out2; out1=out2; //´íÎ󣬲»ÄܶÔÁ÷¶ÔÏó¸³Öµ ofstream print(ofstream); //´íÎ󣬲»Äܳõʼ»¯ofstream²ÎÊý out2=print(out2); //´íÎ󣬲»ÄÜ¿½±´Á÷¶ÔÏó
ÒòΪ²»ÄÜ¿½±´IO¶ÔÏó£¬Òò´Ë²»Äܽ«Ðβλò·µ»ØÀàÐÍÉèÖÃΪÁ÷ÀàÐÍ¡£½øÐÐIO²Ù×÷µÄº¯Êýͨ³£ÒÔÒýÓ÷½Ê½´«µÝºÍ·µ»ØÁ÷¡£¶Áдһ¸öIO¶ÔÏó»á¸Ä±äÆä״̬£¬Òò´Ë´«µÝºÍ·µ»ØµÄÒýÓò»ÄÜÊÇconst¡£
1.2 Ìõ¼þ״̬
IO²Ù×÷¿ÉÄܻᷢÉú´íÎó£¬IOÀàͨ¹ý¶¨ÒåһЩÌõ¼þ״̬À´ÏÔʾIO¶ÔÏóµÄ״̬£¬Ê¹Óû§ÖªµÀÁ÷µÄµ±Ç°×´Ì¬¡£
ÓÉÓÚÁ÷²Ù×÷¿ÉÄÜ·¢ÉúµÄ´íÎó£¬Òò´Ë£¬´úÂëͨ³£Ó¦¸ÃÔÚʹÓÃÒ»¸öÁ÷֮ǰ¼ì²éËüÊÇ·ñ´¦ÓÚÁ¼ºÃ״̬£¬×î¼òµ¥µÄ·½·¨Êǽ«Ëüµ±×÷Ò»¸öÌõ¼þÀ´Ê¹Óá£
while(cin>>word) { ...... }
²éѯÁ÷µÄ״̬
ÓÐʱºò²»½ö½öÐèÒªÖªµÀÁ÷ÊÇ·ñÓÐЧ£¬»¹ÒªÖªµÀµ½µ×ÊÇʲôÔÒòµ¼ÖÂÁ÷ʧЧ£¬IO¿â¶¨ÒåÁËÒ»¸öÓë»úÆ÷Î޹صÄiostateÀàÐÍ£¬ËüÌṩÁ˱í´ïÁ÷״̬µÄÍêÕû¹¦ÄÜ¡£Õâ¸öÀàÐÍÓ¦×÷Ϊһ¸öλ¼¯ºÏÀ´Ê¹Óá£
ʹÓÃgood»òfailÊÇÈ·¶¨Á÷µÄ×ÜÌå״̬µÄÕýÈ··½·¨¡£ÉÏÊö½«Á÷×÷ΪÌõ¼þʹÓõĴúÂëµÈ¼ÛÓÚ£¡fail£¨£©¡£
while(cin>>word) { ...... } ================== cin >> s; while (!(cin.fail())) { }
¹ÜÀíÌõ¼þ״̬
Á÷¶ÔÏóµÄrdstate³ÉÔ±·µ»ØÒ»¸öiostateÖµ£¬¶ÔÓ¦Á÷µÄµ±Ç°×´Ì¬¡£setstate²Ù×÷½«¸ø¶¨Ìõ¼þλÖÃ룬±íʾ·¢ÉúÁ˶ÔÓ¦´íÎó¡£clear³ÉÔ±ÊÇÒ»¸öÖØÔصijÉÔ±¡£ÓÐÒ»¸öÎÞ²ÎÊýµÄ°æ±¾£¬Çå³ýËùÓдíÎó±ê־λ¡£
auto old_state=cin.rdstate(); //¼ÇסcinµÄµ±Ç°×´Ì¬ cin.clear(); //Çå³ýËùÓдíÎó process_input(cin); //ʹÓÃcin cin.setstate(old_state); //½«cinÖÃΪÔÓÐ״̬
1.3 ¹ÜÀíÊä³ö»º³å
ÿ¸öÊä³öÁ÷¶¼¹ÜÀíÒ»¸ö»º³åÇø£¬ÓÃÀ´±£´æ³ÌÐò¶ÁдµÄÊý¾Ý¡£
Ë¢ÐÂÊä³ö»º³åÇø
ÒѾʹÓùý²Ù×Ý·ûendl£¬ËüÍê³É»»Ðв¢Ë¢Ð»º³åÇøµÄ¹¤×÷¡£IO¿â»¹ÓÐÁ½¸öÀàËƵIJÙ×Ý·û£ºflushºÍends¡£flushˢлº³åÇø£¬µ«²»Êä³öÈκζîÍâµÄ×Ö·û£»endsÏò»º³åÇø²åÈëÒ»¸ö¿Õ×Ö·û£¬È»ºóˢлº³åÇø¡£
unitbuf²Ù×Ý·û
unitbufÿ´ÎÊä³ö²Ù×÷ºó¶¼Ë¢Ð»º³åÇø¡£Ëü¸æËßÁ÷ÔÚ½ÓÏÂÀ´µÄÿ´Îд²Ù×÷Ö®ºó¶¼½øÐÐÒ»´Îflush¡£¶ønounitbuf²Ù×Ý·ûÔòÖØÖÃÁ÷£¬Ê¹Æä»Ö¸´Õý³£µÄϵͳ¹ÜÀíµÄ»º³åÇøˢлúÖÆ¡£
cout<<unitbuf; //ËùÓÐÊä³ö²Ù×÷ºó¶¼»áÁ¢¼´Ë¢Ð»º³åÇø cout<<nounitbuf£» //»Øµ½Õý³£µÄ»º³å·½Ê½
2¡¢ÎļþÊäÈëÊä³ö
Í·Îļþfstream¶¨ÒåÁËÈýÖÖÀàÐÍÀ´Ö§³ÖÎļþIO£ºifstream¡¢ofstream¡¢fstream¡£
fstream³ýÁ˼̳Ð×ÔiostreamÀàÐ͵ÄÐÐΪ֮Í⣬fstream»¹¶¨ÒåÁËһЩ×Ô¼º×¨ÓеIJÙ×÷¡£ÕâÊÇÆäËûIOÀàÐÍËùûÓеġ£
2.1 ʹÓÃÎļþÁ÷¶ÔÏó
ÓÃfstream´úÌæiostream&
ÔÚʹÓÃiostreamµÄµØ·½£¬¿ÉÒÔÓÃfstream´úÌæµ÷Óã¬ÒòΪfstreamÊÇ´Óiostream¼Ì³ÐÀ´µÄ¡£
³ÉÔ±º¯ÊýopenºÍclose
¿ÉÒÔÏȶ¨ÒåÒ»¸ö¿ÕÎļþÁ÷¶ÔÏó£¬È»ºóÓÃopen½«ÆäÓëÎļþ¹ØÁªÆðÀ´¡£
Èç¹ûµ÷ÓÃopenʧ°Ü£¬Ôòfailbit»á±»ÖÃ룬Òò´Ë½øÐÐopenÊÇ·ñ³É¹¦µÄ¼ì²âÊÇÒ»¸öºÃµÄÏ°¹ß¡£
ofstream out; out.open("1.txt"); if(out) .......
½«ÎļþÁ÷¶ÔÏó°ó¶¨µ½ÁíÒ»¸öÎļþ֮ǰ£¬ÐèÒªÏÈcloseÒѾ¹ØÁªµÄÎļþ£¬È»ºóÔÙ¹ØÁªÐµÄÎļþ¡£
×Ô¶¯¹¹ÔìºÍÎö¹¹
µ±Ò»¸öfstream¶ÔÏó±»Ïú»Ùʱ£¬close»á×Ô¶¯±»µ÷Óá£
2.2 Îļþģʽ
ÿ¸öÁ÷¶¼ÓÐÒ»¸ö¹ØÁªµÄÎļþģʽ£¬ÓÃÀ´Ö¸³öÈçºÎʹÓÃÎļþ¡£
Ö¸¶¨ÎļþģʽÓÐÈçÏÂÏÞÖÆ
- Ö»¿ÉÒÔ¶ÔofstreamºÍfstream¶ÔÏóÉ趨outģʽ
- Ö»¿ÉÒÔ¶ÔifstreamºÍfstream¶ÔÏóÉ趨inģʽ
- Ö»Óе±outÒ²±»É趨ʱ²Å¿ÉÉ趨truncģʽ
- Ö»Òªtruncû±»É趨£¬¾Í¿ÉÒÔÉ趨appģʽ¡£ÔÚappģʽÏ£¬¼´Ê¹Ã»ÓÐÏÔʽָ¶¨outģʽ£¬ÎļþÒ²×ÜÊÇÒÔÊä³ö·½Ê½±»´ò¿ª
- ateºÍbinaryģʽ¿ÉÓÃÓÚÈκÎÀàÐ͵ÄÎļþÁ÷¶ÔÏó£¬ÇÒ¿ÉÒÔÓëÆäËûÈκÎÎļþģʽ×éºÏʹÓÃ
ÿ¸öÎļþÁ÷ÀàÐͶ¼¶¨ÒåÁËÒ»¸öĬÈϵÄÎļþģʽ£¬ifstreamÊÇin£¬ofstreamÊÇout£¬fstreamÊÇinºÍout
ÒÔoutģʽ´ò¿ªÎļþ»á¶ªÆúÒÑÓÐÊý¾Ý
ĬÈÏÇé¿öÏ£¬´ò¿ªÒ»¸öofstreamʱ£¬ÎļþµÄÄÚÈݻᱻ¶ªÆú¡£×èÖ¹Ò»¸öofstreamÇå¿Õ¸ø¶¨ÎļþÄÚÈݵķ½·¨ÊÇͬʱָ¶¨appģʽ¡£
ofstream app("file",ofstream::app); ofstream app2("file",ofstream::out | ofstream::app);¡¡
ÿ´Îµ÷ÓÃopenʱ¶¼»áÈ·¶¨Îļþģʽ
ofstream out; out.open("file1"); //ģʽÒþº¬ÉèÖÃΪÊä³öºÍ½Ø¶Ï out.close(); out.open("file2",ofstream::app); //ģʽΪÊä³öºÍ×·¼Ó out.close();
ͨ³£Çé¿öÏ£¬outģʽÒâζ×ÅͬʱʹÓÃtruncģʽ£¬Òò´Ë£¬file1µÄÎļþµÄÄÚ¶¯½«±»Çå¿Õ¡£
ÿ´Î´ò¿ªÎļþʱ£¬¶¼ÒªÉèÖÃÎļþģʽ£¬¿ÉÄÜÊÇÏÔʽµØÉèÖã¬Ò²¿ÉÄÜÊÇÒþʽµØÉèÖ㬵±³ÌÐòδָ¶¨Ä£Ê½Ê±£¬¾ÍʹÓÃĬÈÏÖµ¡£
3¡¢stringÁ÷
sstreamÍ·Îļþ¶¨ÒåÁËÈý¸öÀàÐÍÀ´Ö§³ÖÄÚ´æIO£¬ÕâЩÀàÐÍ¿ÉÒÔÏòstring¶ÁдÊý¾Ý£¬stringÏñÊÇÒ»¸öIOÁ÷Ò»Ñù¡£
- istringstream
- ostringstream
- stringstream
ÉÏÊöÈýÖÖ¶¼¼Ì³Ð×Ôiostream¡£Í¬ÑùµÄ£¬³ýÁ˼̳ÐÀ´µÄ²Ù×÷£¬ÆäÒ²¶¨ÒåÁË×Ô¼ºÌØÓеIJÙ×÷¡£
±êÇ©£º
°æȨÉêÃ÷£º±¾Õ¾ÎÄÕ²¿·Ö×ÔÍøÂ磬ÈçÓÐÇÖȨ£¬ÇëÁªÏµ£ºwest999com@outlook.com
Ìرð×¢Ò⣺±¾Õ¾ËùÓÐתÔØÎÄÕÂÑÔÂÛ²»´ú±í±¾Õ¾¹Ûµã£¬±¾Õ¾ËùÌṩµÄÉãÓ°ÕÕƬ£¬²å»£¬Éè¼Æ×÷Æ·£¬ÈçÐèʹÓã¬ÇëÓëÔ×÷ÕßÁªÏµ£¬°æȨ¹éÔ×÷ÕßËùÓÐ
ÉÏһƪ£º¡¾Ä£°å¡¿ Ï߶ÎÊ÷
- C++ ת»»º¯Êý´îÅäÓÑÔªº¯Êý 2020-06-10
- C++ ×Ô¶¯×ª»»ºÍÇ¿ÖÆÀàÐÍת»»£¨Óû§×Ô¶¨ÒåÀàÀàÐÍ£© 2020-06-10
- C++ randº¯Êý 2020-06-10
- C++ ÓÑÔªº¯Êý 2020-06-10
- C++ ÔËËã·ûÖØÔØ 2020-06-10
IDC×ÊѶ£º Ö÷»ú×ÊѶ ×¢²á×ÊѶ ÍйÜ×ÊѶ vps×ÊѶ ÍøÕ¾½¨Éè
ÍøÕ¾ÔËÓª£º ½¨Õ¾¾Ñé ²ß»®Ó¯Àû ËÑË÷ÓÅ»¯ ÍøÕ¾Íƹã Ãâ·Ñ×ÊÔ´
ÍøÕ¾ÁªÃË£º ÁªÃËÐÂÎÅ ÁªÃ˽éÉÜ ÁªÃ˵ãÆÀ Íø׬¼¼ÇÉ
ÐÐÒµ×ÊѶ£º ËÑË÷ÒýÇæ ÍøÂçÓÎÏ· µç×ÓÉÌÎñ ¹ã¸æ´«Ã½
ÍøÂç±à³Ì£º Asp.Net±à³Ì Asp±à³Ì Php±à³Ì Xml±à³Ì Access Mssql Mysql ÆäËü
·þÎñÆ÷¼¼Êõ£º Web·þÎñÆ÷ Ftp·þÎñÆ÷ Mail·þÎñÆ÷ Dns·þÎñÆ÷ °²È«·À»¤
Èí¼þ¼¼ÇÉ£º ÆäËüÈí¼þ Word Excel Powerpoint Ghost Vista QQ¿Õ¼ä QQ FlashGet ѸÀ×
ÍøÒ³ÖÆ×÷£º FrontPages Dreamweaver Javascript css photoshop fireworks Flash
³ÌÐòÉè¼Æ£º Java¼¼Êõ C/C++ VB delphi
- Photoshop»æÖÆÁ¢Ìå·ç¸ñµÄ΢Ц±íÇé
- PSÎÄ×ÖÌØЧ½Ì³Ì£ºÖÆ×÷¹«Â·ÉϸöÐÔµÄÍ¿Ñ»
- PhotoshopÉè¼Æ¾í±ßЧ¹ûµÄ±äÐνð¸ÕµçÓ°
- PSÉ«²ÊÐÞ¸´½Ì³Ì£ºÀûÓÃÉ«½×¹¤¾ß¿ìËÙ¸øºì
- PS°ë͸Ã÷ÎïÌå¿Ùͼ£ºÀûÓÃͨµÀÑ¡Çø¹¤¾ß¿Ù
- PSº£±¨Éè¼Æ¼¼Çɽ̳̣ºÑ§Ï°ÖÆ×÷¸öÐԵč
- PSͼƬÌØЧÖÆ×÷½Ì³Ì£ºÑ§Ï°¸øÌúËþͼƬÖÆ
- ѧϰÓÃphotoshop°Ñ»ë×ǵĺ£Ë®ÕÕƬºóÆÚ
- PS¹Å·çÕÕƬ½Ì³Ì£º¸ø¹Å·çÃÀÅ®´òÔì³öÅ®ÏÀ
- PS¸öÐÔÈËÎﺣ±¨ÖÆ×÷£ºÉè¼Æ´´ÒâʱÉеIJ£
- Ôõô͵ÉϱðÈ˵Ä΢ÐÅ ×î¼òµ¥ÍµÎ¢ÐÅÃÜÂë
- ͼ-µÚ7ÕÂ-¡¶Êý¾Ý½á¹¹Ì⼯¡·Ï°Ìâ½âÎö-ÑÏ
- ·¬ÇÑÖúÊÖ ×îРVisual Assist X ÊÊÓ¦ÓÚ
- »ùÓÚQCustomPlot»æͼ£¬Êó±ê¸úË涯̬ÏÔ
- C++ÖÐdoubleÀàÐ͵ÄÊý×ÖÈçºÎ±£ÁôÈýλС
- Öйú´óѧMOOC-³ÂÔ½¡¢ºÎÇÕÃú-Êý¾Ý½á¹¹-2
- °¢À²®Êý×Öµ½ÖÐÎÄ´óдÊý×ÖµÄת»»
- base64×Ö·û´®×ªÎļþ£¬ÒÔ¼°ngImgCrop²Ã
- cÓïÑÔÐÄÐθæ°×´úÂëʵÏÖ
- ±à³ÌÓïÑÔ¡ª¡ªC----ϸ½Ú