C/C++编程:字符流输入流std :: istream

╰+哭是因爲堅強的太久メ 2022-05-19 12:58 423阅读 0赞

std :: istream

  1. typedef basic_istream<char> istream;

Input stream

20210303110025392.png

输入流对象可以读取和解释字符序列的输入

这是一个basic_istream实例 具有以下模板参数:



















模板参数 定义 评论
charT char 成员char_type的别名 
traits char_traits<char> 成员traits_type的别名 

std :: istream::istream
















initialization (1)
  1. explicit istream (streambuf* sb);
copy (2)
  1. istream& (const istream&) = delete;
move (3)
  1. protected: istream& (istream&& x);

作用:构造对象

  1. // istream constructor
  2. #include <iostream> // std::ios, std::istream, std::cout
  3. #include <fstream> // std::filebuf
  4. int main () {
  5. std::filebuf fb;
  6. if (fb.open ("test.txt",std::ios::in))
  7. {
  8. std::istream is(&fb);
  9. while (is)
  10. std::cout << char(is.get());
  11. fb.close();
  12. }
  13. return 0;
  14. }

" class="reference-link">20210303110634308.png

std :: istream::operator>>
















arithmetic types (1)
  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)
  1. istream& operator>> (streambuf sb );
manipulators (3)
  1. istream& operator>> (istream& (pf)(istream&));
    istream& operator>> (ios& (
    pf)(ios&));
    istream& operator>> (ios_base& (*pf)(ios_base&));

作用:提取格式化的输入

  1. #include <iostream> // std::cin, std::cout, std::he
  2. int main () {
  3. int n;
  4. std::cout << "Enter a number: ";
  5. std::cin >> n;
  6. std::cout << "You have entered: " << n << '\n';
  7. std::cout << "Enter a hexadecimal number: ";
  8. std::cin >> std::hex >> n;
  9. std::cout << "Its decimal equivalent is: " << n << '\n';
  10. return 0;
  11. }

20210303103402801.png

std :: istream::gcount

  1. streamsize gcount()const;

作用:获取字符数

  1. #include <iostream> // std::cin, std::cout
  2. int main () {
  3. char str[20];
  4. std::cout << "Please, enter a word: ";
  5. std::cin.getline(str, 20);
  6. std::cout << std::cin.gcount() << " characters read: " << str << "\n";
  7. return 0;
  8. }

20210303103834521.png

std :: istream::get
















single character (1)
  1. int get();
    istream& get (char& c);
c-string (2)
  1. istream& get (char s, streamsize n);
    istream& get (char
    s, streamsize n, char delim);
stream buffer (3)
  1. istream& get (streambuf& sb);
    istream& get (streambuf& sb, char delim);

作用:从流中提取字符,作为未格式化的输入

  1. #include <iostream> // std::cin, std::cout
  2. #include <fstream> // std::ifstream
  3. int main () {
  4. char str[256];
  5. std::cout << "输入现有文本文件的名称: ";
  6. std::cin.get (str,256); // get c-string
  7. std::ifstream is(str);
  8. char c;
  9. while (is.get(c)){
  10. std::cout << c;
  11. }
  12. is.close();
  13. return 0;
  14. }

20210303104253117.png

std :: istream::getline

  1. istreamgetlinechar * sstreamsize n);
  2. istreamgetlinechar * sstreamsize nchar delim);

作用:从标准输入流中获取行

从流中提取字符作为未格式化的输入,并将它们作为c字符串存储到s中,直到提取的字符是定界字符,或者将n个字符写入s(包括终止的空字符)为止

  1. #include <iostream> // std::cin, std::cout
  2. #include <fstream> // std::ifstream
  3. int main () {
  4. char name[256], title[256];
  5. std::cout << "Please, enter your name: ";
  6. std::cin.getline(name, 256);
  7. std::cout << "Please, enter your favourite movie: ";
  8. std::cin.getline(title, 256);
  9. std::cout << name << "'s favourite movie is " << title;
  10. return 0;
  11. }

20210303104535987.png

std :: istream::ignore

  1. istream& ignore (streamsize n = 1, int delim = EOF);

