C++学习笔记基础篇30——文件IO

写文件例子

常用写文件运算符<<,put,write

#include <fstream>
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
	ofstream fs;
	fs.open("d:\\132.txt");//没有文件会自动生成
	if (!fs)
	{ 
		cout << "无法打开文件" << endl; 
		return 0; 
	}
	fs << 2000 << endl;
	fs << 1.25 << endl;
	fs << "hello" << endl;
	fs.put('y');//只能写一个字符
	char nn[] = "\nddadd\n";
	fs.write(nn,sizeof(nn));//地址,长度,,写进去的默认是二进制
	fs.close();
	system("pause");
	return 0;
}

读文件例子

常用读文件运算符>>,get,getline

int main(int argc, char* argv[])
{
	ifstream fs;
	fs.open("d:\\132.txt");//没有文件会自动生成
	if (!fs)
	{ 
		cout << "无法打开文件" << endl; 
		return 0; 
	}
	//int abc = 0;
	//fs >> abc;
	//cout << abc << endl;
	//float abcd = 0;
	//fs >> abcd;
	//cout << abcd << endl;
	//char szbuf[100] = { 0 };
	//fs >> szbuf;
	//cout << szbuf << endl;
	//char ch = fs.get();
	//cout << ch << endl;
	//char ch1 = fs.get();
	//cout << ch1 << endl;
	//char ch2 = fs.get();
	//cout << ch2 << endl;
	char szbuf[100] = { 0 };
	fs.getline(szbuf,100);
	cout << szbuf << endl;
	memset(szbuf, 0, 100);//清空缓冲区
	fs.getline(szbuf, 100);
	cout << szbuf << endl;
	fs.close();
	system("pause");
	return 0;
}

检测读取是否成功,是否到末尾

①、文件读写是否成功?
可以使用 good、bad、fail 来进行判断!
fail() 方法用于判断最后一次读取数据的时候是否遇到了类型不配的情况,若是返回true(如果遇到了EOF,该方法也返回true)
bad() 如果出现意外的问题,如文件受损或硬件故障,最后一次读取数据的时候发生了这样的问题,方法 bad() 将返回true。
good() 该方法在没有发生任何错误的时候返回true。该方法也指出的最后一次读取输入的操作是否成功。

②、读取文件的时候是否已经读到文件末尾?
可以使用 eof 来进行判断!eof() 方法用于判断最后一次读取数据的时候是否遇到EOF,即到达文件末尾,若是则返回true。

#include <fstream>
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
	//开始写
	ofstream fs_write;
	fs_write.open("d:\\132.txt");//没有文件会自动生成
	if (!fs_write)
	{ 
		cout << "无法打开文件" << endl; 
		return 0; 
	}
	int var = 2000;
	fs_write << var << endl;
	fs_write << var + 1 << endl;
	fs_write.close();
	//开始读
	ifstream fs_read;
	fs_read.open("d:\\132.txt");
	if (!fs_read)return 0;
	var = 0;
	fs_read >> var;

	bool read_is_good=fs_read.good();//返回值是bool类型,判断是否读取成功
	bool end_of_file = fs_read.eof();//返回值是bool类型,判断是否文件末尾
	cout << "read_is_good=" << read_is_good<<endl;

	cout << var << endl;
	fs_read >> var;

	read_is_good = fs_read.good();
	cout << "read_is_good=" << read_is_good << endl;

	cout << var << endl;
	fs_read.close();

	system("pause");
	return 0;
}

文件指针

文件流提供以下成员函数来读取或配置文件指针

tellg() 返回读取文件指针的当前位置
tellp() 返回写入文件指针的当前位置
seekg(指针偏移量) 将读取文件指针移到指定位置
seekg(指针偏移量,参照位置) 将读取文件指针移到指定位置
seekp(指针偏移量) 将写入文件指针移到指定位置
seekp(指针偏移量,参照位置) 将写入文件指针移到指定位置

ios::beg 文件开头计算偏移量
ios::cur 文件当前位置计算偏移量
ios::end 文件结尾计算偏移量

例子

#include <fstream>
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
	//开始写
	ofstream fs_write;
	fs_write.open("d:\\132.txt");//没有文件会自动生成
	if (!fs_write)
	{ 
		cout << "无法打开文件" << endl; 
		return 0; 
	}
	int var = 2000;
	fs_write << var << endl;
	fs_write << var + 123 << endl;
	fs_write.close();
	//开始读
	ifstream fs_read;
	fs_read.open("d:\\132.txt");
	if (!fs_read)return 0;
	var = 0;
	int read_pstr = fs_read.tellg();
	cout << "read_pstr=" << read_pstr << endl;//0
	fs_read >> var;
	read_pstr = fs_read.tellg();
	cout << "read_pstr=" <<  read_pstr  << endl;//4

	//fs_read.seekg(read_pstr+3);//文件指针向后3位
	fs_read.seekg(0, ios::cur);

	cout << var << endl;
	fs_read >> var;
	read_pstr = fs_read.tellg();
	cout << "read_pstr=" << read_pstr << endl;//10  有换行

	cout << var << endl;
	fs_read.close();

	system("pause");
	return 0;
}

利用ios::end求文件大小。

#include <fstream>
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
	//开始写
	ofstream fs_write;
	fs_write.open("d:\\132.txt");//没有文件会自动生成
	if (!fs_write)
	{ 
		cout << "无法打开文件" << endl; 
		return 0; 
	}
	int var = 2000;
	fs_write << var << endl;
	fs_write << var + 123 << endl;
	fs_write.close();
	//开始读
	ifstream fs_read;
	fs_read.open("d:\\132.txt");
	if (!fs_read)return 0;

	fs_read.seekg(0, ios::end);//利用ios::end求文件大小
	int file_size = fs_read.tellg();
	cout << "file_size "<< file_size << endl;
	fs_read.close();

	system("pause");
	return 0;
}

实现文件的拷贝功能

#include <fstream>
#include<iostream>
using namespace std;
void filecopy(const char* des_file,const char* src_file)
{
	//实现文件的拷贝功能
	//des_file为目标文件,src_file为源文件
	//读取源文件
	ifstream fs_read;
	fs_read.open(src_file, ios::binary);//这里必须要用二进制读取文档,不然会出现乱码,无法读取/r
	if (!fs_read){ cout << "源文件打开失败" << endl; return ; }
	fs_read.seekg(0, ios::end);
	int src_file_len = fs_read.tellg();
	cout << "文件大小为" << src_file_len << endl;
	fs_read.seekg(0, ios::beg);//文件指针记得要指回来
	char* file_char = new char[src_file_len];
	fs_read.read(file_char, src_file_len);
	fs_read.close();
	//写入目标文件
	ofstream fs_write;
	fs_write.open(des_file, ios::binary);
	if (!fs_write){ cout << "目标文件打开失败" << endl; return; }
	fs_write.write(file_char, src_file_len);
	/*fs_write << file_char << endl;*/
	cout << "写入成功" << endl;
	fs_write.close();
	delete[]file_char;
}
int main(int argc, char* argv[])
{
	char* src_file = "d:\\123.txt";
	char* des_file = "d:\\789.txt";
	filecopy(des_file, src_file);
	system("pause");
	return 0;
}

1. read必须要用二进制读取文档,不然会出现乱码,无法读取/r
2. fs_read.seekg(0,ios::end);改变文件指针之后,记得改回要读的地址。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值