C++面向对象入门(四十六)文件流

C++中的文件: C++中, 文件被看作一个字符序列, 即文件是由一个个的字符顺序构成的. 即把文件视为一个字符流, 不考虑记录的
界限, 这种文件又被称为流式文件.
文件的分类:
<1> 按数据的存储方式分类:
文本文件: 每个字节存放一个ASCII码表示一个字符的文件
优点:可以字节按字符形式输出, 便于阅读
二进制文件: 将数据的内部存储形式原样保存的文件
优点: 与数据在内存中的存储形式一致, 存储效率高, 无需进行存储形式的转换
<2>按照存取方式分类:
顺序文件
随机读写文件
C++中文件既可以顺序访问, 也可以随机访问
C++对文件的抽象: C++把文件定义为一个文件流类的对象,
C++中要对文件进行输入输出的步骤:
1 创建一个文件流对象
2 将文件流对象与指定文件相关联(打开文件)
3 进行读/写操作
4 关闭文件
文件流(Text Stream): 以外存文件为输入输出对象的数据流, 输出文件流是从内存流向外存文件的数据, 输入文件流是外存文件
流向内存的数据, 每个文件流都在内存有对应的缓冲区与之对应
C++文件流的分类:
ifstream: 输入文件流
ofstream: 输出文件流
fstream: 输入输出文件流
注意事项:
1 均在头文件fstream.h中定义, 欲使用任何一种文件流, 必须包含头文件fstream.h
文件的打开和关闭:
1 打开文件: 使一个文件流对象和一个文件相关联, 在文件流创建后或创建时必须打开文件, 然后才能进行读写
打开文件的两种方法:
<1> 先建立文件流对象, 使用open()函数连接外部文件
语法:
流类 对象名;
对象名.open(文件名, 打开方式);
<2> 调用流类带参数的构造函数, 建立对象的同时连接外部文件
语法:
流类 对象名(文件名, 打开方式);
注意事项:
1 "文件名"是用字符串表示的外部文件的名字, 可以是已赋值的字符串变量或是用双引号括起来的串常量, 要求使用文件全名
2 "文件名"字符串中的"/"必须使用转义字符'//'
3 "打开方式"是ios定义的标识常量, 表示文件的打开方式, 多种打开方式使用'|'分隔

 

 

