C/C++编程:字符流输入流std :: istream
std :: istream
typedef basic_istream<char> istream;
Input stream
输入流对象可以读取和解释字符序列的输入
这是一个basic_istream实例 具有以下模板参数:
模板参数 定义 评论 charT char
成员char_type的别名 traits char_traits<char>
成员traits_type的别名
std :: istream::istream
initialization (1)
explicit istream (streambuf* sb);
copy (2)
istream& (const istream&) = delete;
move (3)
protected: istream& (istream&& x);
作用:构造对象
// istream constructor
#include <iostream> // std::ios, std::istream, std::cout
#include <fstream> // std::filebuf
int main () {
std::filebuf fb;
if (fb.open ("test.txt",std::ios::in))
{
std::istream is(&fb);
while (is)
std::cout << char(is.get());
fb.close();
}
return 0;
}
" class="reference-link">
std :: istream::operator>>
arithmetic types (1)
istream& operator>> (bool& val);
istream& operator>> (short& val);
istream& operator>> (unsigned short& val);
istream& operator>> (int& val);
istream& operator>> (unsigned int& val);
istream& operator>> (long& val);
istream& operator>> (unsigned long& val);
istream& operator>> (long long& val);
istream& operator>> (unsigned long long& val);
istream& operator>> (float& val);
istream& operator>> (double& val);
istream& operator>> (long double& val);
istream& operator>> (void& val);stream buffers (2)
istream& operator>> (streambuf sb );
manipulators (3)
istream& operator>> (istream& (pf)(istream&));
istream& operator>> (ios& (pf)(ios&));
istream& operator>> (ios_base& (*pf)(ios_base&));作用:提取格式化的输入
#include <iostream> // std::cin, std::cout, std::he
int main () {
int n;
std::cout << "Enter a number: ";
std::cin >> n;
std::cout << "You have entered: " << n << '\n';
std::cout << "Enter a hexadecimal number: ";
std::cin >> std::hex >> n;
std::cout << "Its decimal equivalent is: " << n << '\n';
return 0;
}
std :: istream::gcount
streamsize gcount()const;
作用:获取字符数
#include <iostream> // std::cin, std::cout
int main () {
char str[20];
std::cout << "Please, enter a word: ";
std::cin.getline(str, 20);
std::cout << std::cin.gcount() << " characters read: " << str << "\n";
return 0;
}
std :: istream::get
single character (1)
int get();
istream& get (char& c);c-string (2)
istream& get (char s, streamsize n);
istream& get (char s, streamsize n, char delim);stream buffer (3)
istream& get (streambuf& sb);
istream& get (streambuf& sb, char delim);作用:从流中提取字符,作为未格式化的输入
#include <iostream> // std::cin, std::cout
#include <fstream> // std::ifstream
int main () {
char str[256];
std::cout << "输入现有文本文件的名称: ";
std::cin.get (str,256); // get c-string
std::ifstream is(str);
char c;
while (is.get(c)){
std::cout << c;
}
is.close();
return 0;
}
std :: istream::getline
istream&getline(char * s,streamsize n);
istream&getline(char * s,streamsize n,char delim);
作用:从标准输入流中获取行
从流中提取字符作为未格式化的输入,并将它们作为c字符串存储到s中,直到提取的字符是定界字符,或者将n个字符写入s(包括终止的空字符)为止
#include <iostream> // std::cin, std::cout
#include <fstream> // std::ifstream
int main () {
char name[256], title[256];
std::cout << "Please, enter your name: ";
std::cin.getline(name, 256);
std::cout << "Please, enter your favourite movie: ";
std::cin.getline(title, 256);
std::cout << name << "'s favourite movie is " << title;
return 0;
}
std :: istream::ignore
istream& ignore (streamsize n = 1, int delim = EOF);
作用:提取并丢弃字符
从输入序列中提取字符并丢弃它们,直到提取出n个字符或一个等于delim的比较为止。
// istream::ignore example
#include <iostream> // std::cin, std::cout
int main () {
char first, last;
std::cout << "Please, enter your first name followed by your surname: ";
first = std::cin.get(); // get one character
std::cin.ignore(256,' '); // ignore until space
last = std::cin.get(); // get one character
std::cout << "Your initials are " << first << last << '\n';
return 0;
}
std :: istream::peek
int peek();
作用:偷看下一个字符
返回输入序列中的下一个字符,而不提取该字符:保留该字符作为要从流中提取的下一个字符。
// istream::peek example
#include <iostream> // std::cin, std::cout
#include <string> // std::string
#include <cctype> // std::isdigit
int main () {
std::cout << "Please, enter a number or a word: ";
std::cout.flush(); // ensure output is written
std::cin >> std::ws; // eat up any leading white spaces
int c = std::cin.peek(); // peek character
if ( c == EOF ) return 1;
if ( std::isdigit(c) )
{
int n;
std::cin >> n;
std::cout << "You entered the number: " << n << '\n';
}
else
{
std::string str;
std::cin >> str;
std::cout << "You entered the word: " << str << '\n';
}
return 0;
}
std :: istream::read、std :: istream :: tellg、std :: istream :: seekg
istream&read(char * s,streamsize n);
作用:读取数据块
从流中 提取n个字符并将其存储在s指向的数组中。
streampos tellg();
作用:返回当前字符在输入流中的位置。
istream& seekg (streampos pos);
istream& seekg (streamoff off, ios_base::seekdir way);
作用:设置要从输入流中提取的下一个字符的位置
// read a file into memory
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main () {
std::ifstream is ("test.txt", std::ifstream::binary);
if (is) {
// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
std::cout << "Reading " << length << " characters... ";
// read data as a block:
is.read (buffer,length);
if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
// ...buffer contains the entire file...
delete[] buffer;
}
return 0;
}
std :: istream::putback、std:
:unget
istream& putback (char c);
istream& unget();
作用:放回字符
尝试将流中当前位置减少一个字符,使从流中提取的最后一个字符再次可用于输入操作
// istream::putback example
#include <iostream> // std::cin, std::cout
#include <string> // std::string
int main () {
std::cout << "Please, enter a number or a word: ";
char c = std::cin.get();
if ( (c >= '0') && (c <= '9') )
{
int n;
std::cin.putback (c);
// std::cin.unget();
std::cin >> n;
std::cout << "You entered a number: " << n << '\n';
}
else
{
std::string str;
std::cin.putback (c);
//std::cin.unget();
getline (std::cin,str);
std::cout << "You entered a word: " << str << '\n';
}
return 0;
}
std :: istream :: sync
int sync();
作用:同步输入缓冲区
还没有评论,来说两句吧...