软件设计课件:Lecture 10 CPP Stream Input and Output (1).pptVIP

  • 0
  • 0
  • 约1.31万字
  • 约 66页
  • 2021-11-30 发布于安徽
  • 举报

软件设计课件:Lecture 10 CPP Stream Input and Output (1).ppt

单字节低级IO操作 is.get(ch) 将 istream is 的下一个字节放入 ch,返回 is os.put(ch) 将字符 ch 放入 ostream,返回 os is.get() 返回 is 的下一字节作为一个 int 值 is.putback(ch) 将字符 ch 放回 is,返回 is is.unget() 将 is 退回一个字节,返回 is is.peek() 将下一字节作为 int 值返回但不移出它 上述表格中的未格式化的操作一次一个字节地处理流,它们不忽略空白地读 Before input, cin.eof() is 0 Enter a sentence followed by end-of-file: Testing the get and put member functions Testing the get and put member functions ^Z ? EOF in this system is: -1 After input cin.eof() is 1 多字节低级IO操作 is.get(sink, size, delim) 从 is 中读 size 个字节并将它们存储到 sink 所指向的字符数组中。读操作直到遇到delim 字符,或已经读入了 size 个字节 is.get(sink, size, delim) 或遇到文件结束符才结束。如果出现了delim,就将它留在输入流上,不读入到 sink 中。 is.getline(sink, size, delim) 与三个实参的 get 行为类似,但读并丢弃delim is.read(sink, size) 读 size 个字节到数组 sink。返回 is is.gcount() 返回最后一个未格式化读操作从流 is 中读到的字节数 os.write(source, size) 将 size 个字从数组 source 写至 os。返回os is.ignore(size, delim) 读并忽略至多 size 个字符,直到遇到 delim,但不包括 delim。默认情况下,size 是 1 而 delim 是文件结束符 Enter a sentence: Contrasting string input with cin and cin.get The string read with cin was: Contrasting The string read with cin.get was: string input with cin and cin.get Enter a sentence: Using the getline member function ? The sentence entered is: Using the getline member function Thank you! * IO对象都重载了转换操作符operator bool(),在用于条件判断的时候,因为需要一个bool类型,编译器就会对IO对象进行转换,当IO对象的条件不处于任何一种错误状态的时候就会返回true。 * * Outline Introduction Design of C++ IOStreams Usage of IOStreams IO对象不可复制或赋值 标准库类型不允许对流对象做复制或赋值操作,这个要求有两层含义: 不能把流对象存储在vector等容器中:因为只有支持复制的元素类型才可以存储在vector等容器中,而流对象不支持复制 形参或返回类型不能为流类型:如果需要传递或返回IO对象,必须传递或返回指向该对象的指针或引用 例子: ostream out1 = cout; //错误,流对象不能赋值 ostream out2 = cout; //正确,可以使用引用 一般情况下,如果需要传递IO对象对它进行读写,则必须使用非const引用,因为对IO对象的读写会改变它的状态 Enter grade (enter end-of-file to end): 67 Enter grade (enter end-of-file to end): 87 Enter grade (enter end-of-file to end): 73 Enter grade (enter end-of-file to end): 95 Enter grade (enter end-of-file to end): 34 Enter grade (enter end-of-file to end): 99 Enter grade (enter end-of-file to end): ^Z Highest grade i

文档评论(0)

1亿VIP精品文档

相关文档