cin 用法详解:https://blog.csdn.net/bravedence/article/details/77282039
cin.ignore 详解:https://blog.csdn.net/imkelt/article/details/52202002
EOF for cin: https://blog.csdn.net/foxpeter/article/details/6072130
cin.get() and EOF: https://blog.csdn.net/qq_34686440/article/details/55005165
how to use eof(): https://blog.csdn.net/kmno400/article/details/5724577
C I/O: https://blog.csdn.net/baidu_41560343/article/details/90719093
find newline: https://bbs.csdn.net/topics/50041255
CPP StringStream: https://lavi-liu.blog.csdn.net/article/details/89070296
C/CPP string数据类型转换及分隔:https://www.cnblogs.com/loveprogramme/p/11000421.html
Cpp fstream: https://blog.csdn.net/Andy710660541/article/details/51045385; https://blog.csdn.net/xunmengpiaoyun/article/details/17054849?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&dist_request_id=1328769.40256.16175124726219943&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control
'\0' vs. "\0" https://bbs.csdn.net/topics/390615761
input processing:
- cin>>
- return the input stream buffer: istream& ==> can be checked in a conditional clause ==> false if EOF/nothing to read while trying to get cin>>a ==> !!!! not checking 1/0 of the value read
- since it returns istream& ==> we can chain it by cin>>a>>b>>.....
- use ' ' , '\t' or '\n' as delimiter ==> cin>> read words/chunks not just 1 byte/char
- “若缓冲区中第一个字符是空格、tab或换行这些分隔符时,cin>>会将其忽略并清除,继续读取下一个字符,若缓冲区为空,则继续等待。但是如果读取成功,字符后面的分隔符是残留在缓冲区的,cin>>不做处理。”
- cin.get
- cin.get() returns int ==> ascii val of the char, and EOF with the value of -1
- cin.get(dest_char/char*, num_char, delimiter char) returns istream& ==> we can chain it by cin.get(a).get(b)..... ===> read a single char or a specified chunk of size num_char before reaching delimiter char each time.
- "遇到换行符时结束读取,但是不对换行符进行处理,换行符仍然残留在输入缓冲区。"
- "cin.get(str,size);读取一行时,只能将字符串读入C风格的字符串中,即char*"
- cin.getline
- "getline读取一行字符时,默认遇到’\n’时终止,并且将’\n’直接从输入缓冲区中删除掉,不会影响下面的输入处理。"
- "getline函数可以将字符串读入C++风格的字符串中,即string类型。"
- istream& getline(char* s, streamsize count, char delim);
- string::getline
- "getline遇到结束符时,会将结束符一并读入指定的string中,再将结束符替换为空字符。" ==> similar result to cin.getline()
- istream& getline ( istream& is, string& str, char delim);
- the buffer for string::getline can grow dynamically by the power of cpp_string.
- ==> if the delimitor is not found, getline will simply copy everything.
- ==> when the istream is not valid (say only EOF), getline will not change the string at all.
- <stdio.h> gets
- "从标准输入设备读字符串,可以无限读取,不会判断上限,以回车结束或者EOF时停止读取"
- char *gets( char *buffer );
- suppressed use in CPP
other functionals
- cin.ignore(int N, char delim) ==> ignore up to N char/bytes before encountering "delim" char
- istream::ignore(char x) is not implemented ==> ignore(' ') will convert ' ' to an int and ignore that many char
- ignore() ignores 1 char
- !!! ignore (len, delim) will ignore the delim char as well !!!
- see test code
-
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string s = "1234567,67653562,25346457,324234\n"; istringstream ss; ss.str(s); ss.ignore(100, ','); getline(ss, s, ','); cout << s << endl; ss.ignore(100, ','); ss.ignore(); getline(ss,s,','); cout << s; cin.ignore(100, ','); //ignore(len, delimiter) ignores the delimiter char.!!!! cin >> s; cout << s; return 0; }
- cin.eof() ==> return the value of eofbit ==> set to 1 when encountering a "proper" EOF
examples for string::getline and istream::ignore()
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string t1 = "res";
string t2 = "res board";
istringstream ss;
string s1, s2;
s1 = s2 = "temp";
ss.str(t1);
getline(ss, s1, ' ');
cout << "string::getline(stream, string, delim) will copy everything is delim is not found.\n";
cout << s1 << endl;
getline(ss, s2);
cout << "when the stream is invalid (with only EOF), string::getline will not change the string.\n";
cout << (s2 == "") << " the string is not set to \"\"." << endl;
cout << "the string still is: \n";
cout << s2 << endl;
ss.clear();
ss.str(t2);
ss.ignore(' ');
getline(ss, s1);
cout << s1 << endl;
return 0;
}
s1 is not changed, since ss is empty