Hi,I want to redefine fprintf for debugging purposes. That is I want thatall the output that is going to the stdout should be logged in afile.I tried something like#define fprintf (x,y,z) my_fprintf(x,y,z)and the compiler gave about 100 errors. Can anyone help me with this.I have even tried using variadic macros but it seems too complicated.Can anyone just help with such a defination.Thanks and Regards,Prayag Narula 解决方案 Prayag Narula wrote:Hi,I want to redefine fprintf for debugging purposes. That is I want thatall the output that is going to the stdout should be logged in afile.<snip>You cannot portably redefine Standard library functions and symbols. fprintftakes a FILE * argument. Why do you feel that must be stdout. Just pass ita FILE * set to a file, presumably opened by fopen.FILE *log;if((log = fopen("filename", "a")) == NULL) { deal_with_error(); }/* ... */if(fprintf(log, "format string", ...) < 0) { deal_with_error(); }On Jul 23, 1:23 pm, Prayag Narula <[email protected]:#define fprintf (x,y,z) my_fprintf(x,y,z)If there is space between fprintf & (x,y,z) that could be the reasonfor those errors. You need to have it without space "fprintf(x,y,z)".Even better would be "#define fprintf my_fprintf" if x y z remain samewhich I think is highly unlikely as u would be changing replacingstdout with your file pointer.I feel using variadic macros fit better in your scenario as fprintfhas variable arguments. I would to thank you to introduce me thisconcept as was looking for something like this for a long time.. :)"Prayag Narula" <pr***********@gmail.comwrote in messagenews:11**********************@i38g2000prf.googlegr oups.com...Hi,I want to redefine fprintf for debugging purposes. That is I want thatall the output that is going to the stdout should be logged in afile.I tried something like#define fprintf (x,y,z) my_fprintf(x,y,z)and the compiler gave about 100 errors. Can anyone help me with this.I have even tried using variadic macros but it seems too complicated.Can anyone just help with such a defination.If all of your printing to stdout is done with:fprintf( stdout, ...);(that is, using fprintf rather than printf)then it is really easy:#ifdef DEBUG#define OUTFILE myfile#else#define OUTFILE stdout#endifThen all print statements become:fprintf( OUTFILE, ... );Make sure you successfully open myfile beforeany of the fprintf statements are executed.--Fred L. KleinschmidtBoeing Associate Technical FellowAero Stability and Controls Computing 这篇关于重新定义fprintf以进行调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-27 07:40