//从键盘输入到程序,叫标准input;从程序输出到显示器,叫标准output;一并叫标准I/O
//文件的输入和输出,叫文件I/O
cout<<"hellow word";//不加上< char ch=cin.get();//读取一个字符 int main() { char ch2; cin.get(ch2);//会从缓冲区取走1个字符,如果没有会阻塞 char buf[256]; cin.get(buf,256);//会从缓冲区读 cin.getline(buf,256); char a= cin.peek();//窥探一下缓冲区,复制第一个字符出来 } 文件操作: ifstream,继承istream ofstream,继承ostream //和C#一样,通过流读写文件,但是C#通过FileStream的创建,用参数控制就可以了。 读文件操作 #include void CopyFile() { char* fileName="C:\\Users\\Destop\\souce.txt"; char* outputfileName= "C:\\Users\\Destop\\target.txt"; ifstream ism(fileName,ios::in); //有参构造,以只读方式打开文件 ostream osm(outputfileName,ios::out);//只写 //ostream osm(outputfileName , ios::out | ios::app);//只写,并且在文件末尾追加 //或者先无参构造,然后ism..open(fileName,ios::in); if( !ism )//肯定重载了“!”操作符 { cout<<"打开失败"< return; } char ch; while(ism.get(ch)) { osm.put(ch); } ism.close();//关闭 osm.close();//关闭 } 二进制文件操作:对象序列化 class Person { public : int age; int id; Person(int age,int id); Person(); } void WritePerson() { Person p1(10,20),p2(20,10);; char* outputfileName= "C:\\Users\\Destop\\target.txt"; ostream osm(TargetName,ios::out | ios::binary);//以二进制的方式写 osm.write((char*)&p1,sizeof(Person));//专门以二进制的方式写 osm.write((char*)&p2,sizeof(Person));//将第二个对象也写进去 osm.close(); } void ReadPerson() { char* outputfileName= "C:\\Users\\Destop\\target.txt"; ifstream ism(outputfileName, ios::in | ios::binary);//以二进制方式读。 Person p1,p2; ism.read((char*)&p1,sizeof(Person)); ism.read((char*)&p2,sizeof(Person)); ism.close(); }