如何在VC++;6.0?
在VS2010中,我编写了以下可变宏以将信息转储到文件中如何在VC++;6.0?,c,visual-c++,c-preprocessor,visual-c++-6,variadic-macros,C,Visual C++,C Preprocessor,Visual C++ 6,Variadic Macros,在VS2010中,我编写了以下可变宏以将信息转储到文件中 #define INDENT(fp, indent) for(size_t __i = 0; __i < (indent); ++__i) fprintf((fp), " ") // IND_FP = indented fprintf. // This macro uses two IMPLICIT parameters. // 1. (FILE *)fp is a pointer to the output file. // 2
#define INDENT(fp, indent) for(size_t __i = 0; __i < (indent); ++__i) fprintf((fp), " ")
// IND_FP = indented fprintf.
// This macro uses two IMPLICIT parameters.
// 1. (FILE *)fp is a pointer to the output file.
// 2. (size_t)indent specifies the indentation level.
#define IND_FP(format, ...) do{ INDENT(fp, indent); fprintf(fp, format, __VA_ARGS__); }while(0)
但是我必须将十行(如果不是几百行的话)代码从IND\u FP(format,…)
更改为IND\u FP(FP,indent,format,…)
宏有什么技巧可以帮我吗?或者我最好使用显式参数并习惯冗余?您可以通过不使用变量来绕过变量。使用流而不是文件*。像这样的
#define IND_FP Logger(indent)
class Logger {
public:
Logger(int indent);
int operator()(FILE* fp, char* format, ...);
private:
// an exercise for the reader
};
#define LOGGER(logfile,loginfo) logfile << loginfo << std::endl
如果您希望变得更复杂,可以使用
LOGGER(logfile, "x = " << x << " b=" << b);
LOGGER(日志文件,“x=”
这可能适合你的情况。哦,天哪,你肯定是朝着错误的方向迁移。具体来说,你需要使用VC++6的原因是什么?也许它可以在使用现代编译器的情况下得到解决?@CodyGray。我的教授很喜欢VC++6.0。他认为它很轻很小,而VS2010看起来太笨重了。所以我不得不这么做提交时请修改我的代码。我会尽力说服他,但他这个年龄的人很固执。从命令行运行cl.exe
更轻量级,更小巧。他根本不需要在IDE中打开你的代码!说真的,你的论点不应该是你更喜欢使用任何特定的开发环境。比如大多数的C++开发者,我也喜欢VC++ 6环境。问题和你需要做的论点是,它捆绑的编译器实在太老了,不能教你有用的,现代C++的实践。@ CODYGRY。谢谢。这个论点更有说服力,我想和我的教授分享。无论如何,我还是好奇它是否是POSS。我可以在VC++6中解决这个问题。话虽如此,我不推荐在严肃的开发中使用它,就像我不推荐任何宏一样。或者你的基于printf的日志风格。或者VC6。或者,从外观上看,你的教授。
#define LOGGER(logfile,loginfo) logfile << loginfo << std::endl
LOGGER(logfile, x);
LOGGER(logfile, "x = " << x << " b=" << b);
// declaration
char spaces[128];
...
// Initialization
memset(spaces, ' ', sizeof(spaces));
#define LOGGER(logfile,indent,loginfo) \
spaces[indent] = '\0'; \
logfile << spaces << loginfo << std::endl; \
spaces[indent] = ' '
LOGGER(logfile, 3, "x=" << x << " y=" << y << " z=" << z);
FILE *g_fp;
size_t g_indent;
void ind_fp(char *format, ...){
INDENT(g_fp, g_indent);
va_list arg_ptr;
va_start(arg_ptr, format);
vfprintf(g_fp, format, arg_ptr);
va_end(arg_ptr);
}
#define IND_FP (g_fp = fp, g_indent = indent, &ind_fp)