文件操作ofstream,open,close,ifstream,fin,按照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件



1.ofstream,open,close 写入文件

#include<iostream>

#include<fstream>

 

using namespace std;

 

//通过ofstream的方式实现写入文件 open,close

void main()

{

    ofstream fout;  //ofstream输出文件

    fout.open("E:\\1.txt");//打开文件

    fout << "1234abcdef";//写入文件

    fout.close();

}

通过这些代码向文件1.txt中输入文件

2.ifstream,fin 从文件中读取文件并打印输出到屏幕

#include<iostream>

#include<fstream>

 

using namespace std;

 

//通过ifstream流读取文件,并将文件写入str

void main()

{

    ifstream fin("E:\\1.txt");//创建读取文件的流

    char str[50] = { 0 };

    fin >> str;//读取

    fin.close();

    cout << str;

    cin.get();

}

运行结果是,输出:1234abcdef

3.按照行来读取数据

#include<iostream>

#include<fstream>

 

using namespace std;

 

//按照行来读取

void main()

{

    //按照行来读取

    ifstream fin("E:\\1.txt");

    //读取4行数据

    for (int i = 0; i < 4;i++)

    {

        char str[50] = { 0 };

        fin.getline(str, 50);

        cout << str << endl;

    }

    fin.close();

    cin.get();

}

上面结果是输出4行。

4.fout文件输入

#include<iostream>

#include<fstream>

 

using namespace std;

 

void main()

{

    ofstream fout;//ofstream.输出文件

    fout.open("E:\\2.txt");//打开文件

    fout << "锄禾日当午" << endl;//写入文件

    fout << "地雷买下土" << endl;//写入文件

    fout << "谭胜来跳舞" << endl;//写入文件

    fout << "炸成250" << endl;//写入文件

    fout.close();

}

5. fstreamios::in | ios::out表示有读写的权限,通过fstream.getline(写入位置,写入长度)

案例(写入文件又读取文件的方式):

#include<iostream>

#include<fstream>

 

using namespace std;

 

//通过fstream的方式实现文件读写,要注意的是这种方式要求文件已经存在

void main()

{

    //ios::in | ios::out表示有读写的权限

    fstream fio("E:\\3.txt", ios::in | ios::out);

    fio << "锄禾日当午" << endl;//写入文件

    fio << "地雷买下土" << endl;//写入文件

    fio << "谭胜来跳舞" << endl;//写入文件

    fio << "炸成250" << endl;//写入文件

    fio.close();

    {

        fstream fio("E:\\3.txt", ios::in | ios::out);

        for (int i = 0; i < 4; i++)

        {

            char str[50] = { 0 };

            fio.getline(str, 50);

            cout << str << endl;

        }

        fio.close();

    }

    cin.get();

}

6.fio.seekg();随机文件指针,将文件指针移动到指定位置开始读写文件

案例如下:

#include<iostream>

#include<fstream>

 

using namespace std;

 

//通过fstream的方式实现文件读写,要注意的是这种方式要求文件已经存在

void main()

{

    //ios::in | ios::out表示有读写的权限

    fstream fio("E:\\3.txt", ios::in | ios::out);

    fio << "锄禾日当午" << endl;//写入文件

    fio << "地雷买下土" << endl;//写入文件

    fio << "谭胜来跳舞" << endl;//写入文件

    fio << "炸成250" << endl;//写入文件

    fio.close();

    fio.seekg(ios::beg); //文件指针,从文件开头开始执行

    {

        fstream fio("E:\\3.txt", ios::in | ios::out);

        for (int i = 0; i < 4; i++)

        {

            char str[50] = { 0 };

            fio.getline(str, 50);

            cout << str << endl;

        }

        fio.close();

    }

    cin.get();

}

7.文件写入和文件读写

#include<iostream>

#include<fstream>

 

using namespace std;

 

void main()

{

    ofstream fout;

    fout.open("E:\\4.txt");

    //将内容打印到文件

    fout << "ABC" << " " << 123 << " " << 'ch' << endl;

    fout.close();

    ifstream fin("E:\\4.txt");

    char str[10] = { 0 };//读取字符串

    int num = 0;

    char ch = '\0';

    fin >> str >> num >> ch;

    std::cout << str << "\n" << num << "\n" << ch;

 

    std::cin.get();

}

运行结果:

ABC

123

8.文件拷贝

#include<iostream>

#include<fstream>

 

using namespace std;

 

//读写一个字符

//文本与二进制存储

void main()

{

    ifstream fin("E:\\4.txt");//创建读取文件的流

    ofstream fout("E:\\40.txt");

    if (!fin || !fout)

    {

        std::cout << "文件打开失败";

        return;

    }

    std::cout << "文件拷贝开始\n";

    char ch = 0;

    //引用的方法读取到一个字符

    while (fout && fin.get(ch))

    {

        fout.put(ch);//写入一个字节

    }

    fin.close();

    fout.close();

 

    std::cout << "文件拷贝完成";

    cin.get();

}

运行结果:生成了一个40.txt,里面存储的是4.txt中的内容

9.文件追加ios:app

#include<iostream>

#include<fstream>

 

using namespace std;

 

void main()

{

    ofstream fout("E:\\40.txt", ios::app);//追加

    fout << "天下英雄,谭胜第一\n";

    fout.close();

 

    cin.get();

}

发现在40.txt这个文件的最后面多了:天下英雄,谭胜第一

 

 

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值