c++文件

 写文件:

 文件的作用:存储数据

头文件:#include

ftell() 函数用于得到文件位置指针当前位置相对于文件首的偏移字节数;

fseek()函数用于设置文件指针的位置;

rewind()函数用于将文件内部的位置指针重新指向一个流(数据流/文件)的开头;

ferror()函数可以用于检查调用输入输出函数时出现的错误。

文件分类:

1.文本文件: 以ASCLL码存储在电脑上

2.二进制文件:以二进制数存储在电脑上

操作:

ofstream 写操作
ifstream 读操作
fstream 读写操作

打开方式:

使用时可以配合使用:  ios::in | ios::out

ios::in 读文件而打开
ios::out 写文件而打开
ios::ate 初始位置:文件尾
ios::app 追加的方式写文件
ios::trunc 如果文件存在先删除再创建
ios::binary 二进制方式

写文件:

1.头文件: #include

2.创建流对象: ofstream ofs;

3.打开文件: ofs.open("文件路径",打开方式);

4.写数据: ofs<<"写入数据";

5.关闭文件: ofs.close();

#include
//头文件
#include
using namespace std;
int main()
{
	//创建流对象
	ofstream ofs;
	//打开文件
	//写入操作
	ofs.open("text.txt", ios::out);
	//写内容
	ofs << "故辞";
	//关闭文件
	ofs.close();
	system("pause");
	return 0;
}

运行结果:

c++文件_第1张图片

右键cpp文件,打开文件夹 

c++文件_第2张图片

 系统会把文件创建到 项目文件夹中

c++文件_第3张图片

 打开text文件

c++文件_第4张图片

读文件:

 1.头文件: #include

2.创建流对象:ifstream ifs;

3.打开文件并判断文件是否打开成功

ifs.is_open();

ifs.open("文件路径",打开方式)

读数据

第一种:


	char c[50] = { 0 };
	//读入数据
	while (ifs >> c)
	{
		cout << c << endl;
	}

第二种:

	char c1[50] = { 0 };
	//一行一行添加
	while (ifs.getline(c1,sizeof(c1)))
	{
		cout << c1 << endl;
	}

第三种:

添加头文件:#include 

	string s;
	while (getline(ifs,s))
	{
		cout << s << endl;
	}

 第四种方式:

	char a;
	while ((a = ifs.get()) != EOF)//EOF  end of file
	{
		cout << a;
	}

5.关闭文件:ifs.close();

void text()
{
	//创建流对象
	ifstream ifs;
	ifs.open("text.txt", ios::in);
	if (!ifs.is_open())
	{
		cout << "文件打开失败" << endl;
		return;
	}
	//读数据
	//第一种
	char c[50] = { 0 };
	//读入数据
	while (ifs >> c)
	{
		cout << c << endl;
	}
	//第二种
	char c1[50] = { 0 };
	//一行一行添加
	while (ifs.getline(c1,sizeof(c1)))
	{
		cout << c1 << endl;
	}
	//第三种
	string s;
	while (getline(ifs,s))
	{
		cout << s << endl;
	}
	//第四种方式
	char a;
	while ((a = ifs.get()) != EOF)//EOF  end of file
	{
		cout << a;
	}
	//关闭文件
	ifs.close();
}

二进制文件:

 打开方式:ios::blnary

写文件:

write函数所在的头文件为

write有两种用法。一种是:

ssize_twrite(int handle, void *buf, int nbyte);

handle 是文件描述符;

buf是指定的缓冲区,即指针,指向一段内存单元;

nbyte是要写入文件指定的字节数;返回值:写入文档的字节数(成功);-1(出错)

write函数把buf中nbyte写入文件描述符handle所指的文档,成功时返回写的字节数,错误时返回-1.

另一种是:write(const char* str,int n)

str是字符指针或字符数组,用来存放一个字符串。n是int型数,它用来表示输出显示字符串中字符的个数。

write("string",strlen("string");表示输出字符串常量

利用函数流对象:ostream&write(const char *buffer,int elem)

buffer 是指向内存中的存储空间

elem 是读取的字节数

class person
{
public:
	person()
	{}
	person(string name, int age)
	{
		Name = name;
		Age = age;
	}
	string Name;
	int Age;

};
	person p("故辞", 5);
	//创建流对象
	ofstream ofs;
	//打开文件
	ofs.open("perosn.txt", ios::out | ios::binary);
	//写入文件
	ofs.write((const char*)&p, sizeof(person));
	//关闭文件
	ofs.close();

运行结果:

c++文件_第5张图片

读文件:

istream& read( char* buffer,int elem)

buffer:指的是指向内存中的一段内存

elem:读写的字节数

void text1()
{
	//创建流对象
	ifstream ifs;
	//打开文件
	ifs.open("perosn.txt", ios::in | ios::binary);
	//是否打开成功
	if (!ifs.is_open())
	{
		cout << "打开失败" << endl;
		return;
	}
	//读文件
	person p1;
	ifs.read((char*)&p1, sizeof(person));
	cout << p1.Name << "  " << p1.Age;

}

运行结果:

c++文件_第6张图片

C++移动和获取文件读写指针(seekp、seekg、tellg、tellp)

seekg (int x, mode) 设置文件读指针的位置
seekp(int x , mode) 设置文件写指针的位置
tellg() 返回文件读指针的位置
tellp () 返回文件写指针的位置
  1. x 表示起始的位置
  2. mode指的是文件读写指针的设置模式
ios::beg 从文件开始向后的  x  字节处  (x只能为非负数)
ios::cur x 为负数则表示从当前位置朝文件开头方向移动 x字节,为正数则表示从当前位置朝文件尾部移动 x字节,为 0 则不移动
ios::end 让文件指向从文件结尾往前的 |x|(x 的绝对值)字节处。在此情况下,offset 只能是 0 或者负数。

 参考文献:

C++移动和获取文件读写指针(seekp、seekg、tellg、tellp) (biancheng.net)

你可能感兴趣的:(C/C++,c++)