• 【C++ 标准流,文件流】


    ■ 标准输入,输出流,

    iostream 标准库,它提供了 cin 和 cout 方法

    ■ 文件流(ofstream写入,ifstream读取,fstream创建-写入-读取)

    1. C++ 源代码文件中包含头文件 <iostream><fstream>2. 
    
    • 1
    • 2
    数据类型描述
    ofstream该数据类型表示输出文件流,用于创建文件并向文件写入信息。
    ifstream该数据类型表示输入文件流,用于从文件读取信息。
    fstream该数据类型通常表示文件流,且同时具有 ofstream 和 ifstream 两种功能,这意味着它可以创建文件,向文件写入信息,从文件读取信息。

    ■ open()

    void open(const char *filename, ios::openmode mode);

    模式标志描述
    ios::app追加模式。所有写入都追加到文件末尾。
    ios::ate文件打开后定位到文件末尾。
    ios::in打开文件用于读取。
    ios::out打开文件用于写入。
    ios::trunc如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0。

    ■ ofstream

    以写入模式打开文件,并希望截断文件,以防文件已存在

    ofstream outfile;
    outfile.open("file.dat", ios::out | ios::trunc );
    
    • 1
    • 2

    ■ ifstream

    打开一个文件用于读写

    ifstream  afile;
    afile.open("file.dat", ios::out | ios::in );
    
    • 1
    • 2

    ■ 流插入<<

    流插入运算符( << )向文件写入信息

    ifstream  afile;
    afile.open("file.dat", ios::out | ios::in );
    
    • 1
    • 2

    示例一:

    #include 
    #include 
    using namespace std;
     
    int main ()
    {
        
       char data[100];
     
       // 以写模式打开文件
       ofstream outfile;
       outfile.open("afile.dat");
     
       cout << "Writing to the file" << endl;
       cout << "Enter your name: "; 
       cin.getline(data, 100);
     
       // 向文件写入用户输入的数据
       outfile << data << endl;
     
       cout << "Enter your age: "; 
       cin >> data;
       cin.ignore();
       
       // 再次向文件写入用户输入的数据
       outfile << data << endl;
     
       // 关闭打开的文件
       outfile.close();
     
       // 以读模式打开文件
       ifstream infile; 
       infile.open("afile.dat"); 
     
       cout << "Reading from the file" << endl; 
       infile >> data; 
     
       // 在屏幕上写入数据
       cout << data << endl;
       
       // 再次从文件读取数据,并显示它
       infile >> data; 
       cout << data << endl; 
     
       // 关闭打开的文件
       infile.close();
     
       return 0;
    }
    当上面的代码被编译和执行时,它会产生下列输入和输出:
    
    $./a.out
    Writing to the file
    Enter your name: Zara
    Enter your age: 9
    Reading from the file
    Zara
    9
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    ■ 文件位置指针

    istream 和 ostream 都提供了用于重新定位文件位置指针的成员函数。
    这些成员函数包括关于
    istream 的 seekg(“seek get”)
    ostream 的 seekp(“seek put”)
    seekg 和 seekp 的参数通常是一个长整型。

    // 定位到 fileObject 的第 n 个字节(假设是 ios::beg)
    fileObject.seekg( n );
     
    // 把文件的读指针从 fileObject 当前位置向后移 n 个字节
    fileObject.seekg( n, ios::cur );
     
    // 把文件的读指针从 fileObject 末尾往回移 n 个字节
    fileObject.seekg( n, ios::end );
     
    // 定位到 fileObject 的末尾
    fileObject.seekg( 0, ios::end );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    基于Java+Swing实现俄罗斯方块游戏
    【matplotlib 实战】--柱状图
    LeetCode——面试题 01.02.判定是否互为字符重排
    Redis 主从模式
    vue轮播图使用swiper插件
    【python笔记】第八节 函数基础
    C# Thread.Sleep(0)有什么用?
    【虚拟线程】
    6.27 JAVA笔试题
    Python一炮句搞定网页登录验证码自动输入
  • 原文地址:https://blog.csdn.net/sinat_23896491/article/details/136417601