美文网首页
C++基础-(IO和文件操作)

C++基础-(IO和文件操作)

作者: I踏雪寻梅 | 来源:发表于2016-11-16 20:31 被阅读0次

C++基础

IO和文件操作

  • 标准输入输出的设备名有哪些
  • 文件输入类的类名是:iostream
nt g=0;
int fun() 
{
    return ++g;
}
int main(int argc, char **argv)
{
    cout<<fun()<<fun()<<fun()<<fun()<<endl;
    return 0;
}
//4321
  • 输出流注意事项
    • 计算顺序: 自右向左
    • 输出顺序: 自左向右
      • cout 在执行时相当于一个函数,而即将输出的4 个fun( )
      • 函数相当参数,编译器在函数调用时的入栈顺序是从右向左的,所以在指向fun( )函数时,依次从右向左执行,执行完fun()函数之后,cout输出各个参数返回的值,此时又是从左至右进行输出,所以函数的执行结果为:4321
  • ignore使用
#include <iostream>
using namespace std;
int main()
{
    string s;
    cout<<"请输入一串字符:";
    //忽略输入缓冲区前8个字符
    //在前8个字符中存在结束字符,那么就忽略
    //输入缓冲区中,结束字符之前的字符
    cin.ignore(8,' ');
    cin>>s;
    cout<<"strig s="<<s<<endl;
}
/*
1234567890=>90
123 456=>456
*/
  • putback使用
#include <iostream>
using namespace std;
int main()
{
    char ch;
    cin.putback('a');
    cout<<"请输入一个ch数据:";
    cin>>ch;
    cout<<"char ch="<<ch<<endl;
}
//char ch=a
  • peek的使用
#include <iostream>
using namespace std;
int main()
{
    int i;
    string s;
    //获得输入缓存区中的第一个字符
    cout<<"star"<<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;
    }
}
/*
asd=>asd
123=>123
asd123=>asd123
123asd=>123
*/
  • 流操作算子
    • 整数流的基数:流操纵算子dec、oct、hex和setbase
    • 整数通常被解释为十进制(基数为10)整数。如下方法可改变流中整数的基数:
      • 插人流操纵算子hex可设置十六进制基数(基数为16)、
      • 插人流操纵算子oct可设置八进制基数(基数为8)、
      • 插人流操纵算子dec可恢复十进制基数。
    • 用流操纵算子setbase来改变基数,使用setbase或其他任何参数化的操纵算子都必须在程序中包含头文件iomanip
    • 如果不明确地改变流的基数,流的基数是不变的
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int i=11;
    cout<<hex<<i<<" "<<dec<<14<<endl;
    cout<<otc<<i<<" "<<dec<<14<<endl;
    cout<<setbase(8)<<i<<endl;
}
/*
b 14
13 14
13
*/
  • 浮点数的精度
    • 设置浮点数精度(precision、setprecision)
      • 可以用流操纵算子setprecision或成员函数percision控制小数点后面的位数。
      • 设置了精度以后,该精度对之后所有的输出操作都有效,直到下一次设置精度为止。
      • 无参数的成员函数percision返回当前设置的精度。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    double root2=3.14159265;
    for(int places=0;places<=9;places++)
    {
        cout<<setprecision(places)<<root2<<'\n';
    }
}
  • 设置域宽(setw、width)
    • 成员函数ios.width设置当前的域宽(即输入输出的字符数)并返回以前设置的域宽。
    • 如果显示数据所需的宽度比设置的域宽小,空位用填充字符填充。
    • 如果显示数据所需的宽度比设置的域宽大,显示数据并不会被截断,系统会输出所有位。
    • 域宽设置仅对下一行流读取或流插入操作有效,在一次操作完成之后,城宽又被置回0
    • 未对所处理的输出数据提供足够的域宽时,输出数据将按需要的域宽进行输出,有可能产生难以阅读的输出结果。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int t1[5]={1,22,333,4444,55555};
    int t2[5]={111,222,333,444,555};
    for(int i=0;i<5;i++)
    {
        cout.width(5);
        cout<<t1[i];
    }
    cout<<endl;
    for(int j=0;j<5;j++)
    {
        cout.width(6);
        cout<<t2[j];
    }
    cout<<endl;
}
/*
5:   
     1   22  333 444455555
   111  222  333  444  555
6:    
     1    22   333  4444 55555
   111   222   333   444   555
4:
     1  22 333444455555
   111 222 333 444 555
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    char n[30];
    cin.width(5);
    while(cin>>n)
    {
        cout<<"n="<<n<<endl;
        cin.width(5);
    }
    return 0;
}
/*
123
n=123

12345
n=1234
n=5

123456789
n=1234
n=5678
n=9
*/
  • 文件操作
    • ofstream: 写操作的文件类(输出,由ostream 引申而来)
    • ifstream: 读操作的文件类(输入,由istream 引申而来)
    • fstream: 可同时读写操作的文件类(由iostream 引申而来)
  • 文件打开
    • 使用构造函数打开
     eg: ofstream ofile(const char *filename, openmode);
    
    • 使用成员函数open打开
    eg: ofstream ofile;
     ofile.open(const char *filename, openmode)
    
  • 文件打开模式
    • ios::in 输入(读)模式打开文件
    • ios:: out 输出(写)模式打开文件
    • ios::app 追加模式打开文件
    • ios::trunc 若文件已经存在则清空文件的模式打开文件
    • ios::binary 二进制方式打开文件
    • 混合使用
    fstream file;
    file.open(“example”, ios::out | ios::app | ios::binary);
    
  • 打开文件时,没有指定打开模式时,使用默认的打开方式;
ofstream: ios::out | ios::trunc
ifstream: ios::in
fstream:  ios::in | ios::out
  • 文件关闭
fsream file;
file.open(xxxxxxxxxxxxxxxxxx)
if(file!=NULL)
    cout<<"open failed"<<endl;
file.close();
  • 开关文件练习
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
    /*ofstream ofile("/home/sunsuhui/1612/11/11.16/ssh1");
    ofile<<"pear"<<" "<<4.5;
    ofile.close();*/ //打开文件并输入
    /*ifstream ifile("/home/sunsuhui/1612/11/11.16/ssh1");
    char text[20];
    double price;
    ifile>>text>>price;
    cout<<text<<" "<<price;
    ifile.close();*/  //打开文件输出 
    /*ofstream ofile("/home/sunsuhui/1612/11/11.16/ssh2",ios::out|ios::binary);
    char temp[20]="hello";
    ofile.write(temp,20);
    ofile.close();*/
    ifstream ifile("/home/sunsuhui/1612/11/11.16/ssh2",ios::in|ios::binary);
    char temp[20];
    ifile.read(temp,20);
    cout<<temp<<endl;
    ifile.close();
}
  • 文件输出
#include <iostream>
#include <iomanip>
using namespace std;
ostream& tab(ostream& output)
{
    return output<<'\t';
}
int main()
{
    cout<<'a'<<tab<<'b'<<'\t'<<'c'<<endl;
}
  • 文件指针
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream ifile("/home/sunsuhui/1612/11/11.16/1.txt");
    if(NULL==ifile)
    {
        cout<<"打开文件失败"<<endl;
        return -1;
    }
    //定位指针 -get(读指针)
    ifile.seekg(0,ios::end);
    //指针位置函数-get指针(渎指针)
    cout<<"get point position:"<<ifile.tellg()<<endl;
    ifile.close();
}

相关文章

网友评论

      本文标题:C++基础-(IO和文件操作)

      本文链接:https://www.haomeiwen.com/subject/yualpttx.html