---
title: "C++之I/O"
author: "yunchow"
date: "2009-05-27T21:26:20Z"
tags: ["C", "C#", "C++", "iOS"]
categories: ["编程语言"]
slug: "c-i-o"
draft: false
lastmod: "2009-05-27T21:26:20Z"
id: 397521
url: "https://yunchow.iteye.com/blog/397521"
---

\* 重载 + 拷贝构造函数A(const A& o)会把原对象原样复制一份. + 运算符重载 - 单目 - 双目 - 特殊 ------------------------------ * I/O流(iostream) I 输入 从控制台(文件)读取 O 输出 向控制台(文件)写 牢牢记住: 文件和控制台的IO是完全统一的. 数据的传输叫流 所有的流操作都在头文件 iostram 中. cin 键盘(标准输入设备) cout 屏幕(标准输出设备) 利用重定向改变输入输出方向. 特点:提供标准的IO操作,支持缓冲,支持重定向(改向). 满了 输出 ---------&gt; 显示 刷新 回车 输入过程(cin):键盘 -------&gt; 键盘缓冲区 ---&gt; 输入缓冲区 --&gt; 程序 读取 \[code="c++"]//缓冲例子1 #include using namespace std; int main() { char ch,c; cout &lt;&lt; "Please input a char :"; cin &gt;&gt; ch; cout &lt;&lt; "ch=" &lt;&lt; ch &lt;&lt; endl; cout &lt;&lt; "Please input a char :"; cin &gt;&gt; c; cout &lt;&lt; "c=" &lt;&lt; c &lt;&lt; endl; }\[/code] //以下两个输出没有缓冲,也不能重定向,用法跟 cout 完全一样 cerr clog \[code="c++"]//缓冲例子2 #include using namespace std; #include int main() { cout &lt;&lt; "Hello"; cerr &lt;&lt; "World"; for(int i=0;i&lt;5;i++){ time\_t t = time(NULL); while(t==time(NULL)); } cout &lt;&lt; endl; }//会输出 WorldHello \[/code] ios | ------------ | | istream ostream | | ------------- | ifstream iostream ofstream 根据以上继承关系,文件IO与控制台IO完全一致. 以下我看们如何准备文件对象: 控制台对象是已经准备好的.自动释放. \[code="c++"]ofstream fout("a.txt"); fout &lt;&lt; "Hello"; fout.close(); ifstream fin("a.txt"); fin &gt;&gt; 变量名; fin.close();\[/code] ios里重写了(void \*)操作运算符.当出错会输出NULL,也就是0 一个IO流对象在没有出错时可以当成真,在出错时当成假. -------------------------------------------------- 输入操作:把字符变成了需要的类型. 因此输入还有一个名字叫格式化输入. 而输出与输入刚好相反.也称格式化输出. *　非格式化的输入输出．(只对字符操作) 不做任何的格式处理． \*get() 读取一个字符 \[code="c++"]get()从输入流中读取一个字符,返回字符的ascii码. cout &lt;&lt; cin.get() &lt;&lt; ... get(char&)从输入流中读取一个字符,保存到形参中.与 cin&gt;&gt;...相比,它能读一些特殊字符.而cin是不行的. get(char&)可以读取任何字符. \[/code] \[code="c++"]//例程 #include #include #include using namespace std; int main( int argc, char* argv\[] ) { cout &lt;&lt; " ===== " &lt;&lt; argv\[1] &lt;&lt; " ======" &lt;&lt; endl; ifstream fin( argv\[1] ); char ch; while ( fin.get(ch) ){ time\_t t = time(NULL); while(t==time(NULL)); cout &lt;&lt; ch &lt;&lt; flush; } fin.close(); }\[/code] * getline(字符数组名,长度)要保证长度够用,比一行实际长度大. \[code="c++"]char string1\[256]; cin.getline(string1,256); //get a whole line cin &gt;&gt; string1; // stop at the first blank space\[/code] 输入流一旦处于错误状态就不再执行任何输入. 但我们可以手动将其恢复到正常状态.cin.clear();//不是清缓冲区. 我们可以用while(cin.get!='\\n');//清空缓冲区 \[code="c++"]cin.ignore(长度, '\\n')//清空缓冲区 void istream::ignore(int len=1,char c=-1) //函数原型 { for(int i=0; i using namespace std; int main() { cout &lt;&lt; "Input No. or name:"; int no; char name\[20]; char ch = cin.peek(); //偷看一下,不会读走 if(ch&gt;='0' && ch&lt;='9'){ cin &gt;&gt; no; cout &lt;&lt; "no=" &lt;&lt; no &lt;&lt; endl; } else{ cin.getline(name,20); cout &lt;&lt; "name=" &lt;&lt; name &lt;&lt; endl; } } \[/code] * cin.putback()//向输入流中插入一个字符串. 读哪一个退哪一个,不能乱退. \[code="c++"]//例程 #include using namespace std; int main() { char ch; int n; cout &lt;&lt; "Input an integer:" ; cin.get(ch); cin &gt;&gt; n; cout &lt;&lt; "ch=" &lt;&lt; ch &lt;&lt; endl; cout &lt;&lt; "n=" &lt;&lt; n &lt;&lt; endl; cout &lt;&lt; "Input an integer:" ; cin.ignore(1000,'\\n'); cin.get(ch); cin.putback(ch); cin &gt;&gt; n; cout &lt;&lt; "ch=" &lt;&lt; ch &lt;&lt; endl; cout &lt;&lt; "n=" &lt;&lt; n &lt;&lt; endl; }\[/code] 我们可以用cin.width(10)//来限制输入宽度. cin &gt;&gt; ws;//跳过输入缓冲区中空白字符. * read() //后面再说 -------------------------- * 非格式化的输出(只对字符操作) + put(char) //输出一个字符,与cout&lt;&lt;相比,它更纯洁. cout.put(cin.get()); //保证输出一定是个字符 &lt;&lt; endl;//输出一行,没有putline()函数 fout.write(地址,字节数)//把一块内存中的内容写到文件中. fin.read(地址,字节数)//把文件中的内容读到一块内存空间中. 函数原型: XXX(char* addr, int bytes) \[code="c++"]#include #include using namespace std; int main() { int n = 123; double d = 4.56; struct S{ char c; bool b; char addr\[100]; }; S s={'S',true,"beijin"}; class A{ int data; public : A(int d=0) : data(d){} void show() const { cout &lt;&lt; "data=" &lt;&lt; data &lt;&lt; endl; } }; A a(789); ofstream fout("var.dat"); fout.write((char\*)&n,sizeof(n)); fout.write((char\*)&d,sizeof(d)); fout.write((char\*)&s,sizeof(s)); fout.write((char\*)&a,sizeof(a)); } #include #include using namespace std; int main() { int n = 0; double d = 0.0; struct S{ char c; bool b; char addr\[100]; }; S s={'\\0',false,""}; class A{ int data; public : A(int d=0) : data(d){} void show() const { cout &lt;&lt; "data=" &lt;&lt; data &lt;&lt; endl; } }; A a(0); ifstream fin("var.dat"); fin.read((char\*)&n,sizeof(n)); fin.read((char\*)&d,sizeof(d)); fin.read((char\*)&s,sizeof(s)); fin.read((char\*)&a,sizeof(a)); fin.close(); cout &lt;&lt; "n=" &lt;&lt; n &lt;&lt; endl; cout &lt;&lt; "d=" &lt;&lt; d &lt;&lt; endl; cout &lt;&lt; "s.c=" &lt;&lt; s.c &lt;&lt; endl; cout &lt;&lt; "s.b=" &lt;&lt; s.b &lt;&lt; endl; cout &lt;&lt; "s.addr=" &lt;&lt; s.addr &lt;&lt; endl; a.show(); }\[/code] //因此可以将任何数据保存到文件中.将内存中变量保存到文件中.