作用:提取并丢弃字符

从输入序列中提取字符并丢弃它们,直到提取出n个字符或一个等于delim的比较为止。

  1. // istream::ignore example
  2. #include <iostream> // std::cin, std::cout
  3. int main () {
  4. char first, last;
  5. std::cout << "Please, enter your first name followed by your surname: ";
  6. first = std::cin.get(); // get one character
  7. std::cin.ignore(256,' '); // ignore until space
  8. last = std::cin.get(); // get one character
  9. std::cout << "Your initials are " << first << last << '\n';
  10. return 0;
  11. }

20210303104858339.png

std :: istream::peek

  1. int peek();

作用:偷看下一个字符

返回输入序列中的下一个字符,而不提取该字符:保留该字符作为要从流中提取的下一个字符。

  1. // istream::peek example
  2. #include <iostream> // std::cin, std::cout
  3. #include <string> // std::string
  4. #include <cctype> // std::isdigit
  5. int main () {
  6. std::cout << "Please, enter a number or a word: ";
  7. std::cout.flush(); // ensure output is written
  8. std::cin >> std::ws; // eat up any leading white spaces
  9. int c = std::cin.peek(); // peek character
  10. if ( c == EOF ) return 1;
  11. if ( std::isdigit(c) )
  12. {
  13. int n;
  14. std::cin >> n;
  15. std::cout << "You entered the number: " << n << '\n';
  16. }
  17. else
  18. {
  19. std::string str;
  20. std::cin >> str;
  21. std::cout << "You entered the word: " << str << '\n';
  22. }
  23. return 0;
  24. }

20210303105310419.png

std :: istream::read、std :: istream :: tellg、std :: istream :: seekg

  1. istreamreadchar * sstreamsize n);

作用:读取数据块

从流中 提取n个字符并将其存储在s指向的数组中。

  1. streampos tellg();

作用:返回当前字符在输入流中的位置。

  1. istream& seekg (streampos pos);
  2. istream& seekg (streamoff off, ios_base::seekdir way);

作用:设置要从输入流中提取的下一个字符的位置

  1. // read a file into memory
  2. #include <iostream> // std::cout
  3. #include <fstream> // std::ifstream
  4. int main () {
  5. std::ifstream is ("test.txt", std::ifstream::binary);
  6. if (is) {
  7. // get length of file:
  8. is.seekg (0, is.end);
  9. int length = is.tellg();
  10. is.seekg (0, is.beg);
  11. char * buffer = new char [length];
  12. std::cout << "Reading " << length << " characters... ";
  13. // read data as a block:
  14. is.read (buffer,length);
  15. if (is)
  16. std::cout << "all characters read successfully.";
  17. else
  18. std::cout << "error: only " << is.gcount() << " could be read";
  19. is.close();
  20. // ...buffer contains the entire file...
  21. delete[] buffer;
  22. }
  23. return 0;
  24. }

20210303111114410.png

std :: istream::putback、std::istream::unget

  1. istream& putback (char c);
  2. istream& unget();

作用:放回字符

尝试将流中当前位置减少一个字符,使从流中提取的最后一个字符再次可用于输入操作

  1. // istream::putback example
  2. #include <iostream> // std::cin, std::cout
  3. #include <string> // std::string
  4. int main () {
  5. std::cout << "Please, enter a number or a word: ";
  6. char c = std::cin.get();
  7. if ( (c >= '0') && (c <= '9') )
  8. {
  9. int n;
  10. std::cin.putback (c);
  11. // std::cin.unget();
  12. std::cin >> n;
  13. std::cout << "You entered a number: " << n << '\n';
  14. }
  15. else
  16. {
  17. std::string str;
  18. std::cin.putback (c);
  19. //std::cin.unget();
  20. getline (std::cin,str);
  21. std::cout << "You entered a word: " << str << '\n';
  22. }
  23. return 0;
  24. }

2021030311153249.png

std :: istream :: sync

  1. int sync();

作用:同步输入缓冲区

发表评论

表情:
评论列表 (有 0 条评论,423人围观)

还没有评论,来说两句吧...

相关阅读