c++学习标准I/O库、文件操作

标准I/O库

标准输入输出

输入输出对象 istream cin ostream cout

输入
输出

格式化输入输出

  • cin >> //输入
  • cout << //输出
  • hex、oct、dec和setbase
  • 浮点精度precision / setprecision
  • 域宽width / setw
  • 用户自定义流操纵算子-’\n’ ‘\t’ ‘\r’
格式化输出:
  • hex、oct、dec和setbase

进制

  • 浮点精度precision / setprecision

浮点

  • 域宽width / setw

域宽

  • 用户自定义流操纵算子-’\n’ ‘\t’ ‘\r’

自定义流操纵算子

非格式化输入/输出

//输出
ostream& put ( char c ); //写一个字符出去
ostream& write ( const char* s , streamsize n );//写指定长度的字符串出去

//输入
int get();
istream& get ( char& c );
istream& get ( char* s, streamsize n );
istream& get ( char* s, streamsize n, char delim );
istream& get ( streambuf& sb);
istream& get ( streambuf& sb, char delim ); 

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

istream& read ( char* s, streamsize n );

 

非格式化输出:

非格式化输出
 

非格式化输入:read() / getline() 同get()

非格式输入0
非格式输入1
非格式输入2
非格式输入3

#include <iostream>
using namespace std;

int main()
{
//    cout.put('h').put('i') << endl;
//    cout.write("hello",2).write("world",20) << endl;

//    cout << cin.get() << endl;

//    char str;
//    cin.get(str);
//    cout << str << endl;

//    char str1[10];
//    cin.get(str1,6);
//    cout << str1 << endl;

    char str1[10];
    cin.get(str1,6,' ');
    cout << str1 << endl;

    return 0;
}

 
 

文件操作

文件继承类
文件操作
 

文件的打开方式:(ios = ios_base)(默认in|out)
  • ios_base::app:文件末尾处添加
  • ios_base::ate:文件末尾处添加
  • ios_base::binary:文件以二进制的形式打开
  • ios_base::in:文件以写入的方式打开
  • ios_base::out:文件以读出的方式打开
  • ios_base::trunc:文件以擦除重写的方式打开

在这里插入图片描述
 

文件位置指针

  用于指示读写操作所在的下一个字节号;是个整数值,指定文件中离文件开头的相对位置(也称为离文件开头的偏移量) 。

//获取文件指针
fstream file;
...
file.tellp();
file.tellg();

//文件指针的重新定位
file.seekp(10,ios::beg);
file.seeekg(10,ios::end);
  • ios::beg(默认)相对于流的开头定位
  • ios::cur相对于流当前位置定位
  • ios::end相对于流结尾定位
streampos ostream::tellp ( ); //返回值可赋给long类型的变量
ostream& ostream::seekp ( streampos pos );
ostream& ostream::seekp ( streamoff off, ios_base::seekdir dir ); 

streampos istream::tellg ( );
istream& istream::seekg ( streampos pos );
istream& istream::seekg ( streamoff off, ios_base::seekdir dir );
//一个文件有一个指针,输入/输出复用

 

ios::operator! 与 ios::operator void*

ios中的重载

#include <iostream>
#include <iomanip>
using namespace std;

#if 0//进制的格式化输出
int main()
{
    int number;

    cout << "Enter a dec number: ";
    cin >> number;

    cout << hex << number << endl;
    cout << oct << number << endl;
    cout << dec << number << endl;
    cout << setbase(8) << number << endl;

    //hex oct dec setbase(8/10/16)修改了数据的格式
    //在修改格式之前,原来的格式将一直有效
    cout << number << endl;

    return 0;
}
#endif // 0

#if 0//浮点的精度格式化输出
int main()
{
    double root2 = 122.61241232;
//    cout << fixed;

    for (int places = 0; places <= 9;places++ )
    {
        cout.precision( places );
        cout << root2 << endl;
//        cout << setprecision(places) << root2 << endl;
    }
    return 0;
}

#endif // 0

#if 0//设置域宽的格式化输出
int main()
{
    int widthValue = 4;
    char str[10];

    cout << "Enter a str:" << endl;
    cin.width( 5 );

    while (cin >> str)
    {
        cout.width( widthValue++ );
        cout << right << str << endl;
        //cout<<setw(widthValue++) << str << endl;
        cin.width( 5 );
    }

    return 0;
}
#endif // 1

#if 0//自定义流的格式化输出
ostream &endLine(ostream &out)
{
    out << '\n';
    return out;
}

int main()
{
   cout << "HelloWorld" << endLine << "!!!" << endLine;
   return 0;
}
#endif // 0

#if 0//非格式化输出
int main()
{
//    cout.put('h').put('i') << endl;
//    cout.write("hello",2).write("world",20) << endl;

//    cout << cin.get() << endl;

//    char str;
//    cin.get(str);
//    cout << str << endl;

//    char str1[10];
//    cin.get(str1,6);
//    cout << str1 << endl;

    char str1[10];
    cin.get(str1,6,' ');
    cout << str1 << endl;

    return 0;
}
#endif // 0

#if 0//文件操作
#include <fstream>
int main()
{
    fstream file("hello.txt",ios::app|ios::in|ios::out);
    if (!file)
    {
        cout << "File could not be opened" << endl;
        return 0;
    }
    file << "hello_world" << endl;
    file.close();

    char str[50];
    file.open("hello.txt",ios::app|ios::in|ios::out);
    if (!file.is_open())
    {
        cout << "File could not be opened" << endl;
        return 0;
    }
    file >> str;
    cout << str << endl;
    file.close();

    return 0;
}

#endif // 0
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值