注意事项:
对于fstream对象
ios::app与ios::out配合:没有文件会创建新文件, 如果文件存在, 写入内容会追加在已有的内容之后, 不能读内容
ios::app与ios:in配合: 如果文件不存在, 会创建新文件, 可以写入内容, 如果文件存在, 会打开原有文件, 可以在文件末尾追加内容, 不能读内容
ios::ate与ios::out配合: 没有文件会创建新文件, 如果文件存在, 写入内容会覆盖已有内容, 不能读
ios::ate与ios:in配合:  如果文件不存在, 不会创建新文件, 如果文件存在, 会打开原有文件, 不能写入内容, 能读
对于ofstream对象
ios::app与ios::out配合: 没有文件会创建新文件, 如果文件存在, 写入内容会追加在已有的内容之后
ios::app与ios::in配合: 没有文件会创建新文件, 如果文件存在, 则写入内容追加在已有内容后
ios::ate与ios::out配合: 没有文件会创建新文件, 如果文件存在, 写入内容覆盖原有内容
ios::ate与ios::in配合:  没有文件打开失败, 如果文件存在, 则写入内容追加在已有内容后
对于ifstream对象
ios::app与ios::out配合, 没有文件会创建新文件, 如果文件存在能打开成功, 能够读取
ios::app与ios::in配合: 没有文件会创建新文件, 如果文件存在,能打开成功, 能够读取
ios::ate与ios::out配合: 没有文件打开文件失败, 如果文件存在能打开成功, 能读
ios::ate与ios::in配合: 没有文件打开文件失败, 如果文件存在,能打开成功, 能读
总结:
不能读的情况:
fstream--ios::ate--ios::out
不能创建文件的情况:
fstream--ios::ate--ios::in
ifstream--ios::ate--ios::out|ios::in
写入覆盖原有内容的情况:
fstream--ios::ate--ios::out
ostream--ios::ate--ios::out
不能写的情况:
fstream--ios::ate--ios::in
2 关闭文件: 即使打开的文件与流对象"脱钩", 可使用close()函数进行关闭
语法:
<流对象名>.close();
注意事项:
1 通过打开文件和关闭文件, 可使一个流对象和多个文件关联
2 在任何时候, 一个流对象只能和一个文件相关联, 即一个流对象打开了一个文件后, 在未关闭当前关联的文件前,
不能打开其他文件
文件的读写
文件读写的分类:
1 按照读写文件的存储形式分类:
<1> 文本文件的读写: 用默认方式打开, 我们把描述一个对象的信息称为一个记录, 文本文件本身没有记录逻辑结构,
为了便于识别, 文本文件通常把一个记录放一行(使用换行符分隔的逻辑行). 记录的每个数据项之间可以使用空白符,
换行符或制表符等作为分隔符
<2> 二进制文件的读写: 使用ios::binary方式打开文件, 二进制文件的读写方式完全由程序控制, 一般的字处理软件
不能参与编辑. 最好使用istream的函数read()和ostream的函数write()分别来读/写二进制文件, 特别是结构型数据,
这样有助于维护文件数据的安全性和易访问性.
2 按照数据存取方式分类:
<1> 顺序读写: 以ios::app以外的方式打开文件, 会从文件头读, 顺序读写, 同时, ios::ate读文件也是从文件头读的
文件打开后, 系统自动生成两个隐含指的流指针-- 读指针和写指针. 以追加方式打开文件(app)打开文件时,
流指针指向文件末尾, 其他方式打开, 流指针指向文件第一个字节. 文件的读写从流指针指向的位置开始, 每完成一次读\
写操作后, 流指针自动移动到下一个读写分量的起始位置.
<2> 随机读写: 为了增加文件访问的灵活性, 在C++的文件流中定义了几个可随机移动读/写指针的成员函数, 从而实现对
文件的随机读写.
有关读指针的函数:
1 移动读指针函数
istream& istream::seekg(streampos pos); :
该函数的功能是将文件的指针移动到pos指定的位置中
istream& istream::seekg(streamoff offset, seek_dir origin);:
该函数的功能是读指针从origin指定的起始位置, 将指针移动offset(偏移量)个字节数. 当origin为ios::beg时, offset的值
为正数; 当origin为ios::end时, offset的值为负数; 当origin为Ios::cur时, offset的值可以为正数, 也可以为负数
origin的类型seek_dir是一个枚举类型, 有三种取值
ios::beg表示指针的起始位置为文件头
ios::cur表示指针的起始位置为当前位置
ios::end表示指针的起始位置为文件尾
offset两端类型streamoff与long等价, pos的类型streampos也与long等价
2 返回读指针当前指向的位置值函数
streampos istream::tellg();:
该函数的功能是确定文件指针的当前位置
有关写指针的函数
1 移动写指针的函数
ostream& ostream::seekp(streampos pos); :
该函数的功能是将文件的指针移动到pos指定的位置中
ostream& ostream::seekp(streamoff offset, seek_dir origin); :
该函数的功能是写指针从origin指定的起始位置, 将指针移动offset(偏移量)个字节数. 当origin为ios::beg时, offset的值
为正数; 当origin为ios::end时, offset的值为负数; 当origin为Ios::cur时, offset的值可以为正数, 也可以为负数
2 返回写指针当前指向的位置值函数
streampos ostream::tellp();
以上函数末尾的p是put(输出/写), g是get(输入/读), 便于理解和记忆
注意: 某些操作系统中流指针只有一个--既是读指针, 又是写指针
 
 
代码示例:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

