博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++:文件的输入和输出
阅读量:4352 次
发布时间:2019-06-07

本文共 4227 字,大约阅读时间需要 14 分钟。

1、共同的打开文件方式:

fin.open("test.txt",ios::binary)

fout.open("test.txt",ios::binary)
fboth.open("test.txt",ios::in|ios::out|ios::binary)
或者
fistream fin("test.txt",ios::binary)
fostream fout("test.txt",ios::binary)
fstream fboth("test.txt",ios::in|ios::out|ios::binary)

二进制文件的读写

第一种方式:使用函数get和put
1.例如 fout.put(ch)
fin.get(ch)
//练习1 将a到z的26个字符写到E:\\C++\\file2.txt中,并读取出来

#include
#include
using namespace std;//往文件写入数据 void filefout(){ //ofstream fout("E:\\C++\\file2.txt",ios::binary); ofstream fout; fout.open("E:\\C++\\file2.txt",ios::binary); if(!fout) { cout<<"the write file can not open"; exit(1); } char ch='a'; for(int i=0;i<26;i++) { fout.put(ch); ch++; } fout.close();} //从文件读取数据 void filefin(){ ifstream fin; fin.open("E:\\C++\\file2.txt",ios::binary); if(!fin) { cout<<"the read file can not open"; exit(1); } char ch; while(fin.get(ch)) cout<

第二种方式:使用read函数和write函数 

2.例如 fin.read(char*buf,int len)
fout.write(char*buf,int len)

练习2:将两门课程名和成绩以二进制的形式存储在E:\\C++\\file3.txt中,并读取出来

#include
#include
using namespace std;struct list{ char course[15]; int score;};//往文件中写入数据void filewrite(){ list list[2]={
"Computer",90,"Math",78}; ofstream fout("E:\\C++\\file3.txt",ios::binary); if(!fout) { cout<<"The write file can not open!\n"; abort();//exit(1) } for(int i=0;i<2;i++) { fout.write((char*)&list[i],sizeof(list[i])); } fout.close();} //从文件读取数据void fileread(){ list list[2]; ifstream fin("E:\\C++\\file3.txt",ios::binary); if(!fin) { cout<<"The read file can not open!\n"; exit(1); } for(int i=0;i<2;i++) { fin.read((char*)&list,sizeof(list[i])); cout<
<<" "<
<

 

2、在头文件fstream.h中:

类名 说明 功能
ifstream 输入文件流类 用于文件的输入
ofstream 输出文件流类 用于文件的输出
fstream 输入输出流类 用于文件的输入/输出

建立流对象,例如

ifstream fin
ofstream fout
fstream fboth

打开文件方式有两种:
(1)使用成员函数open打开文件
文件流对象.open(文件名,打开模式)
例如:fin.open("test.txt",ios::in)===fin.open("test.txt")
fboth.open("test.txt",ios::in|ios::out|ios::binary)
打开模式例如:ios::in输入操作、ios::out输出操作、ios::binary二进制模式 输入输出模式一般会按流对象进行默认

(2)定义文件流对象时指定参数,通过调用文件流类的构造函数打开文件

例如:ofstream fout("test.txt").....
判断读写文件是否成功?
判断条件:文件打开失败,与文件相关联的流对象的值是0;否则是1.
例如:if(!fout)
{
      cout<<"Cannot open file!\n ";
      //return 1;
}
关闭文件:fout.close();

练习1:先建立一个输出文件,向它写入数据,然后关闭文件,在按输入模式打开它,并读取信息。 

#include
#include
using namespace std;int main(){ //往文件写入数据 ofstream fout("E:\\C++\\file1.txt");//ofstream fout("E:\\C++\\file1.txt","ios:out"); if(!fout) //如果文件打开失败,fout返回0 { cout<<"Cannot open file!\n "; return 1; } fout<<"Hello world!\n"; //将一个字符串写到文件中 fout<<100<<' '<
<<100<

3、

(1). 检测文件结束EOF(end of file) 采用文件流方式读取方式时,使用成员函数eof(),可以检测这个结束符。

如果该函数的返回值非0,表示到达文件尾;返回值为0,表示未到达文件末尾。
例如: ifstream fin;
       ....
if(!fin.eof()) //尚未到达文件末尾
{
       ...
}

或者

ifstream fin;
....
if(!fin) //尚未到达文件末尾
{
       ...
}
或者
这种方式最常用:while(fin.get(ch))
cout.put(ch);//cout<<ch<<endl;
(2).二进制文件的随机读取

类istream提供了3个成员函数来对读指针进行操作:

tellg() //返回输入文件读指针的当前位置
seekg(文件中位置) //将输入文件中读指针移动指定的位置
seekg(位移量,参照位置) //以参照位置为基准,移动若干字节
文件中位置和位移量都是long型整数,以字节为单位,
参照位置 ios::beg //从文件开头计算要移动的字节数
ios::cur //从文件指针的当前位置计算要移动的字节数
ios::end //从文件末尾指针要移动的字节数
例如 fin.seekg(-/+50,ios::cur) //从文件指针当前位置向前/向后移动50个字节

类ostream提供了3个成员函数来对写指针进行操作:
tellp() //返回输出文件写指针的当前位置
seekp(文件的位置) //将输出文件中写指针移动到指定的位置
seekp(位移量,参照位置) //以参照位置为基准移动的若干字节

 

练习:随机访问二进制数据文件

有3门课程的数据,要求:
(1)以读写方式打开一个磁盘文件,并把这些数据存储到磁盘文件E:\\C++\\file4.txt;
(2)将文件指针定位到第3门课程,读取第3门课程的数据并显示出来;
(3)将文件指针定位到第1门课程,读取第1门课程的数据并显示出来;

#include
#include
using namespace std;struct List{ char course[15]; int score;};int main(){ List list[3]={
{
"Computer",98},{
"Math",78},{
"Chinese",100}}; List st; fstream fboth; fboth.open("E:\\C++\\file4.txt",ios::out|ios::in|ios::binary); if(!fboth) { cout<<"The read-write file can not open!\n"; abort(); } for(int i=0;i<3;i++) { fboth.write((char*)&list[i],sizeof(List)); } //定位指针到第3门课程 fboth.seekg(sizeof(List)*2); fboth.read((char*)&st,sizeof(List)); cout<
<<"\t"<
<

 

(4)将文件指针定位从当前位置定位到下一门课程,读取该门课程的数据并显示出来;

转载于:https://www.cnblogs.com/XYQ-208910/p/4912850.html

你可能感兴趣的文章
css Backgroud-clip (文字颜色渐变)
查看>>
安装 OpenSSL 工具
查看>>
用长微博工具发布长微博
查看>>
大庆金桥帆软报表案例
查看>>
方维分享系统,个人中心杂志社显示我的、关注的、推荐的数量
查看>>
JavaScript BOM加载事件
查看>>
Java复习总结——详细理解Java反射机制
查看>>
Navicat for MySQL10.1.7注册码
查看>>
Proxy模式
查看>>
读书多些会怎样
查看>>
浏览器好用的技术
查看>>
HDU 2188------巴什博弈
查看>>
tp5任务队列使用supervisor常驻进程
查看>>
Xmind?
查看>>
spring+quartz 实现定时任务三
查看>>
day2-三级菜单
查看>>
UIView的常用属性
查看>>
Git push
查看>>
蓝桥杯基础练习题9(表达式)
查看>>
关于beescms如何去掉底部版权信息
查看>>