• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            The Way of C++

              C++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              55 Posts :: 0 Stories :: 19 Comments :: 0 Trackbacks

            公告

            The first time i use this blog, i will write something that i learn which i think is worth write down.

            常用鏈接

            留言簿(3)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

                Forgive my ugly English description ,and i may it will help you. -_-
                (Reference by : http://www.cplusplus.com/reference/iostream/istream/getline.html)
                the function prototype is as follows:
                istream& getline (char* s, streamsize n );
                istream& getline (char* s, streamsize n, char delim );
                
                the function extracts charatcters from the input sequence and stores them as c-tring into the array begin at s. Characters are extracted until either n-1 characters have been extracted or the delimiting character is found.( the char delim can be specified by yourself, or be '\n' by default). Remember this, when the delimiter is found ,it will be extracted and discarded, i.e, it is extracted from the input stream and not stored to the s , and the next input operation will begin after this extracted delimiter. ). After the data extracted ,the ending null charactor ('\0') will be appended to s automatically.
                The following is same examples.
                
             1 #include<iostream>
             2 using namespace std;
             3 int main()
             4 {
             5     char fruit[100];
             6     cin.getline(fruit,100,'*');
             7     cout<<fruit<<endl;
             8     
             9     system("pause");
            10 }
            11 
                
                now ,when you input "banana apple pear\n"( here \n means the enter), then the console will show "banana apple pear"
               
               also ,you can specify the delimiter by yourself  as this.
              
             1 #include<iostream>
             2 using namespace std;
             3 int main()
             4 {
             5     char fruit[100];
             6     cin.getline(fruit,100,'*');
             7     cout<<fruit<<endl;
             8     
             9     system("pause");
            10 }
            11 
                now,when you input "banana apple pear * orange\n",then the console will show " banana apple pera" , here the orange is not readed because is is after the delimiter '*'.
                
                So simple about use this function ,right ? now see this example .
                
             1 #include<iostream>
             2 using namespace std;
             3 int main()
             4 {
             5     char fruit[100];
             6     while(cin.getline(fruit,100,'*'))
             7     {
             8         cout<<fruit<<endl;
             9     }
            10     
            11     system("pause");
            12 }
            13 
              Here we want to input number of lines ,each of line is delimited by a '*'.
              In the first time : we input " banana apple *\n" then the console show "banana apple" ,this is right.
              But the problem comes, in the second input " pear orange *\n", now the console show "\n(\n means a blank line) pear orange". Here a blank line will be output, betray to our will that showing "pear orange";
               The problem is that a '\n' is stored in the fruit in the second time as the first element, i.e, the fruit is "\npear orange\0" in the second input. In the first getline ,it extracted the input stream until the first '*' was found, then it discarded the '*', but the following character is '\n' which you input in the first time, this '\n' is still in the input stream. And the second getline read the stream form this '\n'. As we know ,the delimiter is specified as '*' ,so the '\n' is not a delimiter here ,and it will be stored into the second fruit. 
               How to resovle this problem ? In order to avoid the situation above ,we can use the get to extract the '\n' character. And now it work normally.
              
             1 #include<iostream>
             2 using namespace std;
             3 int main()
             4 {
             5     char fruit[100];
             6     while(cin.getline(fruit,100,'*'))
             7     {
             8         cin.get();
             9         cout<<fruit<<endl;
            10     }
            11     
            12     system("pause");
            13 }
            14 
               
               One thing you have to remember is that this is a function of the cin.
               there is also a global function getline() in the heder <string>. But you must use string object as parameter instead of c-string.  See the following example. 
               (Reference to :http://www.cplusplus.com/reference/string/getline.html)
              
             1 #include<iostream>
             2 #include<string>
             3 using namespace std;
             4 int main()
             5 {
             6     string s;
             7     getline(cin,s,'*');
             8     cout<<s<<endl;
             9     system("pause");
            10 }
            11 
                As you can see, the first parameter is istream object on which the extraction operation is performed. The second parameter str is the string object where the extracted content is stored. The Third parameter delim is the delimiting character. The operation of extracting succesive characters is stopped when this delimeter is read. And it will be etracted and discarded. 
               
               
              
              


               
                
                
              
            posted on 2007-12-26 17:55 koson 閱讀(1062) 評(píng)論(3)  編輯 收藏 引用 所屬分類: C/C++

            Feedback

            # re: About the use of getline() 2007-12-27 12:21 夢(mèng)在天涯
            good , where are you in ?
            your english is good!
              回復(fù)  更多評(píng)論
              

            # re: About the use of getline()[未登錄](méi) 2007-12-27 12:28 Koson
            thx, i'm in guangzhou. Actually, i write this using the reference of the website. But i think this may be a way to improve the written english , despite the content is as simple as that. -_-  回復(fù)  更多評(píng)論
              

            # re: About the use of getline()[未登錄](méi) 2007-12-27 14:00 jarod
            ..... where are u in ?..................

            should be

            where are u..........  回復(fù)  更多評(píng)論
              

            国产精品无码久久综合| 精品国产婷婷久久久| 精品国产综合区久久久久久| 色综合久久综合中文综合网| 久久久国产打桩机| 亚洲天堂久久久| 中文字幕无码久久久| 性高湖久久久久久久久AAAAA| 久久久久黑人强伦姧人妻| 欧美日韩中文字幕久久伊人| 91精品国产91热久久久久福利| 久久99国产亚洲高清观看首页| 久久99国产精品二区不卡| 久久久久久综合一区中文字幕 | 97香蕉久久夜色精品国产| 亚洲国产精品综合久久一线 | 狠狠色噜噜色狠狠狠综合久久| 97视频久久久| 久久99国产综合精品女同| 国产精品视频久久久| 国内精品久久久久久中文字幕| 天天影视色香欲综合久久| 中文字幕久久精品无码| 国产精品久久国产精麻豆99网站| 999久久久国产精品| 久久精品视频一| 2021久久国自产拍精品| 久久久无码精品亚洲日韩软件| 亚洲精品蜜桃久久久久久| 91久久精品无码一区二区毛片| 久久亚洲sm情趣捆绑调教| 久久不射电影网| 国产欧美久久久精品影院| 中文字幕久久欲求不满| 2020国产成人久久精品| 国产精品久久久久影视不卡| 香蕉99久久国产综合精品宅男自 | 狼狼综合久久久久综合网| 久久久久久久综合日本| 久久精品国产亚洲77777| 亚洲精品99久久久久中文字幕|