/*
C++中的文件: C++中, 文件被看作一个字符序列, 即文件是由一个个的字符顺序构成的. 即把文件视为一个字符流, 不考虑记录的
界限, 这种文件又被称为流式文件.

文件的分类:
<1> 按数据的存储方式分类:
文本文件: 每个字节存放一个ASCII码表示一个字符的文件
优点:可以字节按字符形式输出, 便于阅读

二进制文件: 将数据的内部存储形式原样保存的文件
优点: 与数据在内存中的存储形式一致, 存储效率高, 无需进行存储形式的转换

<2>按照存取方式分类:
顺序文件
随机读写文件

C++中文件既可以顺序访问, 也可以随机访问

C++对文件的抽象: C++把文件定义为一个文件流类的对象, 

C++中要对文件进行输入输出的步骤:
1 创建一个文件流对象
2 将文件流对象与指定文件相关联(打开文件)
3 进行读/写操作
4 关闭文件

文件流(Text Stream): 以外存文件为输入输出对象的数据流, 输出文件流是从内存流向外存文件的数据, 输入文件流是外存文件
流向内存的数据, 每个文件流都在内存有对应的缓冲区与之对应

C++文件流的分类:
ifstream: 输入文件流
ofstream: 输出文件流
fstream: 输入输出文件流

注意事项:
1 均在头文件fstream.h中定义, 欲使用任何一种文件流, 必须包含头文件fstream.h

文件的打开和关闭:
1 打开文件: 使一个文件流对象和一个文件相关联, 在文件流创建后或创建时必须打开文件, 然后才能进行读写
打开文件的两种方法:
<1> 先建立文件流对象, 使用open()函数连接外部文件
语法:
流类 对象名;
对象名.open(文件名, 打开方式);

<2> 调用流类带参数的构造函数, 建立对象的同时连接外部文件
语法:
流类 对象名(文件名, 打开方式);

注意事项:
1 "文件名"是用字符串表示的外部文件的名字, 可以是已赋值的字符串变量或是用双引号括起来的串常量, 要求使用文件全名
2 "文件名"字符串中的"/"必须使用转义字符'//'
3 "打开方式"是ios定义的标识常量, 表示文件的打开方式, 多种打开方式使用'|'分隔

注意事项:
对于fstream对象
ios::app与ios::out配合:没有文件会创建新文件, 如果文件存在, 写入内容会追加在已有的内容之后, 不能读内容
ios::app与ios:in配合: 如果文件不存在, 会创建新文件, 可以写入内容, 如果文件存在, 会打开原有文件, 可以在文件末尾追加内容, 不能读内容
ios::ate与ios::out配合: 没有文件会创建新文件, 如果文件存在, 写入内容会覆盖已有内容, 不能读
ios::ate与ios:in配合:  如果文件不存在, 不会创建新文件, 如果文件存在, 会打开原有文件, 不能写入内容, 能读

对于ofstream对象
ios::app与ios::out配合: 没有文件会创建新文件, 如果文件存在, 写入内容会追加在已有的内容之后
ios::app与ios::in配合: 没有文件会创建新文件, 如果文件存在, 则写入内容追加在已有内容后
ios::ate与ios::out配合: 没有文件会创建新文件, 如果文件存在, 写入内容覆盖原有内容
ios::ate与ios::in配合:  没有文件打开失败, 如果文件存在, 则写入内容追加在已有内容后

对于ifstream对象
ios::app与ios::out配合, 没有文件会创建新文件, 如果文件存在能打开成功, 能够读取
ios::app与ios::in配合: 没有文件会创建新文件, 如果文件存在,能打开成功, 能够读取
ios::ate与ios::out配合: 没有文件打开文件失败, 如果文件存在能打开成功, 能读
ios::ate与ios::in配合: 没有文件打开文件失败, 如果文件存在,能打开成功, 能读

总结:
不能读的情况:
fstream--ios::ate--ios::out
不能创建文件的情况:
fstream--ios::ate--ios::in
ifstream--ios::ate--ios::out|ios::in
写入覆盖原有内容的情况:
fstream--ios::ate--ios::out
ostream--ios::ate--ios::out
不能写的情况:
fstream--ios::ate--ios::in

2 关闭文件: 即使打开的文件与流对象"脱钩", 可使用close()函数进行关闭
语法:
<流对象名>.close();

注意事项:
1 通过打开文件和关闭文件, 可使一个流对象和多个文件关联
2 在任何时候, 一个流对象只能和一个文件相关联, 即一个流对象打开了一个文件后, 在未关闭当前关联的文件前,
不能打开其他文件

文件的读写
文件读写的分类:
1 按照读写文件的存储形式分类:
<1> 文本文件的读写: 用默认方式打开, 我们把描述一个对象的信息称为一个记录, 文本文件本身没有记录逻辑结构,
为了便于识别, 文本文件通常把一个记录放一行(使用换行符分隔的逻辑行). 记录的每个数据项之间可以使用空白符,
换行符或制表符等作为分隔符
<2> 二进制文件的读写: 使用ios::binary方式打开文件, 二进制文件的读写方式完全由程序控制, 一般的字处理软件
不能参与编辑. 最好使用istream的函数read()和ostream的函数write()分别来读/写二进制文件, 特别是结构型数据,
这样有助于维护文件数据的安全性和易访问性.

2 按照数据存取方式分类:
<1> 顺序读写: 以ios::app以外的方式打开文件, 会从文件头读, 顺序读写, 同时, ios::ate读文件也是从文件头读的 
文件打开后, 系统自动生成两个隐含指的流指针-- 读指针和写指针. 以追加方式打开文件(app)打开文件时,
流指针指向文件末尾, 其他方式打开, 流指针指向文件第一个字节. 文件的读写从流指针指向的位置开始, 每完成一次读\
写操作后, 流指针自动移动到下一个读写分量的起始位置.
<2> 随机读写: 为了增加文件访问的灵活性, 在C++的文件流中定义了几个可随机移动读/写指针的成员函数, 从而实现对
文件的随机读写.

有关读指针的函数:
1 移动读指针函数
istream& istream::seekg(streampos pos); :
该函数的功能是将文件的指针移动到pos指定的位置中
istream& istream::seekg(streamoff offset, seek_dir origin);:
该函数的功能是读指针从origin指定的起始位置, 将指针移动offset(偏移量)个字节数. 当origin为ios::beg时, offset的值
为正数; 当origin为ios::end时, offset的值为负数; 当origin为Ios::cur时, offset的值可以为正数, 也可以为负数
origin的类型seek_dir是一个枚举类型, 有三种取值
ios::beg表示指针的起始位置为文件头
ios::cur表示指针的起始位置为当前位置
ios::end表示指针的起始位置为文件尾
offset两端类型streamoff与long等价, pos的类型streampos也与long等价

2 返回读指针当前指向的位置值函数
streampos istream::tellg();:
该函数的功能是确定文件指针的当前位置

有关写指针的函数
1 移动写指针的函数
ostream& ostream::seekp(streampos pos); :
该函数的功能是将文件的指针移动到pos指定的位置中
ostream& ostream::seekp(streamoff offset, seek_dir origin); :
该函数的功能是写指针从origin指定的起始位置, 将指针移动offset(偏移量)个字节数. 当origin为ios::beg时, offset的值
为正数; 当origin为ios::end时, offset的值为负数; 当origin为Ios::cur时, offset的值可以为正数, 也可以为负数

2 返回写指针当前指向的位置值函数
streampos ostream::tellp();

以上函数末尾的p是put(输出/写), g是get(输入/读), 便于理解和记忆

注意: 某些操作系统中流指针只有一个--既是读指针, 又是写指针
*/

