C++ϵͳѧϰ֮°Ë£ºIO¿â

2018-08-26 17:16:26À´Ô´£º²©¿ÍÔ° ÔĶÁ ()

ÐÂÀÏ¿Í»§´ó»ØÀ¡,ÔÆ·þÎñÆ÷µÍÖÁ5ÕÛ

¡¡¡¡ÐµÄ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
Ìرð×¢Ò⣺±¾Õ¾ËùÓÐתÔØÎÄÕÂÑÔÂÛ²»´ú±í±¾Õ¾¹Ûµã£¬±¾Õ¾ËùÌṩµÄÉãÓ°ÕÕƬ£¬²å»­£¬Éè¼Æ×÷Æ·£¬ÈçÐèʹÓã¬ÇëÓëÔ­×÷ÕßÁªÏµ£¬°æȨ¹éÔ­×÷ÕßËùÓÐ

ÉÏһƪ£º¡¾Ä£°å¡¿ Ï߶ÎÊ÷

ÏÂһƪ£ºPAT (Basic Level) Practice 1006 »»¸ö¸ñʽÊä³öÕûÊý