C++之IO

xiaoxiao2026-08-08  1

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

最新回复(0)