文件流

ofstream  //文件写操作,内存写入存储设备(文件)  输出流
通常我们所说的对一个文件进行写操作,就是把内存里的内容,也就是缓冲区的内容写到硬盘,可以将标准输出设备理解为显示器ifstream      //文件读操作,存储设备到内存.       输入流
通常我们所说对一个文件读操作,就是把存在硬盘的内容写到内存中,也就是缓冲区fstream      //读写操作,对打开的文件可进行读写.   前两者的结合//文件打开方式选项
ios::in  = 0x01, //供读,文件不存在则创建(ifstream默认的打开方 式)
ios::out  = 0x02, //供写,文件不存在则创建,若文件已存在则清空原内容(ofstream默认的打开方式)
ios::ate  = 0x04, //文件打开时,指针在文件最后。可改变指针的位置,常和in、out联合使用
ios::app  = 0x08, //供写,文件不存在则创建,若文件已存在则在原文件内容后写入 新的内容,指针位置总在最后
ios::trunc = 0x10,      // 在读写前先将文件长度截断为0(默认)
ios::nocreate  = 0x20,  //文件不存在时产生错误,常和in或app联合使用
ios::noreplace = 0x40,   //文件存在时产生错误,常和out联合使用
ios::binary   = 0x80  //二进制格式文件//文件保护方式选择项
filebuf::openprot;   //默认的兼容共享方式
filebuf::sh_none;    //独占,不共享
filebuf::sh_read;    //读共享
filebuf::sh_write;   //写共享

创建文件 

ofstream ofs("example.txt"); //打开文件用于写,若文件不存在就创建它
if (!ofs)
{return;//打开文件失败则结束运行
}ofs << setw(10) << "Name: " << "Beethoven" << endl; //使用插入运算符写文件内容   
ofs << setw(10) << "song: " << "Moonlight Sonata" << endl;
ofs.close(); //关闭文件

 fstream

open()用来打开指定的文件,可以通过is_open()判断是否打开成功
is_open();用来判断单开文件是否成功,成功返回true,发欧泽返回false
good();在没有发生任何错误的情况下返回true,否则返回falseofstream f;
f.open("1.txt",ios_base::app);
if(!f.is_open()){cout<<"open error";return -1;
}
f<<"1234";
f<<"456";
f.close();

ifstream

open()用来打开指定的文件,可以通过is_open()判断是否打开成功
is_open();用来判断单开文件是否成功,成功返回true,发欧泽返回false
good();在没有发生任何错误的情况下返回true,否则返回false
eof();判断是否读取文件数结束,读取到结束返回true,否则返回false
fail();当读取操作类型不匹配的情况下返回true,否则返回falseint c;
ifstream inFile;
inFile.open("test.txt");//打开指定的文件
if(!inFile.is_open()){//判断打开是否成功cout<<"打开失败"<<endl;
}
while(inFile.good())//判断是否发生任何类型的错误inFile>>c;
if(inFile.eof())//判断是否文件结束cout<<"文件结束";
else if(inFile.fail())//是否发生类型错误,本程序c为int,当出现char类型,就会出错cout<<"读取失败";
else cout<<"unkown error\n";
inFile.close();

getline()读取一行

ifstream fin("test.txt",ios::in);
string s;
while(getline(fin,s))cout << s;//输出每一行
每次从fin指向的文件中读取一行,一行之中的所有字符都会被读入,包括空格。但是结尾的空格不读入,回车换行也不读入。

打开文件的方法

调用构造函数时指定文件名和打开模式
ifstream f("d:\\12.txt", ios::nocreate); //默认以 ios::in 的方式打开文件,文件不存在时操作失败
ofstream f("d:\\12.txt");  //默认以 ios::out的方式打开文件
fstream f("d:\\12.dat", ios::in|ios::out|ios::binary); //以读 写方式打开二进制文件  使用Open成员函数
fstream f;
f.open("d:\\12.txt",ios::out);  //利用同一对象对多个文件进行操作时要用到open函数

检查是否成功打开

成功:
if (f) {...}       //对ifstream、ofstream对象可 用,fstream对象不可用。 mysql
if (f.good()) {...}  失败:
if (!f) {...}       // !运算符已经重载
if (f.fail()) {...} 

随机读写文件 

通过移动文件读写指针,可在文件指定位置进行读写。seekg(绝对位置);      //绝对移动,    //输入流操作
seekg(相对位置,参照位置);  //相对操作
tellg();          //返回当前指针位置
seekp(绝对位置);      //绝对移动,    //输出流操作
seekp(相对位置,参照位置);  //相对操作   
tellp();          //返回当前指针位置  参照位置: mysql
ios::beg  = 0       //相对于文件头
ios::cur  = 1       //相对于当前位置
ios::end  = 2       //相对于文件尾  

