ofstream fout ,ifstream fin学习笔记

1.ofstream,open,close写入文件


#include<iostream>
#include<fstream>
using namespace std;
//通过ofstream的方式实现写入文件 open,close
int main()
{
    ofstream fout;  //ofstream输出文件
    // ofstream fout("number.txt"); 自动创建一个文件
    fout.open("../pose.txt");//打开文件
    fout << "1234abcdef";//写入文件
    fout.close();
}

通过这些代码向文件1.txt中输入文件,但是会覆盖原来的文件。

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

#include<iostream>
#include<fstream>
using namespace std;
//通过ifstream流读取文件,并将文件写入str中
int main()
{
    ifstream fin("../pose.txt");//创建读取文件的流
    char str[50] = { 0 };
    fin >> str;//读取
    fin.close();
    cout << str;
    cin.get();
}

由于之前对pose.txt进行修改,pose的内容是:123456abcdef

2的输出是:123456abcdef

3.按照行来读取数据

#include<iostream>

#include<fstream>
using namespace std;
//按照行来读取
int main()
{
    //按照行来读取
    ifstream fin("../pose.txt");
    //读取4行数据
    for (int i = 0; i < 4;i++)
    {
        char str[50] = { 0 };
        fin.getline(str, 50);
        cout << str << endl;
    }
    fin.close();
    cin.get();
}
转自:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/toto1297488504/article/details/38948391
  • 17
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值