c++ 第八章 输入输出流和文件

1、ostream类的成员函数write,put
cout.write("hello,world!",sizeof("hello,world!")).put('\n');
2、格式状态字,在ios_base类中定义的一个枚举结构_Fmtflags, ios_base派生了ios,ios派生了ostream。
输出流,枚举结构的成员如下(表征状态字的各位是1或0)默认的只有skipws 和 dec
skipws // 跳过输入中的空白       默认项
unibuf  //插入操作《后,立即刷新缓冲区
uppercase  //十六进制中字母大写 及前缀字母X大写
showbase  //显示八、十六进制前缀0、 0x
showpoint   //显示浮点数末尾的小数点和0
showpos   //正数前加+
left    //左对齐
right  //右对齐
internal // 两端对齐 在符号位后基前缀后填充字符
dec // 十进制 默认选项
oct // 八进制
hex // 十六进制
scientific // 科学计数法表示小数
fixed // 定点表示小数
boolalpha  // bool 变量以true 和 false 形式输出
adjustfield   // left + right + internal
basefield  // dec + oct + hex
floatfield  // scientific + fixed
格式状态字的读取 long flags()
格式状态字的设置 long flags (long),long  setf(long lFlags),long setf(long add,long remove),unset(long remove)
cout.flags(cout.flags() | ios_base::left | ios_base::scientific);
cout.flags((cout.flags() & ~ios_base::basefield) | ios_base::hex);  // 先将进制位全置为0,再将十六进制位置1
cout.setf(ios_base::boolalpha);  // 使boolalpha位为1 不管之前是0或是1
cout.setf(ios_base::hex,ios_base::basefield);   // 等价于先将所有进制位置0  再将十六进制位置1
读取域宽 int i = cout.width()
设置域宽  int before = cout.width(10);  //只有一次有效 返回原域宽
返回填充字符  char c = cout.fill();
设置填充字符  char  before = cout.fill('*'); //永久有效 返回原填充字符
控制符实质就是函数,原型为: ios_base& 控制符(ios_base&)
boolalpha     noboolalpha     showbase    noshowbase  showpoint   noshowpoint  showpos   noshowpos   skipws  noskipws    uppercase  nouppercase   dec  hex   oct   left   right  internal    fixed  scientific    flush  endl    setfill(char)  setprecision(char)   setw(int )   
3、输入流的格式状态字和输出流是彼此独立的,影响输入流的有: skipws  dec oct  hex   cin.width(n)  setw(n)   
get()读取单个字符
#include
using namespace std;
int main()
{
char a,b,c;
cin.get(a).get(b);
cout << a << "," << b << endl;
while((c =cin.get()) != EOF)
{
cout << c;
}
return 0;
}
使用get   getline  读取c风格字符串  二者区别在于使用get会将分隔符遗留在输入流中 getline会取出来并丢弃 ,二者都不会添加到字符串末尾,都会在字符末尾添'\0', 结束字符默认为'\n' ,读入的最大字符数为第二个参数减去一
#include
using namespace std;
int main()
{
char sz1[10] = {0};
char sz2[10] = {0};
cin.get(sz1,10);
cout << sz1 << endl;
cin.get();;                               //用来读取流中遗留的'\n'
cin.getline(sz2,10,'A');
cout << sz2 << endl;
}
ignore函数:从流中丢弃n个字符,或碰到终止符   istream&  istream::ignore(int n = 1,int  c = '\n')
cin.ignore(100,'\n');
read函数:不会在末尾自动添'\0',只有两个参数  istream& istream::read(char* buf,int  n);
peek  函数  :只会读一个字符,不会从流中取出
puckback函数:  将一个字符放回流的始端,下一次最开始取出它
gcount函数 :返回上一次非格式读取的字符数包括get   getline  read  》
4、流状态
ios_base 中定义的表征流状态的枚举结构Iostate
goodbit = 0x0; //正常
eofbit = 0x1; //流末尾
failbit = 0x2;  //操作失败,如没能取得所需字符
badbit = 0x4;//非法操作
读取流状态 bool good()   bool eof()  bool failure()  bool bad()   int  rdstate()
管理流状态  :
清除错误标志,为0表示清除所有错误标志  void ios_base(int nState = 0)
设置标志 ,void ios_base(int nState = 0)

刷新缓冲区 ,cin.sync()   int sync()   //fflush(stdin)

5、文件操作

ifstream  ofstream  fstream

void  open(const  char* filename ,int mode ,int access)

mode:ios::app(追加,写操作)  ios::ate(定位到末尾,读写操作)ios::binary  ios::in  ios::out  ios::trunc(清除)

access:0 普通文件打开访问  1 只读文件  2  隐藏文件 4  系统文件

file.close()//file为ostream对象 此操作仅仅断开文件和流的连接,继续可以和其他文件绑定

is_open() //判断文件是否关联成功

二进制文件读写

read(unsigned char*  buf ,int num)// 若还没有读到num个,就到了文件末尾,可用成员函数 int gcount()来取得实际读取的字符数 可能要用到指针类型转换

write(const unsigned char* buf,int num)

文件定位指针和随机读取

istream& seekg(streampos pos)

istream& seekg (streamoff offset, seek_dir origin)

iistream& seekg(streampos pos)

istream& seekg (streamoff offset, seek_dir origin)

seekdir:    iso_base::cur  ios_base::beg  ios_base::end

pos_type tellg();

pos_type tellp();


6、字符串流

sstream: sstringstream  istringstream  ostringstream


你可能感兴趣的:(c++ 第八章 输入输出流和文件)