欢迎来到天天文库
浏览记录
ID:37710429
大小:32.14 KB
页数:5页
时间:2019-05-29
《getline用法》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、GetLine目录getline功能getline参数getline说明编辑本段getline功能 getline是string类对象的成员函数,属于stringIO操作。用于读取整行文本编辑本段getline参数 getline(cin,str); cin:输入流 str:string对象编辑本段getline说明 getline函数从输入流的下一行读取,并保存读取的内容到str中,但不包括换行符。getline函数将istream参数作为返回值,和输入操作符一样也把它用作判断条件。 范例: #include #include2、> usingnamespacestd; intmain() { stringline; while(getline(cin,line)) cout<3、字符到c中。注意读入的字符数应比实际的大1,因为读入的是字符串,字符串将会以' '作为结束,如果你要读入3个字符,那么i的值应该为4。注意getline会读取并丢弃分界符。后面的自已搞定,创建一个文件流类对象,然后用这个文件流来调用getline函数,比如ifstreamhy1(“hyong1.txt”)//创建hy1流,并打开文件以便读取内容。charc[333];hy1.getline(c,3,'0');//表示,把hy1流关联的hyong1中3个字符或者遇到'0'的字符数读入到c中。假设有一个叫data.txt的文件,它包含以下内容:Fry:OneJilliondolla4、rs.[Everyonegasps.]Auctioneer:Sir,that'snotanumber.数据读取,测试。以下就是基于data.txt的数据读取操作:#includeinclude#includeusingnamespacestd;//输出空行voidOutPutAnEmptyLine(){ cout<<"";}//读取方式:逐词读取,词之间用空格区分//readdatafromthefile,WordByWord//whenusedinthismanner,we'llgetspace-delimited5、bitsoftextfromthefile//butallofthewhitespacethatseparatedwords(includingnewlines)waslost.voidReadDataFromFileWBW(){ ifstreamfin("data.txt"); strings; while(fin>>s) { cout<<"Readfromfile:"<6、ngwhitespace,//wecouldreadthefileinLine-By-LineusingtheI/Ogetline()function.voidReadDataFromFileLBLIntoCharArray(){ ifstreamfin("data.txt"); constintLINE_LENGTH=100; charstr[LINE_LENGTH]; while(fin.getline(str,LINE_LENGTH)) { cout<<"Readfromfile:"<7、//读取方式:逐行读取,将行读入字符串,行之间用回车换行区分//Ifyouwanttoavoidreadingintocharacterarrays,//youcanusetheC++stringgetline()functiontoreadlinesintostringsvoidReadDataFromFileLBLIntoString(){ ifstreamfin("data.txt"); strings; while(getline(fin,s))
2、> usingnamespacestd; intmain() { stringline; while(getline(cin,line)) cout<3、字符到c中。注意读入的字符数应比实际的大1,因为读入的是字符串,字符串将会以' '作为结束,如果你要读入3个字符,那么i的值应该为4。注意getline会读取并丢弃分界符。后面的自已搞定,创建一个文件流类对象,然后用这个文件流来调用getline函数,比如ifstreamhy1(“hyong1.txt”)//创建hy1流,并打开文件以便读取内容。charc[333];hy1.getline(c,3,'0');//表示,把hy1流关联的hyong1中3个字符或者遇到'0'的字符数读入到c中。假设有一个叫data.txt的文件,它包含以下内容:Fry:OneJilliondolla4、rs.[Everyonegasps.]Auctioneer:Sir,that'snotanumber.数据读取,测试。以下就是基于data.txt的数据读取操作:#includeinclude#includeusingnamespacestd;//输出空行voidOutPutAnEmptyLine(){ cout<<"";}//读取方式:逐词读取,词之间用空格区分//readdatafromthefile,WordByWord//whenusedinthismanner,we'llgetspace-delimited5、bitsoftextfromthefile//butallofthewhitespacethatseparatedwords(includingnewlines)waslost.voidReadDataFromFileWBW(){ ifstreamfin("data.txt"); strings; while(fin>>s) { cout<<"Readfromfile:"<6、ngwhitespace,//wecouldreadthefileinLine-By-LineusingtheI/Ogetline()function.voidReadDataFromFileLBLIntoCharArray(){ ifstreamfin("data.txt"); constintLINE_LENGTH=100; charstr[LINE_LENGTH]; while(fin.getline(str,LINE_LENGTH)) { cout<<"Readfromfile:"<7、//读取方式:逐行读取,将行读入字符串,行之间用回车换行区分//Ifyouwanttoavoidreadingintocharacterarrays,//youcanusetheC++stringgetline()functiontoreadlinesintostringsvoidReadDataFromFileLBLIntoString(){ ifstreamfin("data.txt"); strings; while(getline(fin,s))
3、字符到c中。注意读入的字符数应比实际的大1,因为读入的是字符串,字符串将会以' '作为结束,如果你要读入3个字符,那么i的值应该为4。注意getline会读取并丢弃分界符。后面的自已搞定,创建一个文件流类对象,然后用这个文件流来调用getline函数,比如ifstreamhy1(“hyong1.txt”)//创建hy1流,并打开文件以便读取内容。charc[333];hy1.getline(c,3,'0');//表示,把hy1流关联的hyong1中3个字符或者遇到'0'的字符数读入到c中。假设有一个叫data.txt的文件,它包含以下内容:Fry:OneJilliondolla
4、rs.[Everyonegasps.]Auctioneer:Sir,that'snotanumber.数据读取,测试。以下就是基于data.txt的数据读取操作:#includeinclude#includeusingnamespacestd;//输出空行voidOutPutAnEmptyLine(){ cout<<"";}//读取方式:逐词读取,词之间用空格区分//readdatafromthefile,WordByWord//whenusedinthismanner,we'llgetspace-delimited
5、bitsoftextfromthefile//butallofthewhitespacethatseparatedwords(includingnewlines)waslost.voidReadDataFromFileWBW(){ ifstreamfin("data.txt"); strings; while(fin>>s) { cout<<"Readfromfile:"<
6、ngwhitespace,//wecouldreadthefileinLine-By-LineusingtheI/Ogetline()function.voidReadDataFromFileLBLIntoCharArray(){ ifstreamfin("data.txt"); constintLINE_LENGTH=100; charstr[LINE_LENGTH]; while(fin.getline(str,LINE_LENGTH)) { cout<<"Readfromfile:"<7、//读取方式:逐行读取,将行读入字符串,行之间用回车换行区分//Ifyouwanttoavoidreadingintocharacterarrays,//youcanusetheC++stringgetline()functiontoreadlinesintostringsvoidReadDataFromFileLBLIntoString(){ ifstreamfin("data.txt"); strings; while(getline(fin,s))
7、//读取方式:逐行读取,将行读入字符串,行之间用回车换行区分//Ifyouwanttoavoidreadingintocharacterarrays,//youcanusetheC++stringgetline()functiontoreadlinesintostringsvoidReadDataFromFileLBLIntoString(){ ifstreamfin("data.txt"); strings; while(getline(fin,s))
此文档下载收益归作者所有