写文本文件的示例

fstream f("try.txt", ios::out);
f << 1234 << ' ' << 3.14 << 'A' << "How are you"; //写入数据
f.close();
f.open("try.txt", ios::in);
int i;
double d;
char c;
char s[20];
f >> i >> d >> c;  //读取数据
f.getline(s,20);
cout << i << endl; //显示各数据
cout <<d << endl;
cout << c << endl;
cout << s << endl;
f.close(); 运 行结果:
1234
3.14
A
How are you
Press any key to continue显示文本文件的内容

使用get()一次读一个字符

ifstream fin("简介.txt", ios::nocreate);
if (!fin)
{  cout << "File open error!\n";  return;
}
char c;
while ((c=fin.get()) != EOF) cout << c; //注意结束条件的判断
fin.close();

使用get(char *,int n,char delim='\n')一次读多个字符

ifstream fin("d:\\简介.txt",ios::nocreate);
if(!fin)
{  cout<<"File open error!\n";  return;
}
char c[80];
while(fin.get(c,80,'\0')!=NULL)cout<<c; //注意结束条件的判断
fin.close();

使用read(char *,int n)读文件

ifstream fin("简介.txt",ios::nocreate);
if(!fin){
cout<<"File open error!\n";
return;
}
char c[80];
while(!fin.eof())            //判 断文件是否读结束
{  fin.read(c,80);  cout.write(c,fin.gcount());
}
fin.close();

拷贝文件(二进制)

尽管可以使用重载运算符<< 和>>,以及函数(如getline)来操作符输入和输出数据,是合法的,但是在在二进制文件中,这些操作没有什么实际意义。

文件流包括两个为顺序读写数据特殊设计的成员函数:write 和 read。第一个函数 (write) 是ostream 的一个成员函数,都是被ofstream所继承。而read 是istream 的一个成员函数,被ifstream 所继承。类 fstream 的对象同时拥有这两个函数。它们的原型是:

write ( char* buffer, streamsize size );
read ( char* buffer, streamsize size );

buffer 是一块内存的地址,用来存储或读出数据。参数size 是一个整数值,表示要从缓存(buffer)中读出或写入的字符数。

char * buffer;
long size;
ifstream in (filename, ios::in|ios::binary|ios::ate);
size = in.tellg();
in.seekg (0, ios::beg);
buffer = new char [size];
in.read (buffer, size);
in.close();  cout << "the complete file is in a buffer";  delete[] buffer;
ifstream fin("1.exe", ios::nocreate|ios::binary);
if (!fin)
{  cout << "File open error!\n";  return;
}
ofstream fout("2.exe", ios::binary);
char c[1024];
while (!fin.eof())
{  fin.read(c, 1024);  fout.write(c, fin.gcount());
}
fin.close();
fout.close();
cout << "Copy over!\n";注意:若期望是二进制的文件读写,必须要指定读、写的模式有ios::binary,否则读取数据会出错。

写操作
ofstream write(char *buffer, int length) 
buffer是变量指针,一般需要强制转化成char *类型,然后加取地址符,因为任何内容都可以表现成字符的形式,而后面的length则是变量类型的字节长,一般用sizeof进行计算防止不必要的错误,下面看实例。

#include<fstream>
int main()
{using namespace std;int a = 1127;double b = 3.1415;ofstream ofs("test.txt",ios::binary);ofs.write((char*)&a,sizeof(int));ofs.write((char*)&b,sizeof(double));
}注:(其中ios::binary以二进制方式打开文件) 

一个打开并检查输入文件的程序

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
ifstream& open_file(ifstream &in,const string &file)
{  in.close();  in.clear();  in.open(file.c_str());  return in;
}
int main()
{  ifstream file;  open_file(file,"1.txt");  string s;  while(getline(file,s))  {  cout<<s<<endl;  }  file.close();  return 0;
}

(二)读操作。 
ifstream read(char * buffer, int length) 
参数和刚才一样的道理,下面看将上文的a,b输出到控制台。

#include<fstream>
int main()
{using namespace std;int a = 1127;double b = 3.1415;ofstream ofs("test.txt",ios::binary);ofs.write((char*)&a,sizeof(int));ofs.write((char*)&b,sizeof(double)); ofs.close();   int a1;double b1;ifstream ifs("test.txt",ios::binary);ifs.read((char*)&a1,sizeof(int));cout<<a1<<endl;ifs.read((char*)&b1,sizeof(double));cout<<b1<<endl;ifs.close();return 0;
}

其实道理很简单,应用此模式,数据类型复杂一些像结构也照样可以进行读写操作。例如。