class student
{
private:
    long num;
    string name;
    float score;
public:
    //输入学号
    void setNum();

    //输入姓名
    void setName();

    //输入成绩
    void setScore();

    //得到学号
    long getNum();

    //得到姓名
    string getName();

    //得到成绩
    float getScore();
};

/*
读写文本文件
*/
void textFileIO1();

/*
fstream 与ios::app:out
*/
void textFileIO2();

/*
fstream 与ios::app与ios::in
*/
void textFileIO3();

/*
ofstream 与ios::app:与ios:out
*/
void textFileIO4();

/*
ofstream 与ios::app:与ios:in
*/
void textFileIO5();

/*
ifstream 与ios::app:与ios:out
*/
void textFileIO6();

/*
ifstream 与ios::app:与ios:in
*/
void textFileIO7();

/*
fstream 与ios::ate:与ios:out
*/
void textFileIO8();

/*
fstream 与ios::ate:与ios:in
*/
void textFileIO9();

/*
ofstream 与ios::ate:与ios:out
*/
void textFileIO10();

/*
ofstream 与ios::ate:与ios:in
*/
void textFileIO11();

/*
ifstream 与ios::ate:与ios:out
*/
void textFileIO12();

/*
ifstream 与ios::ate:与ios:in
*/
void textFileIO13();

