C++ fstream/ifstream/ofstream 类用于进行文件操作

一、ofstream  // 输出文件流,向文件写内容

#include<iostream>
#include <fstream>
using namespace std;
int main() 
{
	ofstream file1("ofstream.txt");
	file1 << "hello world";
	file1.close();
}

二、ifstream  // 输入文件流,从已有的文件读

#include<iostream>
#include <fstream>
using namespace std;
int main() 
{
	char ch;
	ifstream file1("ofstream.txt");
	while (file1.get(ch))
	{
		cout << ch;
	}
	file1.close();
}

三、fstream  // 文件流,打开文件供读写

打开模式:
        ios::app    打开文件用于追加,不存在则创建,存在不清空    写在文件尾,读在文件头
        ios::ate    打开时定义到文件末尾
        ios::binary    以二进制模式进行读写
        ios::in    以读权限打开文件,不存在则失败,存在不清空   
        ios::out    以写权限打开文件,不存在则创建,存在则清空  
        ios::trunc    打开文件时清空

构造函数或成员函数 open 用于打开文件
good成员函数检查流是否可用
eof成员函数用于输入流是否结束(检测文件是否读结束)

 

>>操作符用于从文件中读取数据到变量
<<操作符用于输出数据到文件

file>>char *;

file>>char;

file.get(char);

file.get(char *,int);

file.getline(char *,int sz);

 file.getline(char *,int sz,char eol);

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

int main()
{
	fstream fs;
	fs.open("test1.txt",ios::out);
	if(fs.good())
	{
		cout << "打开文件成功" << endl;
	}
	else
	{
		cout << "打开文件失败" << endl;
	}
	int i=9;
	while(i--)
	{
		fs << "hello" << " " << 100 << endl;
	}
}
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	fstream fs("test1.txt",ios::in);
	if(fs.good())
	{
		cout << "打开文件成功" << endl;
	}
	else
	{
		cout << "打开文件失败" << endl;
	}
    //方法一
    string str;
	int num;
	fs >> str;
	fs >> num;
	cout << str << "--" << num << endl;
    //方法二
	char ch;
	while((ch=fs.get())!=EOF)
	{
		cout<<ch;
	}
    //方法三
    char c[20];
	while(fs.get(c,20,'\0')!=NULL)
	{
		cout<<c;
	}
    //方法四
    char c[20];
	while(!fs.eof())
	{
		fs.read(c,20);
		cout.write(c,fs.gcount());
	}
}

格式化控制函数,类似:左对齐、右对齐、宽度、填充、小数点位数。

二进制读写:成员函数 read/write
        read(char_type *__s,streamsize __n)
        gcount成员函数可以获取上次流的二进制读写操作的字节数

        write(char_type *__s,streamsize __n)
        good 成员函数可以获取到写操作是否成功

使用二进制读写方式写的复制文件的操作:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
	ifstream fin("test.txt", ios::in|ios::binary);
	if (!fin)
	{
		cout << "File open error!\n";
		return -1;
	}
	ofstream fout("test2.txt", ios::binary);
	char c[1024];
	while (!fin.eof())
	{
		fin.read(c, 1024);
		fout.write(c, fin.gcount());
	}
	fin.close();
	fout.close();
}

随机读写:seekp(off_type,ios_base::seekdir)
    功能:设置文件的位置指针
    off_type:偏移值
        正值向右,负值向左
    seekdir:基础位置
        ios::beg    文件开头
        ios::cur    文件当前位置
        ios::end    文件末尾

    获取文件位置指针:tellp
    该成员函数返回当前文件流的位置指针(字节数)
    也可以借助此函数获取文件的大小

 // 调整文件的位置指针到末尾
    fs.seekp(0,ios::end);
    cout << "文件的总字节数:" << fs.tellp() << endl;

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值