Skip to content

Getting Started zh CN

weigao edited this page Jun 20, 2026 · 1 revision

快速开始

本页面介绍如何在 C 或 C++ 项目中安装并开始使用 cfstream

cfstream 是一个轻量级的头文件库。你不需要额外编译或链接 .c / .cpp 源文件,只需要在项目中包含正确的头文件即可使用。

环境要求

C++

C++ 项目请使用:

#include "cfs.hpp"

推荐标准:

  • C++98 或更高版本

C++ 版本同时提供标准流重定向和独立的 C++ 文件流。

C

C 项目请使用:

#include "cfs.h"

要求标准:

  • C99 或更高版本

如果 C 标准低于 C99,cfs.h 会通过 #error 终止编译。

下载

请从 Release 页面下载最新版本:

下载后解压压缩包,并根据你的项目语言选择需要的头文件。

选择正确的头文件

头文件 语言 说明
cfs.hpp C++ cfstream 的 C++ 接口
cfs.h C cfstream 的 C 接口

在 C++ 源文件中使用 cfs.hpp

在 C 源文件中使用 cfs.h

正常情况下,不建议在同一个源文件中同时包含这两个头文件。

将头文件加入项目

推荐做法是将头文件放在源文件所在的同一目录中。

C++ 项目:

your-project/
├── main.cpp
└── cfs.hpp

C 项目:

your-project/
├── main.c
└── cfs.h

然后使用双引号包含头文件。

C++:

#include "cfs.hpp"

C:

#include "cfs.h"

你也可以将头文件放入编译器的 include 路径,然后使用尖括号包含:

#include <cfs.hpp>
#include <cfs.h>

不过对于大多数小型项目,直接把头文件放在项目目录中会更简单,也更推荐。

C++ 快速开始

C++ 版本主要提供两种使用 cfstream 的方式。

方法一:使用独立的 C++ 文件流

当你希望保留标准输入输出不变时,这是更推荐的 C++ 用法。

#include "cfs.hpp"

int main() {
    cfs::cppfs("test.in", "test.out");

    int n;
    cfs::cin >> n;
    cfs::cout << n << '\n';

    cfs::closecppfs();

    return 0;
}

这个程序会从 test.in 读取数据,并写入 test.out

在这种模式下:

  • cfs::cin 从输入文件读取
  • cfs::cout 向输出文件写入
  • std::cinstd::cout 不会被重定向

这种方式适合需要同时区分文件流和控制台流的场景。

方法二:重定向标准输入输出流

你也可以直接重定向标准输入输出流。

#include <iostream>
#include "cfs.hpp"

int main() {
    cfs::cfs("test.in", "test.out");

    int n;
    std::cin >> n;
    std::cout << n << '\n';

    cfs::closecfs();

    return 0;
}

这个程序会重定向标准输入输出。

在这种模式下:

  • std::cintest.in 读取
  • std::cout 写入 test.out

这种方式接近 freopen 的用法,但由 cfstream 进行封装。

C 快速开始

C 版本用于重定向标准输入输出流。

#include <stdio.h>
#include "cfs.h"

int main(void) {
    cfs("test.in", "test.out");

    int n;
    scanf("%d", &n);
    printf("%d\n", n);

    closecfs();

    return 0;
}

这个程序会从 test.in 读取数据,并写入 test.out

在这种模式下:

  • scanf 从输入文件读取
  • printf 向输出文件写入

只重定向输入或输出

输入文件名和输出文件名都可以传入 NULL

在 C++ 中,也可以使用 nullptr

如果某个参数为 NULLnullptr,对应的流就不会被重定向。

只重定向输出

#include <iostream>
#include "cfs.hpp"

int main() {
    cfs::cfs(NULL, "test.out");

    int n;
    std::cin >> n;
    std::cout << n << '\n';

    cfs::closecfs();

    return 0;
}

在这个例子中:

  • 输入仍然来自控制台
  • 输出会写入 test.out

如果你使用的是支持 nullptr 的 C++ 标准,也可以写成:

cfs::cfs(nullptr, "test.out");

只重定向输入

#include <iostream>
#include "cfs.hpp"

int main() {
    cfs::cfs("test.in", NULL);

    int n;
    std::cin >> n;
    std::cout << n << '\n';

    cfs::closecfs();

    return 0;
}

在这个例子中:

  • 输入会从 test.in 读取
  • 输出仍然发送到控制台

如果你使用的是支持 nullptr 的 C++ 标准,也可以写成:

cfs::cfs("test.in", nullptr);

查看当前版本

cfstream 提供了获取当前库版本的函数。

C++

#include <iostream>
#include "cfs.hpp"

int main() {
    std::cout << cfs::getversion() << '\n';

    return 0;
}

C

#include <stdio.h>
#include "cfs.h"

int main(void) {
    printf("%s\n", cfs_getversion());

    return 0;
}

版本号会以 const char* 的形式返回。

基本注意事项

当你不再需要文件流时,建议关闭它。

对于 C++ 独立文件流:

cfs::closecppfs();

对于 C++ 标准流重定向:

cfs::closecfs();

对于 C:

closecfs();

关闭操作会恢复或关闭对应的流资源。

推荐继续阅读

完成本页后,可以继续阅读:

Clone this wiki locally