900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 文件输入输出(文本文件 二进制文件)(ifstream ofstream)

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

时间:2022-01-22 03:21:33

相关推荐

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

一.写入文件流程

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

ofstream fout;fout.open(filename);//上面两步可以合并为:ofstream fout(filename);//直接调用ofstream 类的构造函数fout<<"data";//写入文件fout.close();

二.读取文件流程

创建一个ifstream对象管理输入流。将该对象与特定的文件关联起来。以使用cin的方式使用该对象。关闭文件流。

ifstream fin;fin.open(filename);//上面两步可以合并为:ifstream fin(filename);//直接调用ifstream 类的构造函数//以使用cin的方式使用该对象。char ch;fin>>ch;//从filename中读取一个字符char buf[80];fin>>buf;//从filename读取80个字符fin.getline(buf,80);//从文件中读取一行string line;getline(fin,line);//从文件中读取一行fin.close();

三.检查文件流是否打开成功

fin.open(filename);//创建文件流//方法一if(fin.fail()){cout<<"文件打开失败!"<<endl;}//方法二if(!fin){cout<<"文件打开失败!"<<endl;}//方法三if(!fin.is_open()){cout<<"文件打开失败!"<<endl;}

其中,方法三更好用,因为它能够检测出其他方式不能检测出的微妙问题。

四.文件写入、读取例子

#include <iostream>#include <fstream>using namespace std;int main() {string filename;cout << "input file name:" << endl;cin >> filename;//创建文件的输出流ofstream fout(filename.c_str());fout << "For your eyes only!\n";//写向文件cout << "Enter secret number:";//写向屏幕float secret;cin >> secret;fout << "Your secret number is " << secret << endl;fout.close();//创建文件的输入流ifstream fin(filename);if (!fin.is_open()) {cout << "文件打开失败!" << endl;}cout << "Here are the contents of " << filename << ":" << endl;char ch;while (fin.get(ch))cout << ch;cout << "Done\n";fin.close();}

五.文件模式

文件模式是描述文件将被如何使用:读、写、追加等。

trunc:单词原型为:truncate,译为截断。

因此,ios_base::trunc:打开已有的文件,以接受成熟输出时将被截断;也就是删除以前的内容。

ios_base::out:本身也会导致文件被截断,相当于先删除以前文件内容,再写入文件。

app:单词原型为append,译为追加。

使用方法:

ifstream fin(filename, C++mode);//调用构造函数,同时申明模式//或ofstream fout;fout.open(filename, C++mode);//在open函数中申明模式

ios_base::ate和ios_base::app都将文件指针指向打开的文件尾。二者的区别在于,前者将指针放在文件尾;后者只允许将数据添加到文件尾。

六.追加文件案例

#include <iostream>#include <fstream>#include <string>#include <cstdlib>//for exit()using namespace std;const char* file = "guest.txt";int main() {char ch;ifstream fin;fin.open(file);if (fin.is_open()) {cout << "Here are the current content of the " << file << " file:\n";while (fin.get(ch))cout << ch;fin.close();}ofstream fout(file, ios_base::out | ios_base::app);if (!fout.is_open()) {cerr << "Can't open " << file << " file for output.\n";exit(EXIT_FAILURE);}cout << "Enter guest names (enter a blank line to quit):\n";string name;cin >> name;fout << name << endl;//show revise filefin.clear();//not necessary for some compilerfin.open(file, ios_base::in);if (fin.is_open()) {cout << "Here are the new contents of the " << file << " file:\n";while (fin.get(ch))cout << ch;fin.close();}

七.二进制文件

将数据存储在文件中,可以将其存储为文本格式或二进制格式。

文本格式:指的是将所有内容(甚至是数组)都存储为文本。例如,以文本格式存储值**-2.324216e+07**,将存储该数字包含的13个字符。

二进制格式:指的是存储值的计算机内部表示。也就是说,计算机不是存储字符,而是存储这个值的64位double表示。

对于字符来说,二进制表示和文本表示都是一样的,即字符的ASCII码的二进制表示。但是对于数字来说,二进制表示与文本表示有很大的差别。

例如:

0.375的二进制表示

0(符号位)0111110(指数位)110000000000000000000000(二进制分数位)

0.375的文本表示

00110000(字符0编码)00101110(字符.编码)00110011(字符3编码)00110111(字符7编码)00110111(字符5编码)

优缺点:

文本格式:便于读取,可以使用编译器或字处理器来读取和编辑文本文件,可以很方便地将文本文件从一个计算机系统传输到另一个计算机系统。

二进制文件:对数字来说比较精确,因为他存储的是值的内部表示,不会出现转换误差和舍入误差。同时,保存数据更快,因为不需要转换,占用空间较小。然而,如果另一个系统使用另一种内部表示,则可能无法将数据传输给该系统。

