Solved

Fake FILE*

Posted on 2002-05-14
12
544 Views
Last Modified: 2008-03-03
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
Comment
Question by:chanmo
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7007925
Its not possible

Roshmon
0
 
LVL 30

Expert Comment

by:Axter
ID: 7007930
Why are you trying to do this?
0
 
LVL 4

Expert Comment

by:newmang
ID: 7007940
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!

 

Author Comment

by:chanmo
ID: 7007963
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
 
LVL 4

Expert Comment

by:newmang
ID: 7007980
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
 
LVL 31

Accepted Solution

by:
Zoppo earned 50 total points
ID: 7008014
>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
 
LVL 31

Expert Comment

by:Zoppo
ID: 7008018
BTW, chanmo, why don't you simply use sprintf() instead of fprintf() and write the
data into a string?
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 7008021
sorry, just read that you have two cases :(  ... sprintf could only help you with case 2.
0
 
LVL 10

Expert Comment

by:makerp
ID: 7008174
put this at the top of the c file

#define fprintf
0
 
LVL 10

Expert Comment

by:makerp
ID: 7008175
after the includes
0
 

Author Comment

by:chanmo
ID: 7026411
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 7026583
You're welcome.

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

have a nice day,

regards,

ZOPPO
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 79
PDF library for Delphi 2 135
IdTCPClient1->Disconnect(); not working 3 86
why doesn't assert terminate here? 2 11
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question