/*
顺序读写二进制文件
*/
void binaryFileIO1();

/*
随机读写二进制文件
*/
void binaryFileIO2();
int main()
{
    //textFileIO();

    //textFileIO2();

    //textFileIO3();

    //textFileIO4();

    //textFileIO5();

    //textFileIO6();

    //textFileIO7();

    //textFileIO8();

    //textFileIO9();

    //textFileIO10();

    //textFileIO11();

    //textFileIO12();

    //textFileIO13();

    //binaryFileIO1();

    binaryFileIO2();
    system("pause");
}

void textFileIO1()
{
    char str[10];
    int i = 0;
    cout << "输入一个字符串(最多含九个字符):" << endl;
    //输入一个字符串
    cin.get(str, 10, '!');
    //以输出方式打开字符串
    ofstream outfile("No64_textfile.txt");
    if (!outfile)
    {
        cout << "File cannot be opened." << endl;
        return ;
    }
    char c = str[i];
    while (i < cin.gcount())
    {
        if (isdigit(c))
        {
            //将数组串存入文件outfile
            outfile << c;
        }

        if (isdigit(c) && !isdigit(str[i]))
        {
            outfile << endl;
        }

        i++;
        c = str[i];
    }
    outfile << '\0';
    outfile.close();

    //以输入方式打开文件
    ifstream infile("No64_textfile.txt");
    if (!infile)
    {
        cout << "File cannot be opened." << endl;
        return;
    }
    cout << "存入No64_textfile.txt文件中的数字串为:" << endl;
    //从文件中读取数据, 并输出到屏幕上
    while (!infile.eof())
    {
        //从文件中读取一行数据
        infile.getline(str, sizeof(str));
        cout << str << endl;
    }
    infile.close();
    return;
}

void textFileIO2()
{
    //先手动删除No64_textfile.txt
    //ios::app与ios::out 配合
    fstream out("No64_textfile.txt", ios::app | ios::out);
    if (!out)
    {
        cout << "when file not exist, ios::app and ios::out cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::app and ios::out can opened file(first create a new file)." << endl;
        out << "hello world ";
        
    }
    out.close();
    out.open("No64_textfile.txt", ios::app | ios::out);
    out << "hello world ";
    out.close();
    //No64_textfile.txt:hello world hello world
    out.open("No64_textfile.txt", ios::app | ios::out);
    char ch;
    out.get(ch);
    cout << (int)ch << endl;
    out.close();
    //-52(文件结束符ASCII码)
    //对于fstream对象
    //ios::app与ios::out配合
    //没有文件会创建新文件
    //如果文件存在, 写入内容会追加在已有的内容之后
    //不能读内容
}