使用:

const int LIM = 20;struct planet{char name[LIM];double population;double g;};planet p1;ofstream fout("planet.dat", ios_base::out | ios_base::binary);//ios_base::binary二进制fout.write((char*)&p1, sizeof p1);//程序前往p1结构的地址,并将开始的36个字节(sizeof p1表达式的值)复制到与fout关联的文件中//信息恢复ifstream fin("planet.dat", ios_base::out | ios_base::binary);//ios_base::binary二进制fin.read((char*)&p1, sizeof p1);//read()将从文件中复制sizeof p1个字节到p1结构中。

write():这种方法将内存中指定数目的字节复制到文件中,必须将地址强制转换为char的指针。

使用案例:

#include <iostream>#include <fstream>#include <iomanip>#include <cstdlib>//for exit()using namespace std;inline void eatline() {while (cin.get() != '\n') continue; }const char* file = "planets.dat";struct planet{char name[20];double population;double g;};int main() {planet p1;cout << fixed << right;//show initial contentsifstream fin(file, ios_base::out | ios_base::binary);if (fin.is_open()) {cout << "Here are the current contents of the " << file << " file:\n";while (fin.read((char*)&p1, sizeof p1)) {//写入p1中cout << setw(20) << p1.name << ":" << setprecision(0) << setw(12) << p1.population << setprecision(2) << setw(6) << p1.g << endl;}fin.close();}//add dataofstream fout(file, ios_base::out | ios_base::binary|ios_base::app);if (!fout.is_open()) {cerr << "Can't open" << file << "file for output:\n";exit(EXIT_FAILURE);}cin.get(p1.name, 20);while (p1.name[0] != '\0') {eatline();cout << "Enter planetary population:";cin >> p1.population;cout << "Enter planet's acceleration of gravity:";cin >> p1.g;eatline();fout.write((char*)&p1, sizeof p1);cout << "输入回车结束,再次输入name:" << endl;cin.get(p1.name, 20);}fout.close();//show revised filefin.clear();//not required for some implementations,but won't hurtfin.open(file, ios_base::out | ios_base::binary);if (fin.is_open()) {cout << "Here are the current contents of the " << file << " file:\n";while (fin.read((char*)&p1, sizeof p1)) {//写入p1中cout << setw(20) << p1.name << ":" << setprecision(0) << setw(12) << p1.population << setprecision(2) << setw(6) << p1.g << endl;}fin.close();}cout << "Done" << endl;}

八.EOF()函数

EOF:原型end of file,用于判断作为文件结束标志的文件,必须是文本文件。

while (!infile.eof())

九.以前笔记

功能:将source.txt文件中内容,写进target.txt文件中。

source.txt文件

target.txt文件

读文件

两种方法,如下:

#include <iostream>#include <fstream>//引入文件读写的头文件using namespace std;//文件路径string source = "C:\\Users\\19a405\\Desktop\\source.txt";string target = "C:\\Users\\19a405\\Desktop\\target.txt";int main() {//方法一:调用ifstream类构造函数ifstream ism1(source,ios::in);//只读方式打开文件//方法二:调用ifstream类内部函数open()ifstream ism2;ism2.open(source, ios::in);//判断文件是否打开成功if (!ism1) {//这里在ifstream类中重载了!号cout << "文件打开失败!" << endl;}//读文件char ch;//get()每次读取一个字符,就是cin.get()作用while (ism1.get(ch)){cout << ch;}//关闭文件ism1.close();}

结果:

写文件

#include <iostream>#include <fstream>//引入文件读写的头文件using namespace std;//文件路径string source = "C:\\Users\\19a405\\Desktop\\source.txt";string target = "C:\\Users\\19a405\\Desktop\\target.txt";int main() {//读文件//方法一:调用ifstream类构造函数ifstream ism1(source,ios::in);//只读方式打开文件//方法二:调用ifstream类内部函数open()ifstream ism2;ism2.open(source, ios::in);//写文件//方法一ofstream osm1(target, ios::out);//方法二ofstream osm2;osm2.open(target, ios::out);//判断文件是否打开成功if (!ism1) {//这里在ifstream类中重载了!号cout << "文件打开失败!" << endl;}//读文件char ch;while (ism1.get(ch))//get()每次读取一个字符,就是cin.get()作用{cout << ch;//给target写内容osm2.put(ch);}//关闭文件ism1.close();osm2.close();}

结果:

问题

我们再次运行上述写文件代码。

结果:依然没变。

但是,我们想在文件后面进行追加后续文字,出现两个“我怀念的 等”。

文件追加

修改:增加ios::app.

//写文件//方法一ofstream osm1(target, ios::out|ios::app);//方法二ofstream osm2;osm2.open(target, ios::out|ios::app);

继续运行:

结果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。