# p03/ch11 — Qt 示例

## 可运行示例

| 目标 | 源文件 | 说明 |
|---|---|---|
| `qfile_basic` | `qfile_basic.cpp` | 构建运行见顶层 README；离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh qfile_basic` |
| `qdatastream_io` | `qdatastream_io.cpp` | 构建运行见顶层 README；离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh qdatastream_io` |
| `qtextstream_io` | `qtextstream_io.cpp` | 构建运行见顶层 README；离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh qtextstream_io` |

## 参考片段（签名/伪代码，未单独建目标）

### [block 1] .1 基本文件操作

```cpp
QFileInfo fi("/tmp/archive.tar.gz");
QString base = fi.baseName(); // base = "archive"
QString base = fi.completeBaseName(); // base = "archive.tar"
QString ext = fi.suffix(); // ext = "gz"
QString ext = fi.completeSuffix(); // ext = "tar.gz"
```

### [block 2] .2 二进制文件读写

```cpp
QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << QString("the answer is");
out << (qint32)42;
```

### [block 3] .2 二进制文件读写

```cpp
QFile file("file.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
QString str;
qint32 a;
in >> str >> a;
```

### [block 4] .2 二进制文件读写

```cpp
QFile file("file.dat");
file.open(QIODevice::ReadWrite);
QDataStream stream(&file);
QString str = "the answer is 42";
stream << str;
```

### [block 5] .3 文本文件读写

```cpp
QFile data("file.txt");
if (data.open(QFile::WriteOnly | QIODevice::Truncate))
{
QTextStream out(&data);
out << "The answer is " << 42;
}
```

### [block 6] .3 文本文件读写

```cpp
QFile data("file.txt");
if (data.open(QFile::ReadOnly))
{
QTextStream in(&data);
QString str;
int ans = 0;
in >> str >> ans;
}
```

