知道流的概念,会使用fstream来对文件进行操作
一、流的概念
1.什么是流
1.流是一种抽象的概念,表示了数据的无结构化传递
2.c++流是指:
信息从外部输入设备(如键盘)向计算机内部(如内存)输入和
从内存向外部输出设备(显示器)输出的过程
3.c++定义有了I/O标准类库,用以完成流操作的功能
二、fstream的作用
ifstream 对文件输入(读文件) input file stream
ofstream 对文件输出(写文件)output file stream
fstream 对文件输入或输出 file stream
ofstream的默认打开方式是, 截断式写入 ios::out | ios::trunc
fstream的默认打开方式是, 不截断式写入 ios::out
1.fstream的使用
file stream //文件流
1.fstream是c++中常用的文件操作类,用于文件操作,和C语言学过的文件操作的作用一样
2.c++文件操作
1.包含头文件fstream,并打开命名空间std或者使用std::fstream
2.使用fstream类来实例化对象,通过对象的成员来进行文件操作
3.常用的成员函数
1.open()
open("文件名路径",打开方式)//打开文件
文件路径:绝对路径,相对路径
打开方式:表示打开文件的模式,不同的模式对应不同的数据操作(只读,只写,二进制操作等)
模式标志 | 描述 |
---|---|
ios::in | 读方式打开文件,文件不存在,则打开错误 |
ios:out | 写方式打开文件,文件不存在,则新建文件,文件原来就存在,则打开时清除原来的内容 |
ios::trunc | 打开文件时会清空内存存储的所有数据,单独使用时和 ios::out相同 |
ios::app | 打开文件,在尾部添加数据,文件不存在,则新建该文件 |
ios::ate | 打开文件,将文件读指针指向文件末尾,文件不存在,则打开错误 |
ios::binary | 以二进制方式打开文件,如果不指定该模式,则默认以文本模式打开 |
以上打开方式, 可以使用位操作 | 组合起来
2.close()
关闭文件
3.is_open()
判断文件是否成功打开,成功返回1,失败返回0
4.eof()
判断文件是否到达文件末尾,到了文件末尾返回true,失败返回false
char str[1024] = { 0 };
int i = 0;
//以只读方式打开文件
fstream file("1.txt", ios::in);
while (!file.eof())//判断文件是否到达末尾,到了返回true,否则返回false
{
file.get(str[i++]);
}
cout << str << endl;
5.put()
往文件中写入一个字符
#include<fstream>
fstream file;
file.open("1.txt",ios::out);
file.put('w');
char c='s';
file.put(c);
6.get()和getline()
从文件中读取字符或字符串
//从文件中读取字符,有三种常用的形式
char ch;
file.get(ch);//读取一个字符,赋值给ch
ch=file.get();//读取一个字符,赋值给ch
get(char* str,int num,char delim='\n');//读取num个字符,赋值给str,或在这个期间读到'\n'结束
getline(char* str,int num,char delim='\n');//从一行数据中读取num个字符,赋值给str,或在这个期间读到了'\n'结束
get()和getline()的区别:
都可以读取多个字符,get会把文件内遇到终结符留在输入流,所以需要使用get将终结符扔掉。getline自动把输入流中的终结符取消掉了,下次可以继续直接读取。读一行字符时,一般使用getline
class myfstream :public fstream//可以自己添加功能
{
public:
};
int main()
{
fstream file("1.txt", ios::in);//打开文件用于读取数据,如果文件不存在,则打开错误
//读取一行字符
char str[100] = { 0 };
file >> str;//中间遇到空格就读取完成
file.getline(str, 100, 'd');//读取100个字符到str中(读的只是一行),遇到d就停止,最后一个参数也可以不写,有默认值\n
cout << str << endl;
//读取多行字符
char str[2][100] = { 0 };
file.getline(str[0], 100);
file.getline(str[1], 100);
cout << str[0] << endl;
cout << str[1] << endl;
file.close();
system("pause");
return 0;
}
7.seekp()和seekg()
1.seekg( off_type offset,ios::seekdir origin ); //起始位置
作用:设置输入流的位置
参数1: 偏移量
参数2: 相对位置
2.seekp()
作用:设置输出流的位置
文件指针的移动
//1.txt里面存的是 abcde
fstream file("1.txt", ios::in);
//设置输出流的位置
file.seekp(3, ios::cur);
cout << (char)file.get() << endl;//d
file.close();
file.seekp(5,ios::beg);//从文件开头的位置往后偏移5个字符
在这里,数字5,表示的是文件指针往后移动5个字节的位置,如果是-5,那么就是往前移动5个字节的位置
第二个参数是文件指针从哪里移动位置,一共有三个:
ios::beg-->文件开头
ios::end-->文件末尾
ios::cur-->文件指针当前的位置
8.tellg()和tellp()
返回该输入流的当前位置(距离文件的起始位置的偏移量)
获取文件指针的移动大小,文件指针可以移动
int len = file.tellg();//获取文件指针移动了多少
//2.png的大小是33016字节
fstream file("2.png", ios::binary|ios::in);
file.seekp(0, ios::end);
int size = file.tellp();
cout << size << endl;//33016
file.close();
9.write()写入文件
write(const char* str,int str_size);//写操作,向文件中写入str,大小为str_size
int a=10;
file.write((const char*)&a,sizeof(int));
10.read()读取文件
read(char* str,int str_size);//读操作,从文件中读取数据到str,一共读取str_size大小
int a;
file.read((char*)&a,sizeof(int));//读取4个字节的整数,赋值给a
11.二进制形式读写文件
//写入一个数字
int a = 10;
//写文件
fstream file("1.txt", ios::binary | ios::out);//以二进制方式打开文件,若不指定此模式,则以文本模式打开。再用于写入数据
//将a写入文件
file.write((const char*)(&a), sizeof(int));
//关闭文件
file.close();
//读文件
fstream file("1.txt", ios::binary | ios::in);
//从文件读取数据到a中
file.read((char*)&a, sizeof(int));
cout << a << endl;
file.close();
//一个数字,异或同一个数两次,会得到它本身 45^98^98=45
//写文件
fstream file("1.txt", ios::binary | ios::out);
int b = 0;
char str[100] = "adjddss";
b = strlen(str);//大小为7
file.write((const char*)(&b), sizeof(int));
file.write(str, b);
file.close();
b = 0;
char str1[100] = { 0 };
//读文件
fstream file("1.txt", ios::binary | ios::in);
file.read((char*)&b, sizeof(int));
cout << b << endl;
file.read(str1, b);
cout << str << endl;
file.close();
12.文本形式写入文件
#include<iostream>
#include<fstream>
using namespace std;
/*
文件操作可以一边读一边写,但是不建议这样做(文件指针分不清)
读写分开进行
打开方式要和操作方式一样,以只读的方式打开,那么就只读,写是不行的
*/
int main()
{
fstream file;
file.open("1.txt", ios::out);//打开文件用于写入数据,如果文件不存在,则新建该文件,如果文件原来存在,则打开时清除原来的内容
if (file.is_open())//打开成功返回1,失败返回0
cout << "打开成功" << endl;
else
cout << "打开失败" << endl;
//写入字符
file.put('s');
char c = 'b';
//读取一个字符到c中
file.put(c);
//写入字符串
file << "awnjankjnks教你带你";//重载了<<运算符的
file.close();//关闭文件
system("pause");
return 0;
}
三、使用重载的<<>>
1、使用运算符来进行文件操作的优点类似于I/O流中的cin和cout,只不过cin和cout作用在内存,而fstream是作用在文件
//定义一个file文件对象
fstream file;
int x=666;
//读操作
file.open("1.txt",ios::out);
//写入文件
file<<"输入输出"<<endl;
file<<x<<endl;
file.close();
//读文件
file.open("1.txt",ios::in);
char str[1024];
int temp;
//从文件中读取数据到str和temp中
file>>str>>temp;
cout<<str<<temp<<endl;
四、对文件流按格式读写
使用stringstream
1.写文件
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string name;
int age;
//读文件
ofstream outfile;
outfile.open("user.txt", ios::out | ios::trunc);
while (1) {
cout << "请输入姓名: [ctrl+z退出] ";
//使用ctrl+z,文件会接收到结束信号
cin >> name;
if (cin.eof()) { //判断文件是否结束
break;
}
cout << "请输入年龄: ";
cin >> age;
//stringstream格式化读入文件的数据
stringstream s;
s << "name:" << name << "\t\tage:" << age << endl;
//s.str()——返回的是一个string类型的数据
outfile << s.str();
}
// 关闭打开的文件
outfile.close();
system("pause");
return 0;
}
2.读文件
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <Windows.h>
using namespace std;
int main(void)
{
char name[32];
int age;
string line;
//读文件
ifstream infile;
infile.open("user.txt");
while (1) {
//获取inline文件中的一行数据写到line中
getline(infile, line);
if (infile.eof()) { //判断文件是否结束
break;
}
//ssanf_s格式化读出来的数据
sscanf_s(line.c_str(), "姓名:%s 年龄:%d", name, sizeof(name),&age);
cout << "姓名:" << name << "\t\t年龄:" << age << endl;
}
infile.close();
system("pause");
return 0;
}