void textFileIO3()
{
    
    //先手动删除No64_textfile.txt
    fstream in("No64_textfile.txt", ios::app | ios::in);
    if (!in)
    {
        cout << "when file not exist, ios::app and ios::in cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::app and ios::in can opened file(first create a new file)." << endl;
        in << "hello world ";
    }
    in.close();
    in.open("No64_textfile.txt",ios::app | ios::in);
    in << "hello world";
    in.close();
    //No64_textfile.txt:hello world hello world
    in.open("No64_textfile.txt", ios::app | ios::out);
    char ch;
    in.get(ch);
    cout << (int)ch << endl;
    in.close();
    //-52(文件结束符)
    //对于fstream对象
    //ios::app与ios:in配合
    //如果文件不存在, 会创建新文件, 可以写入内容
    //如果文件存在, 会打开原有文件, 可以在文件末尾追加内容
    //不能读内容
}

void textFileIO4()
{
    //先手动删除No64_textfile.txt
    ofstream out("No64_textfile.txt", ios::app | ios::out);
    if (!out)
    {
        cout << "when file not exist, ios::app and ios::out cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::app and ios::out can opened file(first create a new file)." << endl;
        out << "hello world ";

    }
    out.close();
    out.open("No64_textfile.txt", ios::app | ios::out);
    out << "hello world ";
    out.close(); 
    //No64_textfile.txt:hello world hello world

    //对于ofstream对象
    //ios::app与ios::out配合
    //没有文件会创建新文件
    //如果文件存在, 写入内容会追加在已有的内容之后
}

void textFileIO5()
{
    //先手动删除No64_textfile.txt
    ofstream out("No64_textfile.txt", ios::app | ios::in);
    if (!out)
    {
        cout << "when file not exist, ios::app and ios::in cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::app and ios::in can opened file(first create a new file)." << endl;
        out << "hello world ";
    }
    out.close();
    out.open("No64_textfile.txt", ios::app | ios::in);
    out << "hello world ";
    out.close();
    //No64_textfile.txt:hello world hello world

    //对于ofstream对象
    //ios::app与ios::in配合
    //没有文件会创建新文件
    //如果文件存在, 则写入内容追加在已有内容后
}

void textFileIO6()
{
    //先手动删除No64_textfile.txt
    ifstream out("No64_textfile.txt", ios::app | ios::out);
    if (!out)
    {
        cout << "when file not exist, ios::app and ios::out cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::app and ios::out can opened file(first create a new file)." << endl;
        //out << "hello world ";
        //ifsteam和输出相关的函数, 不能输出
    }
    out.close();
    out.open("No64_textfile.txt", ios::app | ios::out);
    char ch;
    out.get(ch);
    cout << (int)ch << endl;
    out.close();
    //对于ifstream对象
    //ios::app与ios::out配合
    //没有文件会创建新文件
    //如果文件存在能打开成功
    //能够读取
}

void textFileIO7()
{
    //先手动删除No64_textfile.txt
    ifstream out("No64_textfile.txt", ios::app | ios::in);
    if (!out)
    {
        cout << "when file not exist, ios::app and ios::in cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::app and ios::in can opened file(first create a new file)." << endl;
        //out << "hello world ";
        //out << "hello world ";
        //ifsteam和输出相关的函数, 不能输出
    }
    out.close();
    out.open("No64_textfile.txt", ios::app | ios::out);
    char ch;
    out.get(ch);
    cout << (int)ch << endl;
    out.close();
    //对于ifstream对象
    //ios::app与ios::in配合
    //没有文件会创建新文件
    //如果文件存在,能打开成功
    //能够读取
}

void textFileIO8()
{
    //先手动删除No64_textfile.txt
    //ios::ate与ios::out 配合
    fstream out("No64_textfile.txt", ios::ate | ios::out);
    if (!out)
    {
        cout << "when file not exist, ios::ate and ios::out cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::ate and ios::out can opened file(first create a new file)." << endl;
        out << "hello world ";

    }
    out.close();
    //No64_textfile.txt:hello world 
    out.open("No64_textfile.txt", ios::ate | ios::out);
    out << "new";
    out.close(); 
    out.open("No64_textfile.txt", ios::app | ios::out);
    char ch;
    out.get(ch);
    cout << (int)ch << endl;
    out.close();
    //No64_textfile.txt:new

    //对于fstream对象
    //ios::ate与ios::out配合
    //没有文件会创建新文件
    //如果文件存在, 写入内容会覆盖已有内容
    //不能读
}

