I need to write such a define in C/C++
#define scanf( fscanf(inf,
in order to replace each scanf(
with fscanf(inf,
literary
But I do not know how...
Thanks
I need to write such a define in C/C++
#define scanf( fscanf(inf,
in order to replace each scanf(
with fscanf(inf,
literary
But I do not know how...
Thanks
You want to use a Variadic macro.
In your case, I believe you want:
#define scanf(...) fscanf(inf,__VA_ARGS__)
You can't replace a parenthesis. If you're using Visual C++, then you can use a variadic macro to accomplish what you want:
#define scanf( format, ... ) fscanf( inf, format, __VA_ARGS__ )
Other compilers may have a similar facility, but I'm not familiar with them.
not possible the way you tried of course.
you have to do things like
#define scanf(S, ...) fscanf(inf, S, __VA_ARGS__)
See e.g. here
EDIT: GNU cpp supports variadic macros too; it is VA_ARGS preceded by double underscore and ended with double underscore... I have to study escaping markup here...
Just write all your code using fscanf, and define the file name with a macro like this:
#ifdef USE_SCANF
#define SCANF_FILE(file) stdin
#else
#define SCANF_FILE(file) file
#endif
fscanf(SCANF_FILE(blah), "%d", &a);
I need to write such a define in C++
No, you don't. What you really want to do is redirect stdin
.
freopen(inf, "r", stdin);