• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

Fake FILE*

fwrite(), fprintf() and similar functions take a FILE* as the target device. The FILE* should be a file opened by fopen() or special devices like stdout and stderr.

I want to know if there is a FILE* device that does nothing, so that if I pass it to fprintf(), it does not output anything to any file/device. Can I use NULL to achieve this? ( fprintf(NULL, "hello world"); )
0
chanmo
Asked:
chanmo
  • 4
  • 2
  • 2
  • +3
1 Solution
 
Roshan DavisCommented:
Its not possible

Roshmon
0
 
AxterCommented:
Why are you trying to do this?
0
 
newmangCommented:
What platform are you running on? If it is Unix/Linux you should be able to do the following

FILE * fptr=fopen("/dev/null","w");
if(fptr!=NULL)
   fprintf(fptr,"This goes to bit heaven\n");
fclose(fptr);

but one wonders why you would want to do this?

fprintf(NULL,"blahblah"); won't work - NULL is not a valid file pointer.

Windows/DOS doesn't have an equivalent for /dev/null so unless you use a memory file that disappears when your application terminates I would think you are out of luck.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
chanmoAuthor Commented:
There are 2 separate cases that I want this:

1. Disable some fprintf() statements by setting the FILE* to such a device.

2. Get the number of bytes to be written by the fprintf() call, without writing to a file. For example:

int i = SOME_INTEGER;
int num_bytes = fprintf(fp, "%d", i);

I want to know num_bytes, but do not want the overhead of real file I/O operations. I don't want to use sprintf() because the size can be very large and it's not practical to allocate such a buffer for this.

I know there are ways to get around and get the jobs done, but if such a null device exists it should be the most efficient way.

It'll run on both Windows and UNIX platforms.
0
 
newmangCommented:
Hmmm

Well you should be OK on Unix with /dev/null but I think you are stymied for Windows as event using a memory file will result in the memory being allocated, albeit by the OS.

Of course, if you had TCP/IP on the Windows system you could always open a UDP socket to 127.0.0.1 as a file and use that as your FILE* (127.0.0.1 is the loopback address of your own system so the traffic would not get onto the LAN and annoy others) but it seems like a lot of work.

You may have to write your own version of sprintf which does the formatting but doesn't output anything.

Good luck
0
 
ZoppoCommented:
>Windows/DOS doesn't have an equivalent for /dev/null so unless you use a memory file that
> disappears when your application terminates I would think you are out of luck.
That's not correct: In Windows/DOS there's a NUL-device you can use for this like:
     FILE * fptr=fopen("nul","w");
     if(fptr!=NULL)
          fprintf(fptr,"This goes to bit heaven\n");
     fclose(fptr);

ZOPPO
0
 
ZoppoCommented:
BTW, chanmo, why don't you simply use sprintf() instead of fprintf() and write the
data into a string?
0
 
ZoppoCommented:
sorry, just read that you have two cases :(  ... sprintf could only help you with case 2.
0
 
makerpCommented:
put this at the top of the c file

#define fprintf
0
 
makerpCommented:
after the includes
0
 
chanmoAuthor Commented:
Thank you very much.

If possible, I'd like to split the points between Zoppo and newmang, who suggested /dev/null. But I'll use the code on Windows more, so the points go to Zoppo this time.
0
 
ZoppoCommented:
You're welcome.

If you want I can offer newmang 25 points...

have a nice day,

regards,

ZOPPO
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now