void textFileIO9()
{
    //先手动删除No64_textfile.txt

    //创建文件
    system("cd ..");
    system("fsutil file createnew No64_textfile.txt 0");
    fstream in("No64_textfile.txt", ios::ate | ios::in);
    if (!in)
    {
        cout << "when file not exist, ios::ate and ios::in cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::ate and ios::in can opened file(first create a new file)." << endl;
        in << "hello world ";
    }
    in.close();
    in.open("No64_textfile.txt", ios::app | ios::in);
    char ch;
    in.get(ch);
    cout << ch << endl;
    in.close();
    //对于fstream对象
    //ios::ate与ios:in配合
    //如果文件不存在, 不会创建新文件
    //如果文件存在, 会打开原有文件, 不能写
    //能读
}

void textFileIO10()
{
    //先手动删除No64_textfile.txt
    ofstream out("No64_textfile.txt", ios::ate | ios::out);
    if (!out)
    {
        cout << "when file not exist, ios::ate and ios::out cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::ate and ios::out can opened file(first create a new file)." << endl;
        out << "hello world ";

    }
    out.close();
    //No64_textfile.txt:hello world
    out.open("No64_textfile.txt", ios::ate | ios::out);
    out << "new ";
    out.close();
    //No64_textfile.txt:new

    //对于ofstream对象
    //ios::ate与ios::out配合
    //没有文件会创建新文件
    //如果文件存在, 写入内容覆盖原有内容
}

void textFileIO11()
{
    //先手动删除No64_textfile.txt

    /*system("cd ..");
    system("fsutil file createnew No64_textfile.txt 0");*/
    ofstream out("No64_textfile.txt", ios::ate | ios::in);
    if (!out)
    {
        cout << "when file not exist, ios::ate and ios::in cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::ate and ios::in can opened file(first create a new file)." << endl;
        out << "hello world ";
    }
    out.close();
    //No64_textfile.txt:hello world
    out.open("No64_textfile.txt", ios::ate | ios::in);
    out << "new";
    out.close();
    //No64_textfile.txt:hello world new

    //对于ofstream对象
    //ios::ate与ios::in配合
    //没有文件打开失败
    //如果文件存在, 则写入内容追加在已有内容后
}

void textFileIO12()
{
    //先手动删除No64_textfile.txt

    system("cd ..");
    system("fsutil file createnew No64_textfile.txt 0");
    ifstream out("No64_textfile.txt", ios::ate | ios::out);
    if (!out)
    {
        cout << "when file not exist, ios::ate and ios::out cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::ate and ios::out can opened file(first create a new file)." << endl;
        //out << "hello world ";
        //ifsteam和输出相关的函数, 不能输出
    }
    out.close();
    out.open("No64_textfile.txt", ios::app | ios::out);
    char ch;
    out.get(ch);
    cout << (int)ch << endl;
    out.close();
    //对于ifstream对象
    //ios::ate与ios::out配合
    //没有文件打开文件失败
    //如果文件存在能打开成功
    //能读
}

void textFileIO13()
{
    //先手动删除No64_textfile.txt
    system("cd ..");
    system("fsutil file createnew No64_textfile.txt 0");
    ifstream out("No64_textfile.txt", ios::ate | ios::in);
    if (!out)
    {
        cout << "when file not exist, ios::ate and ios::in cannot opened file." << endl;
    }
    else
    {
        cout << "when file not exist, ios::ate and ios::in can opened file(first create a new file)." << endl;
        //out << "hello world ";
        //out << "hello world ";
        //ifsteam和输出相关的函数, 不能输出
    }
    out.close();
    out.open("No64_textfile.txt", ios::app | ios::out);
    char ch;
    out.get(ch);
    cout << (int)ch << endl;
    out.close();
    //对于ifstream对象
    //ios::ate与ios::in配合
    //没有文件打开文件失败
    //如果文件存在,能打开成功
    //能读
}

