C++输入输出流学习笔记

自己之前在学习C++ 的时候从来没有系统地学习过输入输出流,眼看各种面试就要蜂拥而来,不禁一慌,赶紧拿出本C++ Primer Plus来看一下,自己写一些小程序测试一下。让自己对cin, cout有更深的认识。

1.cout进行输出

cout<< 可以识别C++中所有的基本类型,对应不同类型的参数会采用不同的原型。函数最终返回一个 ostream &即引用类型,所以输出可以通过<<连接起来,可以 cout<< xxx << xxx << xxx;

ostream还为下面的指针类型定义了插入操作符函数:

const signed char * ; const unsigned char * ; const char * ; void *

因为C++ 用指向字符串存储地址的指针来表示字符串,因此用cout来输出 char *, signed char *, unsigned char *,最终都会输出整个字符串

如果希望输出的是字符串的首地址需要将 char * 转换为 void * 来输出。

char * amount = "dozen";

cout<< amount; //输出dozen字符串

cout<<(void*)amount; //输出dozen字符串的首地址

ostream除了<< 操作之外还提供了put() 和write() 方法,前者用于显示字符,后者用于显示字符串。

ostream & put(char) 只有char原型,其他的类型都会转换为char。put的返回类型也是ostream &,可连续使用 cout.put(x).put(y).put(z)

write()显示整个字符串

cout.write (const char_type * s, streamsize n); write()并不会遇到空字符时自动停止打印字符

多数C++ 实现会在输入即将发生时刷新缓冲区。flush刷新缓冲区,而控制符endl刷新缓冲区,并插入一个换行符

新式的C++中当输出整数or浮点数时,指数大于等于6或小于等于-5时,将用科学技术法表示。

cout<<dec; cout<<hex; cout<<oct; 会改变输出的格式为10进制,16进制,8进制

cout.width(5)将下次输出的宽度设定为5,只影响下一次的输出格式

cout.fill('*') cout默认用空格填充字段中未被使用的部分,可以用fill()成员函数来改变填充字符。新的填充字符将一直有效,直到改变它为止。

cout.precision(5) C++的默认精度为6,precision()用来设置精度,设置将一直有效

cout.setf(ios_base::showpoint),显示小数点和末尾的0

setf(): ios_base::boolalpha ios_base::showbase ios_base::showpoint ios_base::uppercase ios_base::showpos

2.cin进行输入

istream类为下列字符指针重载了>>抽取操作符, signed char *; unsigned char*; char *;

cin>> 读取从非空白字符开始,到与目标类型不匹配的第一个字符之间的全部内容

流状态:eofbit, badbit, failbit 当全部三个状态都为0说明一切顺利。程序可以检查流状态来决定下一步做什么

当输入不匹配或到达文件尾时,需要使用不带任何参数的clear()重新打开输入

单字符输入: cin.get(char & ch); 到达文件尾后,cin.get(void)都将返回值EOF——iostream头文件提供的一个符号常量。也可以使用 ch = cin.get();

字符串输入:getline() get() ignore()

istream & getline(char *, int, char);  istream & getline(char *, int); //getline将换行符丢弃

istream & get(char *, int, char); istream & get(char *, int); //get将换行符留在流中

istream & ignore(int, char); istream & ignore();

int均为读取个数,char为作为分界符的字符

其他的istream read() peek() gcount() putback()

3.文件输入和输出

让程序写入文件:

ofstream fout;

fout.open("filename");

或 ofstream fout("filename");

fout<<"Dull Data"; //Dull Data写入fout中

由于磁盘驱动器被设计成以大块的方式传输数据,而不是逐字节地传输,因此通过缓冲可以大大提高从程序到文件的传输数据的速度。

让程序读取文件:

ifstream fin;

fin.open("filename");

或ifstream fin("filename");

char buf[80];

fin>>buf;

fin.close();

 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 
 5 using namespace std;
 6 
 7 int main() {
 8         string filename;
 9         cout<<"Please input the filename
";
10         cin>>filename;
11         
12         ofstream fout(filename.c_str());
13 
14         fout<<"For your eyes only
";
15         cout<<"Enter your secret number
";
16         float secret;
17         cin>>secret;
18         fout<<"Your secret number is "<<secret<<endl;
19         fout.close();
20 
21         ifstream fin(filename.c_str());
22         cout<<"Here are the contents of "<<filename<<":
";
23         char ch;
24         while(fin.get(ch))
25                 cout<<ch;
26         cout<<"Done
";
27         fin.close();
28 
29         string new_file;
30         cin>>new_file;
31         ofstream new_ofstream;
32         new_ofstream.open(new_file.c_str());
33         new_ofstream<<"Hello";
34         new_ofstream.close();
35         ifstream new_ifstream;
36         new_ifstream.open(new_file.c_str());
37         while(new_ifstream.get(ch))
38                 cout<<ch;
39         new_ifstream.close();
40 
41         return 0;
42 }

其中string.c_str()是将string的头指针转化成char* 返回

C++支持打开多个文件,如果可以减少文件同时打开的数目应尽量减少,节省计算机资源

文件模式:

ios_base::in 打开以读取

ios_base::out 等价于ios_base::out | ios_base::trunc

ios_base::out | ios_base::trunc 打开以写入, 如果已经存在, 则截短文件

ios_base::out | ios_base::app 打开以写入,只追加

ios_base::out | ios_base::out 打开以读写,在文件允许的位置写入

ios_base::out | ios_base::out | ios_base::trunc 打开以读写,如果已经存在,则首先截短文件

ios_base::out | ios_base::binary 以C++mode和二进制模式打开

ios_base::out | ios_base::ate 以指定的模式打开,并移至文件尾。

-------------END-----------

原文地址:https://www.cnblogs.com/noanswer/p/3330332.html