#include<fstream>
#include<iostream>
#include<cstring>
struct A
{int a;double b;
};
int main()
{using namespace std;A aa={1127,3.1415};ofstream ofs("test.txt",ios::binary);ofs.write((char*)&aa,sizeof(A));ofs.close();A bb;ifstream ifs("test.txt",ios::binary);ifs.read((char*)&bb,sizeof(A));cout<<bb.a<<endl;cout<<bb.b<<endl;return 0;
}

参考:

http://www.it610.com/article/5040888.htm

fstream、ifstream、ofstream相关推荐

  1. ofstream、ifstream、fstream

    文章目录 1 文件读写相关的流 1.1 文件读写相关的流 1.2 文件打开方式 2 ofstream.ifstream.fstream 2.1 ofstream和ifstream读写文本文件 2.2 ...

  2. C++读写文件操作(fstream、ifstream、ofstream、seekg、seekp、tellg、tellp用法)

    本文主要总结用C++的fstream.ifstream.ofstream方法读写文件,然后用seekg().seekp()函数定位输入.输出文件指针位置,用tellg().tellp()获取当前文件指 ...

  3. fstream、ifstream、ofstream创建新文件

    先阅读 iostream的工程实践,论述了isotream的用途与局限,与c语言io的对比(more effetive c++ item23也有论述) 关键问题1:如果文件不存在,三种流如何处理? 关 ...

  4. fstream,ifstream,ofstream 详解与用法

    fstream,istream,ofstream 三个类之间的继承关系 fstream :(fstream继承自istream和ofstream) 1.typedef basic_fstream< ...

  5. C++ 笔记(21)— 处理文件(文件打开、关闭、读取、写入)

    C++ 提供了 std::fstream ,旨在以独立于平台的方式访问文件. std::fstream 从 std::ofstream 那里继承了写入文件的功能,并从 std::ifstream 那里 ...

  6. ifstream和ofstream的理解

    ifstream和ofstream的理解 开发工具与关键技术:Visual Studio.C++ 作者:张国军 撰写时间:2019年06月04日 各种计算机应用系统通常把一些相关信息组织起来保存在外存 ...

  7. C++文件操作详解(ifstream、ofstream、fstream)【笔记本】

    C++文件操作详解(ifstream.ofstream.fstream) C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由ostream引申而来) ifst ...

  8. 考考你的基础知识:C++ 文件操作ofstream、ifstream使用

    测试平台:XP+VS2008,win32控制台程序consoletest 利用ofstream文件的输出操作 #include <fstream> using namespace std; ...

  9. 文件输入输出(文本文件、二进制文件)(ifstream、ofstream)

    一.写入文件流程 创建一个ofstream对象来管理输出流. 将该对象与特定文件关联起来. 以使用cout方式使用该对象,唯一区别是输出进文件,不是屏幕. 关闭文件流. ofstream fout; ...

最新文章

  1. 【原创】开源Math.NET基础数学类库使用(14)C#生成安全的随机数
  2. 【Linux】一步一步学Linux——nice命令(127)
  3. leetcode :数组和链表
  4. python记录日志_5分钟内解释日志记录—使用Python演练
  5. [css] CSS的伪类和伪对象有什么不同?
  6. apache poi excel显示 base64 图片_数据处理之带图片Excel数据处理解惑
  7. 把自定义类实例存储到LSO
  8. 英文字母信息熵与冗余度计算Python实现
  9. 媲美Teamviewer的远程桌面软件推荐 总有一款适合你
  10. 《疯狂Java讲义》(第5版) 李刚
  11. 运营小实践 -- 借助大数据平台刻画虎牙直播APP人群画像
  12. 深入浅出推荐系统(一):推荐系统基本架构
  13. 油管铺设 离散数学 合工大 prim算法
  14. Java开发--implement Serializable
  15. Android 百度人脸识别问题总结
  16. HTML 表单元素的基本样式
  17. jquery的eval的使用
  18. C++反射(Reflection)
  19. Jenkins首次安装选择推荐插件时出现“: No such plugin: cloudbees-folder” 解决方案
  20. 实现微信产品问题反馈群实时监控与问题自动录入(上)

热门文章

  1. 《那些年啊,那些事——一个程序员的奋斗史》——40
  2. 图像处理 低频滤波器 笔记
  3. java贪吃蛇(障碍物*咬尾巴)
  4. Linux删除带‘\\’的文件夹
  5. python win32gui+pynput 自动回复qq消息
  6. win10系统无法访问ftp服务器地址,win10如何访问ftp服务器地址
  7. 微服务09——中间件
  8. 【FPGA】实战之创建项目
  9. [Android]朝花夕拾之使用DexClassLoader动态加载广点通jar包
  10. Apache 的 X-sendfile