博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
比较:I/O成员函数getline() 与 get()(第二种用法)的用法异同
阅读量:4549 次
发布时间:2019-06-08

本文共 2064 字,大约阅读时间需要 6 分钟。

 

首次,定义三个字符数组;

char a[20];

char b[20];

char c[20];

 

其次,建立一个文件——"Test 2.txt";

其中的字符内容:ffffff,ssssssssss;ggggggg,eeeeeee(ps:就一行)

 

再次,对输入进行文件类定向;

ifstream fin("Test 2.txt");

 

最后,进行对比研究;

 

表格 1

 

代码A:

fin.get(a, 20, ',');

cout<<a<<endl;
fin.get(a, 20, ',');    
cout<<a<<endl;
fin.get(a, 20, ',');
cout<<a<<endl;

输出:

fffff

 

 

Press any key to continue

(此后所有均是完整的输出)。

代码B:

fin.get(a, 20, ',');

cout<<a<<endl;
fin.get(a, 20, ';');    
cout<<a<<endl;
fin.get(a, 20, ',');
cout<<a<<endl;

输出:

ffffff

,ssssssssss

;ggggggg

Press any key to continue

代码C:

fin.get(a, 20, ',');

cout<<a<<endl;
fin.get(b, 20, ',');    
cout<<b<<endl;
fin.get(c, 20, ',');
cout<<c<<endl;

输出:

ffffff

 

 

Press any key to continue

代码D:

fin.get(a, 20, ',');

cout<<a<<endl;
fin.get(b, 20, ';');      
cout<<b<<endl;
fin.get(c, 20, ',');
cout<<c<<endl;

输出:

ffffff

,ssssssssss

;ggggggg

Press any key to continue

代码E:

fin.getline(a, 20, ',');

cout<<a<<endl;
fin.getline(a, 20, ',');    
cout<<a<<endl;
fin.getline(a, 20, ',');
cout<<a<<endl;

输出:

ffffff

ssssssssss;ggggggg

eeeeeee

Press any key to continue

代码F:

fin.getline(a, 20, ',');

cout<<a<<endl;
fin.getline(a, 20, ';');
cout<<a<<endl;
fin.getline(a, 20, ',');
cout<<a<<endl;

输出:

ffffff

ssssssssss

ggggggg

Press any key to continue

代码G:

fin.getline(a, 20, ',');

cout<<a<<endl;
fin.getline(b, 20, ',');
cout<<b<<endl;
fin.getline(c, 20, ',');
cout<<c<<endl;

输出:

ffffff

ssssssssss;ggggggg

eeeeeee

Press any key to continue

代码H:

fin.getline(a, 20, ',');

cout<<a<<endl;
fin.getline(b, 20, ';');
cout<<b<<endl;
fin.getline(c, 20, ',');
cout<<c<<endl;

输出:

ffffff

ssssssssss

ggggggg

Press any key to continue

 

 观察表格 1可得:

  1. 无论get()函数还是getline()函数,它们输入的结果与前后数组(接受字符的容器)是否一致无关;

    对比表格中(colum1, colum3),以及(colum2, colum4);

 

  2.  对于get()函数,连续三次输入,如果某一个输入被一个分隔符(如, ',') “绊住,那么后面按照相同分隔符的输入也被绊住,输出空值;

    对比表格中第一行(colum1, colum2, colum3);

 

  3.  对于get()函数,前后两个输入的分隔符不相同,后一个会继承前一个的分隔符;

    观察表格中第一行的(colum2 & colum4);

 

  4.  对于getline()函数,不会继承分隔符号;

    观察第二列的两行或者第四列的两行;

 

  5.  对于getline()函数, 不会出现被相同分隔符绊住的情况;

    观察第一列的上下两行或者第三列的上下两行;

转载于:https://www.cnblogs.com/richard-c-java/p/3292142.html

你可能感兴趣的文章
Linux:Gentoo系统的安装笔记(三)
查看>>
lumen可以使用laravel-ide-helper
查看>>
重装Windows系统后,Linux系统启动引导失败
查看>>
例2-2
查看>>
day04_12/14/2016_bean属性的设置之自动注入
查看>>
python-基础day5
查看>>
在sublime text 3中安装中文支持
查看>>
js 三目运算
查看>>
重载和重写在jvm运行中的区别(一)
查看>>
nginx配置详解
查看>>
eclipse-luna 安装svn插件(转)
查看>>
从零开始的H5生活
查看>>
设计模式 序
查看>>
Sublime Text2 快捷键汇总
查看>>
结对项目之四则运算
查看>>
Greendao
查看>>
java正则表达式
查看>>
(一)Redis 基础 - 安装
查看>>
codeforces 336 Div.2 B. Hamming Distance Sum
查看>>
201421123042 《Java程序设计》第12周
查看>>