#define use in C / C ++

I need to write such a definition in C / C ++

#define scanf( fscanf(inf,

to replace each scanf(with a fscanf(inf,literary

But I dont know how...

thank

+5
source share
5 answers

You want to use the Variadic macro .

In your case, I believe you want:

#define scanf(...) fscanf(inf,__VA_ARGS__)
+11
source

I need to write such a definition in C ++

No no. What you really want to do is redirect stdin.

freopen(inf, "r", stdin);
+4
source

fscanf :

#ifdef USE_SCANF
#define SCANF_FILE(file) stdin
#else
#define SCANF_FILE(file) file
#endif

fscanf(SCANF_FILE(blah), "%d", &a);
+3

, , , .

-

#define scanf(S, ...) fscanf(inf, S, __VA_ARGS__)

.

EDIT: GNU cpp also supports variable macros; this is VA_ARGS, which is preceded by a double underscore and ends with a double underscore ... I need to study the markup escaping here ...

+1
source

You cannot replace brackets. If you are using Visual C ++, you can use a variable macro to accomplish what you want:

#define scanf( format, ... ) fscanf( inf, format, __VA_ARGS__ )

Other compilers may have a similar object, but I am not familiar with them.

0
source

All Articles