c++文件操作

c++文件操作:

文件操作三大步(逻辑如下):

1、打开文件

2、读写文件

3、关闭文件

介绍下头文件:

#include //文件读写头文件

记忆如下:

     a、文件:file

     b、流:stream

合并就是:fstream

小贴士:

输出:程序向文件或者显示器或其他设备输出内容

输入:程序从文件或者设备中读取内容称之为输入

(cin:输入,cout:输出;读取文件:输入,写入文件:输出(注意下概念))

想看结果的直接跳转完整代码:

读取文件过程如下——:

1、建立对象(注意理解哦):

ifstream in;     //输入对象——文件读

ofstream out; //输出对象——文件写

fstream  fs;   //输入输出对象——可读可写

2、打开文件

in.open(这里填写文件路径,打开模式);

out.open(这里填写文件路径,打开模式);

out.open(这里填写文件路径,打开模式);  

第一个参数:文件路径

第二个参数:打开模式

ios::app Opens an output file for appending. ios::ate Opens an existing file (either input or output) and seeks the end.  ios::in Opens an input file. Use ios::in as an open_mode for an ofstream file to prevent truncating an existing file.  ios::out Opens an output file. When you use ios::out for an ofstream object without ios::app, ios::ate, or ios::in, ios::trunc is implied. ios::nocreate Opens a file only if it already exists; otherwise the operation fails. ios::noreplace Opens a file only if it does not exist; otherwise the operation fails. ios::trunc Opens a file and deletes the old file (if it already exists). ios::binary Opens a file in binary mode (default is text mode).  

翻译(还是看英文舒服):

ios::app    打开输出(写入)文件供追加。

ios::ate     打开一个已存在文件(输入(读)或输出(写))并定位到文件尾。

ios::in       打开一个输入(读)文件,使用ios::in作为输出流的打开模式来防止截断                   现有文件

ios::out    打开一个输出文件(写),当使用ios::out没有ios::app、ios::ate或ios::in                    的ofstream对象时,ios::trunc是隐含的。

ios::nocreate 仅打开一个已存在的文件;否则操作失败.  

ios::noreplace 仅打开一个不存在的文件,否则操作失败. 

ios::trunc 打开一个文件,如果已存在,则删除旧文件.  

ios::binary  以二进制打开文件(默认为文本模式).   

ifstream 一般组合方式(打开模式):

      1、ios::in |ios::binary    // 二进制打开(常用用于文件流)

      2、ios::in                      //打开文件

      3、ios::ate                  //打开文件定位到数据尾

ofstream 一般组合方式(打开模式):

      1、ios::app或ios::app|ios::out        //无文件则创建,有文件则追加

      2、ios::ate或ios::ate|ios::out          //无文件则生成,有文件则清空

      3、 ios::ate|ios::in                           //无文件,则失败,有文件,则追加

fstream 一般组合方式(打开模式): 

      1、ios::app|ios::out                    //写,无文件,则创建。有文件,则追加

      2、ios::app|ios::in                       //读,不存在则打开失败   

      3、ios::ate|ios::out                      //无则创建,有则清空

      4、ios::ate|ios::in                       //读,不存在则打开失败

     5、ios::ate|ios::in|ios::out             //可读可写,指针置于文件尾部

小贴士:注意下打开模式(读还是写,会有冲突哦~)

介绍:

       文件路径都知道,什么可说的,打开模式,自行理解

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

完整案例(以二进制为例):

~~~

#include

#include

using namespace std;

typedef struct student {

int age = 0;

string name;

}stu;

int main() {

long temp = 0;

stu a;

a.age = 10;

a.name = "大宝";

stu b;

b.age = 88;

b.name = "sod";

//创建写文件对象(输出)

ofstream out;

out.open("E:/c++教学/c++_four/four.txt", ios::app | ios::out | ios::binary); //追加模式写

if (out.is_open() == false) {

cout << "打开失败";

return 0;

}

else {

out.write((char*)& a, sizeof(a));//不兼容(强制转换)

out.write((char*)& b, sizeof(b));//不兼容(强制转换)

out.close();

}

//写结束

//定义c,用来接收读取的数据

stu c;

//创建读文件对象(输入)

ifstream in;

in.open("e:/c++教学/c++_four/four.txt", ios::in | ios::binary);//in是读

if (in.is_open() == false) {

cout << "打开失败";

return 0;

}

else {

in.seekg(0, ios::end);//将指针移到未见末尾

long length = in.tellg();

in.seekg(0, ios::beg);//将指针移到文件头

long k = length / sizeof(c);

for (int i = 0; i < k; i++) {

    temp = sizeof(c) * i;

in.seekg(temp);

in.read((char*)& c, sizeof(c));

cout << c.name;

cout << c.age;

cout << endl;

}

in.close();

}

cout << "操作结束";

return 0;

}

~~~

你可能感兴趣的:(c++文件操作)