【C++】C++中的文件IO
C++中的文件IO
- 1. 相关的类
- 2.相关的成员函数
- (1) 打开文件
- 写法一:open
- 写法二:fout
- (2) 读取文件
- 写法一: get
- 示例代码:get读取文件
- 写法二:read
- 示例代码:文件的读取
- 示例代码:文件的循环读取
- 写法三: getline //读取一行数据
- 示例代码:
- (3) 写入文件
- 写法一:put
- 写法二:write
- 示例代码:文件的写入
- (4)文件关闭
- (5)设置读写偏移--》随机读写文件(不按照顺序)
- (6)如何判断文件打开/新建是否成功
- (7)gcount()方法获取读到的字节数
- 示例代码:
1. 相关的类
#include <fstream>
fstream //跟文件的读写有关 ifstream //input file stream读取文件读取文件用到的一些函数,其实cin也使用了(ifstream是istream的子类)ofstream //output file stream写入文件写入文件用到的一些函数,其实cout也使用了(ofstream是ostream的子类)
2.相关的成员函数
(1) 打开文件
写法一:open
ifstream fin;
fin.open(); //没有新建功能,文件不存在打开就失败
void open (const char* filename, ios_base::openmode mode = ios_base::in);参数: filename --》路径名mode --》ios_base::in 可读ios_base::out 可写
写法二:fout
ofstream fout("1.txt"); //可以不用调用open,创建对象的同时,顺便把文件打开(清空打开,会清空源文件)//具备新建功能
或者:
ofstream fout;fout.open("1.txt",ios_base::out) 会清空源文件fout.open("1.txt",ios_base::out|ios_base::in)
(2) 读取文件
写法一: get
istream& get (char& c);
istream& get (char* s, streamsize n); //只读第一行
示例代码:get读取文件
#include <iostream>
#include <fstream> //跟文件IO有关的头文件
#include "myhead.h"
using namespace std;/*文件的读取
*/int main()
{char buf[5]={0};int ret;//创建ifstream的对象,通过这个对象来调用方法实现文件的读取 ifstream in;//打开你要读取的文件in.open("./1.txt");if(!in.is_open()){cout<<"打开文件失败"<<endl;}//一个个字节读取//ret=in.get();bzero(buf,5);in.get(buf,5);cout<<"刚才读取的内容是: "<<buf<<endl;in.get(buf,5);cout<<"刚才读取的内容是: "<<buf<<endl;in.get(buf,5);cout<<"刚才读取的内容是: "<<buf<<endl;in.get(buf,5);cout<<"刚才读取的内容是: "<<buf<<endl;in.get(buf,5);cout<<"刚才读取的内容是: "<<buf<<endl;//关闭文件in.close();return 0;
}
写法二:read
read (char* s, streamsize n);
fin.read(); //读取多个字节
示例代码:文件的读取
#include <iostream>
#include <fstream> //跟文件IO有关的头文件using namespace std;int main()
{//创建ifstream的对象,通过这个对象来调用方法实现文件的读取 ifstream in;//打开你要读取的文件in.open("./1.txt");if(!in.is_open()){cout<<"打开文件失败"<<endl;}//读取文件内容char buf[100]={0};in.read(buf,100);cout<<"刚才读取的内容是: "<<buf<<endl;//关闭文件in.close();return 0;
}
示例代码:文件的循环读取
#include <iostream>
#include <fstream> //跟文件IO有关的头文件
#include "myhead.h"using namespace std;int main()
{char buf[100]={0};//创建ifstream的对象,通过这个对象来调用方法实现文件的读取 ifstream in;//打开你要读取的文件in.open("./1.txt");if(!in.is_open()){cout<<"打开文件失败"<<endl;}while(1){//读取文件内容bzero(buf,100);in.read(buf,100);cout<<"刚才读取的内容是: "<<buf<<endl;if(in.eof())break;}//关闭文件in.close();return 0;
}
写法三: getline //读取一行数据
istream& getline (char* s, streamsize n ); //回车作为行的结束
istream& getline (char* s, streamsize n, char delim );第三个参数:delim 让程序员指定新的字符作为行结束的标记如果你没有指定,默认把回车换行作为行结束的标记
示例代码:
#include <iostream>
#include <fstream> //跟文件IO有关的头文件
#include "myhead.h"
using namespace std;int main()
{char buf[100]={0};//创建ifstream的对象,通过这个对象来调用方法实现文件的读取 ifstream in;//打开你要读取的文件in.open("./1.txt");if(!in.is_open()){cout<<"打开文件失败"<<endl;}while(1){//按行读取文件内容bzero(buf,100);in.getline(buf,100);//in.getline(buf,100,'@'); //指定@作为读取结束标记cout<<"刚才读取的内容是: "<<buf<<endl;if(in.eof())break;}//关闭文件in.close();return 0;
}
(3) 写入文件
写法一:put
ostream& put (char c);
写法二:write
ostream& write (const char* s, streamsize n);
示例代码:文件的写入
#include <iostream>
#include <fstream> //跟文件IO有关的头文件
#include "myhead.h"
using namespace std;/*文件的写入
*/int main()
{char buf[5]={0};int ret;//创建ofstream的对象,通过这个对象来调用方法实现文件的写入 //写法1:新建对象的时候,传递文件路径名(具备新建,清空覆盖源文件的功能,不需要调用open)//ofstream out("./1.txt");//写法2:ofstream out;out.open("./1.txt");//写入一个字节out.put('#');//写入多个字节out.write("nihao",3);//关闭文件out.close();return 0;
}
(4)文件关闭
fin.close();
(5)设置读写偏移–》随机读写文件(不按照顺序)
lseek() fseek() --->linux
设置读的偏移 seekg()
设置写的偏移 seekp()istream& seekg (streamoff off, ios_base::seekdir way);参数: off --》偏移多少字节way --》从哪里开始偏移ios_base::beg //起始位置 SEEK_SETios_base::cur //当前位置 SEEK_CURios_base::end //末尾位置 SEEK_ENDostream& seekp (streamoff off, ios_base::seekdir way);
(6)如何判断文件打开/新建是否成功
方法一:
bool is_open();
方法二:
对象判断 if(对象)
(7)gcount()方法获取读到的字节数
streamsize gcount() const;
示例代码:
#include <iostream>
#include <fstream> //跟文件IO有关的头文件using namespace std;/*设置读取文件的偏移
*/int main()
{//创建ifstream的对象,通过这个对象来调用方法实现文件的读取 ifstream in;//打开你要读取的文件in.open("./1.txt");//写法1://if(!in.is_open())//写法2:if(!in){cout<<"打开文件失败"<<endl;return -1;}//设置读取的偏移in.seekg(5,ios_base::beg); //起始位置往后偏移5个字节//读取文件内容--》从第6个字节开始读取char buf[100]={0};in.read(buf,100);//我想知道read读取了多少字节的数据cout<<"刚才读取的真实字节数: "<<in.gcount()<<endl;cout<<"刚才读取的内容是: "<<buf<<endl;//关闭文件in.close();return 0;
}
