请问require.h这段代码的意思,还望高手解答

life02 2008-02-22 01:49:11
//: :require.h
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Test for error conditions in programs
// Local "using namespace std" for old compilers
#ifndef REQUIRE_H
#define REQUIRE_H
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>

inline void require(bool requirement,
const std::string& msg = "Requirement failed"){
using namespace std;
if (!requirement) {
fputs(msg.c_str(), stderr);
fputs("\n", stderr);
exit(1);
}
}

inline void requireArgs(int argc, int args,
const std::string& msg =
"Must use %d arguments") {
using namespace std;
if (argc != args + 1) {
fprintf(stderr, msg.c_str(), args);
fputs("\n", stderr);
exit(1);
}
}

inline void requireMinArgs(int argc, int minArgs,
const std::string& msg =
"Must use at least %d arguments") {
using namespace std;
if(argc < minArgs + 1) {
fprintf(stderr, msg.c_str(), minArgs);
fputs("\n", stderr);
exit(1);
}
}

inline void assure(std::ifstream& in,
const std::string& filename = "") {
using namespace std;
if(!in) {
fprintf(stderr, "Could not open file %s\n",
filename.c_str());
exit(1);
}
}

inline void assure(std::ofstream& out,
const std::string& filename = "") {
using namespace std;
if(!out) {
fprintf(stderr, "Could not open file %s\n",
filename.c_str());
exit(1);
}
}
#endif // REQUIRE_H ///:~
请问怎么理解1.string& msg = "Requirement failed"
2.fputs(msg.c_str(), stderr);
3. fprintf(stderr, msg.c_str(), args);
的意思?
以及这段代码的作用?


...全文
140 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
robertzhang10 2008-02-23
  • 打赏
  • 举报
回复
string msg = "Hello";
msg.c_str() 也是 "Hello", 只是类型不同而已。

fputs 的返回值一般不用, 如果你要用就
if(fputs(msg.c_str(), stderr)<0)
printf("error");

stderr 你就理解为屏幕

ofstream 是C++里面的输出文件流类
ryfdizuo 2008-02-22
  • 打赏
  • 举报
回复
std::ofstream& out
这里ofstream是什么意思 ?
----------------
ostream表示输出流。与cout类似

if(!out)
out还有值的吗?
----------
!out表示输出失败的话,out.fail()类似,
life02 2008-02-22
  • 打赏
  • 举报
回复
std::ofstream& out
这里ofstream是什么意思 ?


if(!out)
out还有值的吗?

life02 2008-02-22
  • 打赏
  • 举报
回复
fputs(msg.c_str(), stderr);
fputs("\n", stderr);
分别是什么意思啊

fputs()函数把str(字符串)指向的字符写到给出的输出流. 成功时返回非负值, 失败时返回EOF.
怎么判定它返回的值,又说stderr是标准错误输出,就不懂了
请教了!!!
life02 2008-02-22
  • 打赏
  • 举报
回复
还望楼上几人能帮我解答
life02 2008-02-22
  • 打赏
  • 举报
回复
那 msg.c_str()等于多少啊?
c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同
robertzhang10 2008-02-22
  • 打赏
  • 举报
回复
1. 楼上说了

2.fputs(msg.c_str(), stderr);
把msg转成char*,然后输出到标准错误输出,默认也就是屏幕

3. fprintf(stderr, msg.c_str(), args);

把args里面的字符串,按照msg里面的控制格式输出到标准错误输出。
比如说 msg = "Must use %d arguments"; args = 2;
则输出到stderr 就是 Must use 2 arguments
hityct1 2008-02-22
  • 打赏
  • 举报
回复
const std::string& msg = "Requirement failed")
表示msg默认值是"Requirement failed"
zeloas 2008-02-22
  • 打赏
  • 举报
回复
string& msg = "Requirement failed"
msg是"Requirement failed"转化为string的引用
fputs(msg.c_str(), stderr);
将msg转化为C风格的字符串结果输出到标准错误输出
fprintf(stderr, msg.c_str(), args);
当msg.c_str()返回的字符串中包含的"%s"替换为args变量后内容输出到标准错误输出中

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