陷入传统 C++ 之间IO库和标准化IO库

发布于 2024-12-12 05:03:03 字数 294 浏览 2 评论 0原文

我的问题是经过一番挫折之后,实际上我最近研究了标准C++ IO库。我在Linux机器上开发,所以一切都很好。由于我对文件 io 使用了异常处理 (file.exceptions(flags))),旧版本的 GNU C++ 编译器不支持该异常处理。实际的部署机器有非常旧的 g++ 版本,可能是 2.9x 左右。我正在编写一个数据记录器应用程序,因为我编写了很多依赖于 try-catch 对的代码。我现在该怎么办。我尝试声明一个从 std::exception 继承的异常。有用。将 fstream 包装在头文件中是个好主意吗?如果是,我应该怎么做,比如继承,还是只是包装?

My question is after a frustration, actually I recently studied the standard C++ IO library. I developed on a Linux machine, so everything was fine. Since I used exception handling for file io (file.exceptions(flags))), which is not supported by older version of GNU C++ compiler. The actual deployment machine has very old version of g++ probably 2.9x something. I am writing a data recorder application, since i wrote a lot of code relying on try-catch pair. What should I do now. I tried declaring an exception inherited from std::exception. It works. Is it a good idea to wrap fstream in a header file. If yes, how should I do it, like inherit, or just wrap?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雅心素梦 2024-12-19 05:03:03

由于您使用的是linux & gcc 已经有了,开始使用 GNU 自动工具可能是个好主意。解决此类可移植性问题是自动工具的核心目的之一。

自动工具将生成一个名为 config.h 的文件,其中包含一组#defines,指示您的环境中是否存在某些功能。 (在这种情况下,AC_CXX_EXCEPTIONS 可能是您想要的检查。)然后,您可以使用 #ifdef 标记让预处理器排除您专门为与旧编译器兼容而编写的代码,只要配置脚本看到该代码他们没有必要。

第一次使用自动工具的学习曲线有点僵硬,但这是一次性的时间成本。它们将使您未来开展的每个项目都更容易设置。您还需要检查您的目标计算机是否支持自动工具,如果支持,则支持哪个版本的工具。

Since you're using linux & gcc already, it might be a good idea to start using the GNU autotools. Solving portability problems of this type is one of the core purposes of the autotools.

The autotools will generate a file named config.h, with a set of #defines that indicates the presence or absence of certain features in your environment. (In this case, AC_CXX_EXCEPTIONS is likely the check you want.) You can then use #ifdef tags to have the preprocessor exclude the code you wrote specifically for compatibility with the old compiler whenever the configure script sees that they are not necessary.

The first time you use autotools is a bit of a stiff learning curve, but that's a one-off time cost. They'll make every future project you embark on much easier to set up. You'll also want to check that your target machine supports the autotools, and if so which version of the tools are supported.

人生戏 2024-12-19 05:03:03

这是我的解决方法,兼容的.h 文件:

#ifndef __COMPATIBLE
#define __COMPATIBLE

#include "exception.hpp"

#ifdef DEPRECATED_LYNX
namespace util
{
    DECLARE_EXCEPTION(_Failure)
}
#define _failure util::_Failure

#else
#define _failure std::ifstream::failure
#endif // DEPRECATED_LYNX

#endif // __COMPATIBLE

这是我对应的 cpp 文件:

#include "compatible.h"

#ifdef DEPRECATED_LYNX
DEFINE_EXCEPTION(util, _Failure)
#endif

由于我是新手,这只是一个解决方法,我现在需要手动抛出异常,所以我包装了 fstream。在 badbit、failbit 和 eofbit 上引发异常。我不太知道它有多好。

This is my workaround, compatible.h file:

#ifndef __COMPATIBLE
#define __COMPATIBLE

#include "exception.hpp"

#ifdef DEPRECATED_LYNX
namespace util
{
    DECLARE_EXCEPTION(_Failure)
}
#define _failure util::_Failure

#else
#define _failure std::ifstream::failure
#endif // DEPRECATED_LYNX

#endif // __COMPATIBLE

This is my coresponding cpp file:

#include "compatible.h"

#ifdef DEPRECATED_LYNX
DEFINE_EXCEPTION(util, _Failure)
#endif

Since I am a newbie, this is just a workaround, I now need to manually throw exceptions so i wrapped up the fstream. Throwing exceptions on badbit, failbit and eofbit. I don't know much how good it is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文