自做美食哪些网站,html公司网页,长沙创求网络科技有限公司,网站后台管理jsI/O 流#xff0c;流类库 输入流#xff1a;与输入设备#xff08;如键盘#xff09;相联系的流 输出流#xff1a;与输出设备#xff08;如显示器#xff09;相联系的流 输入/输出流#xff1a; 与输入输出设备#xff08;如磁盘#xff09;相联系的流 流类#xf…I/O 流流类库 输入流与输入设备如键盘相联系的流 输出流与输出设备如显示器相联系的流 输入/输出流 与输入输出设备如磁盘相联系的流 流类C中预先定义了一组类用于处理输入/输出 流类库C中所有的流类组成的集合 streambuf 类底层
派生类
1. filebuf 类使用文件来保存缓冲区的字符序列
2. strstreambuf 类在内存中进行提取和插入操作的缓冲区管理
3. conbuf 类提供控制光标、设置颜色、定义活动窗口、清屏、清行等功能
ios 类 高层
直接派生类 1. 输入流istream 2. 输出流ostream 3. 文件流fstreambase 4. 串流strstreambase 键盘输入与屏幕输出
一般的输入/输出
数据传输方法
1. 使用在标准输入/输出头文件 stdio.h 中声明的库函数
2. 使用标准命名空间 std 中的输入输出流类文件 iostream 中定义的流对象 cin 和 cout
3. 使用 和
#include iostream
#include stringusing namespace std;const int size 80;int main()
{char buffer[size],buffer2[size];char buffer3[size];string str1,str2;cout Enter a sentence: ;cin buffer;cout buffer with cin: buffer endl;cin.get(buffer2,size);cout buffer2 with cin.get: buffer2 endl;cout Enter a sentence: ;cin.getline(buffer3,size);cout buffer3 with cin.getline: buffer3 endl;cout Enter strings: ;cin str1 ;cout str1 with cin: str1 endl;getline(cin,str2);cout str2 with getline: str2 endl;return 0;
}
格式化输入/输出
#include iostream
using namespace std;int main()
{cout.setf(ios::showpos); //符号显示cout.setf(ios::scientific); //科学计数法cout 123 123.3 endl;cout.unsetf(ios::showpos); //符号不显示cout 123 123.3 endl;return 0;
}
#include iostream
#include iomanip
using namespace std;int main()
{int i6879;int j1234;int k-10;coutsetw(6)ijkendl;coutsetw(6)isetw(6)jsetw(6)kendl;return 0;
}
自定义控制符函数
#include iostream
#include iomanip
using namespace std;ostream setup(ostream stream)
{stream.setf(ios::left);stream setw(10)setfill($);return stream;
}int main()
{cout 10 Hello endl;cout setup 10 Hello endl;cout setup 10 10 Hello endl;return 0;
}
重载提取运算符和插入运算符
#include iostream
using namespace std;class CDate
{
private:int Date_Year;int Date_Month;int Date_Day;
public:friend istream operator (istream input, CDate Date);friend ostream operator (ostream output, const CDate Date);
};istream operator (istream input, CDate Date)
{input Date.Date_Year Date.Date_Month Date.Date_Day;return input;
}ostream operator (ostream output, const CDate Date)
{output Date.Date_Year - Date.Date_Month - Date.Date_Day;return output;
}int main()
{CDate d;cin d;cout d endl;return 0;
}
文件的输入/输出 cpp 读写文件操作 1. 包含头文件 fstream 2. 定义一个文件流对象 3. 建立或打开文件 4. 进行读/写操作 5. 关闭文件 文件的打开与关闭 两种方式 1. 先定义文件流对象然后调用成员函数 open 打开文件 2. 在定义文件流对象时直接利用其构造函数打开文件 get() 和 put() 一般成对使用既可用于读写文本文件也可用于读写二进制文件每次读写一字节 #include fstream
#include iostream
#include string
using namespace std;void CreateFile(string filename);
void ReadFile(string filename);int main() {CreateFile(test.txt);ReadFile(test.txt);return 0;
}void CreateFile(string filename) {ofstream file(filename);if (file.is_open()) {file Hello, world! endl;file.close();}else {cout Error: Could not open file. endl;}
}void ReadFile(string filename) {ifstream file(filename);if (file.is_open()) {string line;while (getline(file, line)) {cout line endl;}file.close();}else {cout Error: Could not open file. endl;}
} read() 和 write() 一般成对使用既可用于读写文本文件也可用于读写二进制文件每次读写一个数据块