C++学习:在C++中的文件输入和文件输出
简介:
在C语言中,我们有fread和fwrite用于文件的输入和输出,在java中我们也有inputStream和outPutStream输入输出流,在C++语言中,同样为我们封装了自己的输入ifstream,输出流ofstream,当然C++是面向对象的,所以调用的方式上可能跟C有点点小小的区别
ifstream和ofstream都是fstream的子类
提示:
博主:章飞_906285288
博客地址:http://blog.csdn.net/qq_29924041
文件打开方式属性
属性 | 作用 |
---|---|
ios::in | 供读,文件不存在则创建(ifstream默认的打开方 式) |
ios::out | 供写,文件不存在则创 建,若文件已存在则清空原内容(ofstream默认的打开方式) |
ios::ate | 文件打开时,指针在文件最后。可改变指针的位置,常和in、out联合使用 |
ios::app | 供写,文件不存在则创建,若文件已存在则在原文件内容后写入 新的内容,指针位置总在最后 |
ios::trunc | 在读写前先将文件长度截断为0(默认) |
ios::nocreate | 文件不存在时产生错误,常和in或app联合使用 |
ios::noreplace | 文件存在时产生错误,常和out联合使用 |
ios::binary | 二进制格式文件 |
文件保护方式选择项:
属性 | 作用 |
---|---|
filebuf::openprot | 默认的兼容共享方式 |
filebuf::sh_none | 独占,不共享 |
filebuf::sh_read | 读共享 |
filebuf::sh_write | 写共享 |
文件流的打开方式
ifstream f("a.txt", ios::nocreate); //默认以 ios::in 的方式打开文件,文件不存在时操作失败
ofstream f("a.txt"); //默认以 ios::out的方式打开文件
fstream f("a.dat", ios::in|ios::out|ios::binary); //以读 写方式打开二进制文件
文件的关闭
ifstream file("a.txt");
fi.close(); //使用完毕之后需要关闭
文件打开成功的判断
1:直接使用返回流来进行判断
ifstream file("a.txt",ios::out);
if(file){
//
}
2:使用good函数来进行判断,或者也可以用来判断写的东西是否正确
if(file.good()){
//打开成功的判断
}
如:good()是没有任何类型检查的
while(file.good()){
cin >> num;
}
3:可以使用is_open()来判断是否打开
file.open("a.txt");
if(file.is_open()){
//打开成功
}
当然如果判断打开失败,则可以用!来表示
判断读取到文件的尾部EOF,使用函数eof()
伪代码:
instream infile("a.txt");
if(infile){
while(!infile.eof()){
//如果没有读取到文件文件尾部
}
}
文件io流的读写操作:
1:字符输入输出
使用>>和<<来进行读写操作,只能来进行文本操作,读取二进制可能会出错
2:字节输入:
使用write和read函数来进行读写操作
文件io流来获取读写的字节数
getcount()函数,返回的就是读写的字节数
随机进行文件的读写操作
随机读写函数:
seekg(绝对位置); //绝对移动, //输入流操作
seekg(相对位置,参照位置); //相对操作
tellg(); //返回当前指针位置
seekp(绝对位置); //绝对移动, //输出流操作
seekp(相对位置,参照位置); //相对操作
tellp(); //返回当前指针位置
随机读写的标志位:
属性 | 作用 |
---|---|
fios::beg | 相对于文件头 |
ios::cur | 相对于当前位置 |
ios::end | 相对于文件尾 |
重要提示:
因为文件输入输出也是属于io流中的,所以适用于cout和cin的属性设置的,对文件流来所也是适用的,具体的可以参考C++IO流的api
执行步骤:
1. 必须包含头文件fstream
2. 头文件fstream定义了一个用于处理输出的ofstream
3. 需要声明一个或者多个ofstream对象,并以自己喜欢的方式对其进行命名,条件是遵守常用命名规则
4. 必须指明命名空间std,例如:为引用元素ofstream,必须使用变异指令using或者前缀std::;
5. 必须将ofstream对象与文件进行关联起来,为此方法之一就是open()方法
6. 使用完毕文件之后需要将其关闭,使用close()方法
7. 可以结合使用ofstream对象和操作符来输出各种文件类型的数据
注意:
虽然头文件中提供了预先定义好的cout的ostream对象,但是也必须要声明自己的ofstream对象,并且为其命名.并且将同文件关联起来
声明一个对象关联起来,便可以像cout一样使用它,所有可用于cout的操作方法( << ,endl,setf())都是可以用于ofstream对象的
记住创建好outFile之后,就可以像cout一样去使用它了
示例代码:
/*
* ===========================================================================
*
* Filename: ofsteam.cpp
* Description:
* Version: 1.0
* Created: 2017年07月06日 22时31分24秒
* Revision: none
* Compiler: gcc
* Author: (),
* Company:
*
* ===========================================================================
*/
#include
#include
using namespace::std;
int main(int agrc,char* argv[]){
ofstream outFile;
char automobile[50];
int year;
double a_price;
double b_price;
//创建并且打开文件(没有则创建,有则覆盖)
outFile.open("carinfo.txt",ios::out);
cout << "Enter the make and model of automobile;";
cin.getline(automobile,50);
cout <<"Enter the model year:";
cin >> year;
cout << "Enter the origin asking price:";
cin >> a_price;
b_price = 0.913 * a_price;
cout << fixed;
cout.precision(2);//保留两位小数点
cout.setf(ios_base::showpoint); //设置展现小数点
cout << "make the model:" <cout <<"Year:"<cout <<"a_price:"<cout <<"b_price:"<//打开成功 也可以适用outFile.good()
if(outFile){
outFile.precision(2); //向设置cout一样设置保留位数
outFile << fixed; //一般的方式输出浮点数
outFile.setf(ios_base::showpoint);//展示小数点
outFile << "make the model:"<//使用<<将文本输入到文件中去
outFile <<"year:"<"a_price:"<"b_price" <//关闭文本文件输出流
outFile.close();
return 0;
}
具体的结果就不在这里进行输出,有兴趣的copy执行,代码取自C++primer
执行步骤:
ifstream对象本身被用作测试条件的时候,如果最后一个读取操作成功,他将被转换成布尔值true,否则为false
注意:
1:虽然头文件iostream中提供了一个预定义好的名为cin和istream对象,但是必须声明自己的ifstream对象,为其命名,并且将其与文件关联起来
2:方法open()接受一个C风格字符串做为参数,还可以是一个字面字符串,也可以是存储在数组中的字符串
3:与文件关联起来后,能够使用在cin上的操作,都可以使用在ifstream的对象上
4:使用is_open()判断检查文件是否打开了,太老的编译器可能不支持
inFile.open("a.txt");
if(!inFile.is_open()){
exit(1);
}
5:论文件打开的重要性:
打开失败的原因
1:指定文件不存在
2:文件目录错误
3:用户输错了文件名字
4:访问被拒绝(访问权限问题)
6:读取文件的时候有几点需要检查的地方:
1:程序读取文件的时候不应该超过EOF(文件结尾的符号),如果读到EOF或者eof()为true
2:程序可能遇到一些读取类型不匹配的情况
3:某些预期之外的东西,如文件受损或硬件故障等.
示例代码
/*
* ===========================================================================
*
* Filename: ifstream.cpp
* Description:
* Version: 1.0
* Created: 2017年07月07日 21时48分41秒
* Revision: none
* Compiler: gcc
* Author: (),
* Company:
*
* ===========================================================================
*/
#include
#include
#include
using namespace::std;
const int SIZE = 50;
int main(int argc,char* argv[]){
ifstream inFile;
char content[50];
/*inFile.open("carinfo.txt",ios::in);
if(inFile.is_open()){
while(!inFile.eof()){
inFile.getline(content,50);
cout << content << endl;
}
}*/
char fileName[SIZE];
cout << "Enter a name of File:";
cin.getline(fileName,SIZE);
inFile.open(fileName,ios::in);/*打开文件*/
if(!inFile.is_open()){
cout<<"open file is error"<< endl;
exit(1);
}
while(inFile.good()){
inFile.getline(content,50);
cout << content << endl;
}
if(inFile.eof()){/*判断是否到了文件尾部*/
cout << "End of File reached"<< endl;
}else if(inFile.fail()){/*判断是否是类型不匹配或者磁盘错误*/
cout << "Input by Data mismatched" <else {/*未知错误*/
cout << "unknow reason" </*关闭文件*/
return 0;
}
/*
* ===========================================================================
*
* Filename: copyTest.cpp
* Description:
* Version: 1.0
* Created: 2017年07月07日 22时07分41秒
* Revision: none
* Compiler: gcc
* Author: (),
* Company:
*
* ===========================================================================
*/
#include
#include
#include
#include
using namespace::std;
const int SIZE = 50;
int main(int argc,char* argv[]){
ifstream inFile;
ofstream outFile;
char inFileName[SIZE];
char outFileName[SIZE];
char buffer;
char buffer2[20];
cout << "Enter a src file name:";
cin.getline(inFileName,SIZE);
cout << "Enter a des file name:";
cin.getline(outFileName,SIZE);
inFile.open(inFileName,ios::in | ios::binary);
if(!inFile.is_open()){
cout << "open src file error"<exit(1);
}
outFile.open(outFileName,ios::out | ios::binary);
if(!outFile.is_open()){
cout <<"open des file error"<//注意在return的时候将流关闭
outFile.close();
exit(1);
}
int num;
while(inFile.good()){
inFile.read(buffer2,sizeof(buffer2));
num = inFile.gcount();//获取读取的字节
outFile.write(buffer2,num);
//inFile.get(buffer);//读取字符流
//outFile << buffer;//将字符流写入
}
if(inFile.eof()){
cout << "end of input"<< endl;
}else if(inFile.fail()){
cout << "dismatched"<else {
cout <<"unknow"<return 0;
}