首次,定义三个字符数组;
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可得:
- 无论get()函数还是getline()函数,它们输入的结果与前后数组(接受字符的容器)是否一致无关;
对比表格中(colum1, colum3),以及(colum2, colum4);
2. 对于get()函数,连续三次输入,如果某一个输入被一个分隔符(如, ',') “绊住,那么后面按照相同分隔符的输入也被绊住,输出空值;
对比表格中第一行(colum1, colum2, colum3);
3. 对于get()函数,前后两个输入的分隔符不相同,后一个会继承前一个的分隔符;
观察表格中第一行的(colum2 & colum4);
4. 对于getline()函数,不会继承分隔符号;
观察第二列的两行或者第四列的两行;
5. 对于getline()函数, 不会出现被相同分隔符绊住的情况;
观察第一列的上下两行或者第三列的上下两行;