天行健 发表于 2025-1-23 20:46:31

C++进阶知识01.文件、异常处理

## 文件和流

使用 C++ 中的标准库 fstream 从文件读取流和向文件写入流。要在 C++ 中进行文件处理,必须在 C++ 源代码文件中包含头文件 `<iostream>` 和 `<fstream>`。


| 数据类型 | 描述                                                                                                                         |
| :--------- | :----------------------------------------------------------------------------------------------------------------------------- |
| ofstream | 该数据类型表示输出文件流,用于创建文件并向文件写入信息。                                                                     |
| ifstream | 该数据类型表示输入文件流,用于从文件读取信息。                                                                               |
| fstream| 该数据类型通常表示文件流,且同时具有 ofstream 和 ifstream 两种功能,这意味着它可以创建文件,向文件写入信息,从文件读取信息。 |

### 打开文件

在从文件读取信息或者向文件写入信息之前,必须先打开文件。<b>ofstream</b> 和 <b>fstream</b> 对象都可以用来打开文件进行写操作,如果只需要打开文件进行读操作,则使用 <b>ifstream</b> 对象。

open() 函数是 fstream、ifstream 和 ofstream 对象的一个成员。open() 函数的标准语法如下:

```cpp
void open(const char *filename, ios::openmode mode);
```


| 模式标志   | 描述                                                                   |
| :----------- | :----------------------------------------------------------------------- |
| ios::app   | 追加模式。所有写入都追加到文件末尾。                                 |
| ios::ate   | 文件打开后定位到文件末尾。                                             |
| ios::in    | 打开文件用于读取。                                                   |
| ios::out   | 打开文件用于写入。                                                   |
| ios::trunc | 如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0。 |

在设置模式时可以设置多种模式,如对文件可读可写

```cpp
ifstreamafile;
afile.open("file.dat", ios::out | ios::in );
```

写模式打开文件,如果存在内容则先清空

```cpp
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
```

### 关闭文件

调用 close() 方法即可。

### 完整例子

```cpp
#include<iostream>
#include<fstream>
using namespace std;

// 写入读取也是用重定向
int main(){
    ofstream writeFile;
    writeFile.open("afile.data");
    cout<<"写入数据"<<endl;
    char data;
    cin.getline(data,100);
    writeFile<<data<<endl; // data 的数据重定向到 writeFile 中
    writeFile.close();

    ifstream readFile;
    char data2;
    readFile.open("afile.data");
    readFile>>data2; // 读取的数据重定向到 data2
    cout<<data<<endl;
}
```

## 异常处理

C++ 的异常处理的关键字有三个:try、catch、throw

- <b>throw:</b>当问题出现时,程序会抛出一个异常。这是通过使用 throw 关键字来完成的。
- <b>catch:</b>在您想要处理问题的地方,通过异常处理程序捕获异常。catch 关键字用于捕获异常。
- <b>try:</b>try 块中的代码标识将被激活的特定异常。它后面通常跟着一个或多个 catch 块。

```cpp
try{

}catch(ExceptionName e1){

}catch(ExceptionName e1){

}
```

### 抛出异常

由于我们抛出了一个类型为 <b>const char\*</b> 的异常,因此,当捕获该异常时,我们必须在 catch 块中使用 const char\*。

```cpp
#include<iostream>
using namespace std;

double division(int a, int b){
    if(b==0){
      throw "除数不能为0";
    }
    return a/b;
}

int main(void){
    try{
      division(10,0);
    }catch(const char* msg){
      cerr<<msg<<endl;
    }
    return 0;
}
// 除数不能为 0
```

### 自定义异常

<b>what()</b> 是异常类提供的一个公共方法,它已被所有子异常类重载。这将返回异常产生的原因。

```cpp
#include<iostream>
using namespace std;

double division(int a, int b){
    if(b==0){
      throw new bad_alloc;
    }
    return a/b;
}

class MyExecption : public exception{
    public:
    const char* what() const throw(){
      return "自定义的 C++ 异常";
    }
};

void useMyExeception(){
    throw MyExecption();
}

int main(void){
    try{
      useMyExeception();
    }catch(MyExecption& e){
      cerr<<e.what()<<endl; // 自定义的 C++ 异常
    }
    return 0;
}
```
页: [1]
查看完整版本: C++进阶知识01.文件、异常处理