ignore,putback,peek文件

2016-11-21  本文已影响0人  萌面大叔2

※标准流的设备名

标准流的设备名.png

※成员函数ignore

#include<iostream>
using namespace std;
int main()
{
    string s;
    cout<<"请输入一串字符串";
    //1.忽略输入缓存区中的前8个字符 
    //2.在前8个字符中存在结束字符,那么就忽略 
    //输入缓冲区 结束字符 之前的字符 
    cin.ignore(8,' ');//设置' '为结束字符 
    cin>>s;
    cout<<"string s="<<s<<endl;
    return 0; 
}

运行结果:


1.PNG

※成员函数putback

#include<iostream>
using namespace std;
int main()
{
    char ch;
    cin.putback('a');
    cout<<"请输入一个ch数据:";
    cin>>ch;
    cout<<"string ch="<<ch<<endl;
    return 0; 
}


运行结果:

3.PNG

※成员函数peek

#include<iostream>
using namespace std;
int main()
{
    int i;
    string s;
    //输入缓冲区中第一个字符 
    cout<<"strart"<<endl;
    char ch=cin.peek();//没有数据的时候,等待用户输入 
    cout<<"end"<<endl;
    if((ch>='0')&&(ch<='9'))
    {
        cin>>i;//"123abc"   i=123
        cout<<"int i="<<i<<endl;
    }
    else
    {
        cin>>s;
        cout<<"string s="<<s<<endl;
    }
}


运行结果:


2.PNG

**※整数流的基数:流操纵算子dec、oct、hex和setbase **

**整数通常被解释为十进制(基数为10)整数。如下方法可改变流中整数的基数: **

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int i=11;
    cout<<hex<<i<<" "<<dec<<i<<endl;//11转换为16进制变成b,在转换成十进制变成11
    cout<<setbase(8)<<i<<endl;//变成八进制
    return 0; 
}

运行结果:

1.PNG

※设置浮点数精度(precision、setprecision)

可以用流操纵算子setprecision或成员函数percision控制小数点后面的位数。

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    double roof2=3.141592666;
    for(int places=0;places<=9;places++)
    cout<<setprecision(places)<<roof2<<'\\n' ;
    return 0; 
}

运行结果:


2.PNG

※设置域宽(setw、width)

  • 成员函数ios.width设置当前的域宽(即输入输出的字符数)并返回以前设置的域宽。
例题1:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int t1[5]={12345,1,3333,33,7777};
    int t2[5]={12,4,67,54645,653};
    for(int i=0;i<5;i++)
    {
        cout.width(6);//设置宽度 类型,作用相当于\\t
        cout<<t1[i];
        
    }
    cout<<endl;
    for(int j=0;j<5;j++)
    {
        cout.width(6);
        cout<<t2[j];
        
    }
    cout<<endl;
}

例题1:
运行结果:

3.PNG
例题2:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    char n[30]={0};
    cin.width(5);
    while(cin>>n)
    {
        cout<<"n="<<n<<endl;
        cin.width(5);
        
    }
    return 0;
}

例题2:
运行结果:


1.PNG

※用户自定义的流操纵算子

#include<iostream>
#include<iomanip>
using namespace std;
ostream& tab(ostream& output)//作用相当于\\t
{
    return output<<'\\t';
}
int main()
{
    cout<<'a'<<tab<<'b'<<'\\t'<<'c'<<endl;
    return 0;
}

运行结果:

output.PNG

※cin.good

#include<iostream>
#include<iomanip>
using namespace std;
#include<limits>
int main()
{
    int a;
    int b;
    cin>>a;
    cout<<"a="<<a<<endl;
    cout<<"cin1="<<cin.good()<<endl;//检查cin是否损坏 
    if(!cin.good())
    {
        cin.clear();//修复cin 
        cin.sync();//清除输入缓存区内的所有数据 Windows下的 
    //  cin.ignore(numeric_limits<streamsize>::max(),'\\n');清除输入缓存区内的所有数据 Linus下的 
    }
    cout<<"cin2="<<cin.good()<<endl;
    cin>>b;
    cout<<"b="<<b<<endl;
    return 0;
}

运行结果:


4.PNG

文件

文件打开

文件关闭

当文件的读写操作完成之后,我们必须将文件关闭以使文件重新变为可访问的。关闭文件时需要调用成员函数close( ),它负责将缓存中的数据排放出来并关闭文件。
这个函数一旦被调用,原来的流对象就可以被用来打开其他的文件了,这个文件也可以重新被其他的进程访问了。

include<fstream>

fstream file;
file.open(“example”, ios::out | ios::app | ios::binary);
if(file !=NULL) {
cout<<“open failed”<<endl;
}
//……. 文件操作
file.close();

在文件中输入
#include<iostream>
#include<iomanip>
using namespace std;
#include<fstream>
int main()
{
    ofstream ofile("zyz1");//打开文件
    ofile<<"pear"<<" "<<4.5;//在文件中输入
    ofile.close();//关闭文件
    return 0; 
} 

读文件内容:
#include<iostream>
#include<iomanip>
using namespace std;
#include<fstream>
int main()
{
    ifstream ifile("zyz1");
    char sztext[20];
    double price;
    ifile>>sztext>>price;
    cout<<sztext<<" "<<price;
    ifile.close();
    return 0; 
} 

运行结果:


5.PNG

以二进制的形式在文件中存储

文件write
#include<iostream>
#include<iomanip>
using namespace std;
#include<fstream>
int main()
{
    ofstream ofile("zyz2.txt",ios::out|ios::binary);
    char temp[20]="nihao";
    ofile.write(temp,20);
    ofile.close();
    return 0; 
} 

文件read
#include<iostream>
#include<iomanip>
using namespace std;
#include<fstream>
int main()
{
    ifstream ifile("zyz2.txt",ios::in|ios::binary);
    char temp[20];
    ifile.read(temp,20);
    cout<<temp<<endl;
    ifile.close();
    return 0; 
} 

运行结果:


1.PNG
流指针相关函数
  • tellg( )和 tellp( )
返回一个pos_type类型,即整数,分别代表当前读指针(get) 和 写指针(put) 的位置
  • seekg( pos_type position ) 和 seekp( pos_type position )
    流指针被改变为指向文件开始计算的一个绝对位置,传入的参数类型与函数tellg 和 tellp 的返回值类型相同
  • seekg( offset, seekdir) 和 seekp( offset, seekdir)
    从由参数seekdir 设定的位置开始计算一个位移 offset,其中seekdir的值可以是: ios::beg(流开始的位置),ios::cur(流当前的位置),ios::end(流末尾的位置)
#include<iostream>
using namespace std;
#include<fstream>
int main()
{
    ofstream ofile("exam1.txt",ios::out|ios::binary);
    char temp[20]="nihao";
    ofile.write(temp,20);
    ofile.close();
    
    ifstream ifile("exam1.txt");
    if(NULL==ifile)
    {
        cout<<"打开文件失败"<<endl;
        return -1;
    }
    //定位函数 -get 指针(读指针) 
    ifile.seekg(0,ios::end);
    //指针位置函数 -get指针(读指针) 
    cout<<"get point position:"<<ifile.tellg()<<endl;
    ifile.close();
    return 0; 
} 

运行结果:

2.PNG
上一篇 下一篇

猜你喜欢

热点阅读