void binaryFileIO2()
{
    fstream file;
    file.open("No64_binaryfile2.txt", ios::in | ios::out | ios::binary |ios::trunc);
    if (!file)
    {
        cout << "file No64_binaryfile2.txt cannot be opened." << endl;
        return;
    }
    student stu[100], s;
    cout << "首先输入三个学生信息:" << endl;
    cout << "学号\t姓名\t成绩" << endl;
    for (size_t i = 0; i < 3; i++)
    {
        //输入学号
        stu[i].setNum();
        //输入姓名
        stu[i].setName();
        //输入成绩
        stu[i].setScore();
        file.write((char *)&(stu[i]), sizeof(student));
    }

    cout << "在在第二个学生后插入两个学生信息:" << endl;
    cout << "学号\t姓名\t成绩" << endl;
    file.seekp(sizeof(student) * 2);
    for (size_t i = 3; i < 5; i++)
    {
        //输入学号
        stu[i].setNum();
        //输入姓名
        stu[i].setName();
        //输入成绩
        stu[i].setScore();
        file.write((char *)&(stu[i]), sizeof(student));
    }
    cout << "输出第2, 4个学生信息:" << endl;
    cout << "学号\t姓名\t成绩" << endl;
    file.seekg(sizeof(student) * 1, ios::beg);
    file.read((char *)&s, sizeof(student));
    cout << s.getNum() << "\t";
    cout << s.getName() << "\t";
    cout << s.getScore() << "\n";
    file.seekg(sizeof(student) * 3, ios::beg);
    file.read((char *)&s, sizeof(student));
    cout << s.getNum() << "\t";
    cout << s.getName() << "\t";
    cout << s.getScore() << "\n";
    file.close();
    return;
}

void binaryFileIO()
{
    ofstream outfile("No64_binaryfile.txt", ios::binary);
    if (!outfile)
    {
        cout << "File No64_binaryfile.txt cannot be opened" << endl;
        return;
    }
    student stu[100];
    char ch;
    int i = 0;
    while (true)
    {
        cout << "你想要输入更多记录吗(y/n)?" << endl;
        cin >> ch;
        if (ch == 'n' || ch == 'N')
        {
            break;
        }
        i++;
        stu[i].setNum();
        stu[i].setName();
        stu[i].setScore();
        outfile.write((char *)&stu[i],sizeof(student));
    }
    outfile.close();
    cout << "********输入结束********" << endl;
    cout << "你想看文件内容吗(y/n)?" << endl;
    cin >> ch;
    if (ch == 'y' || ch == 'Y')
    {
        ifstream infile("No64_binaryfile.txt", ios::binary);
        if (!infile)
        {
            cout << "File No64_binaryfile.txt cannot be opened." << endl;
            return;
        }
        cout << "学号" << "\t姓名" << "\t成绩" << endl;
        i = 1;
        infile.read((char *)&stu[i], sizeof(student));
        while (infile)
        {
            cout << stu[i].getNum() << "\t";
            cout << stu[i].getName() << "\t";
            cout << stu[i].getScore() << endl;
            i++;
        infile.read((char *)&stu[i], sizeof(student));
        }
        infile.close();
    }
    return;
}

void student::setNum()
{
    cout << "请输入学号:" << endl;
    cin >> num;
}

void student::setName()
{
    cout << "请输入姓名" << endl;
    cin >> name;
}

void student::setScore()
{
    cout << "请输入成绩:" << endl;
    cin >> score;
}

long student::getNum()
{
    return num;
}

string student::getName()
{
    return name;
}

float student::getScore()
{
    return score;
}

 

posted @ 2020-09-08 20:34  DNoSay  阅读(429)  评论(0编辑  收藏  举报