Solved

Fake FILE*

Posted on 2002-05-14
12
540 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
std::atomic with custom class 6 190
Need some help with listview limit text 20 98
sorting efficency of sorting algorithm 30 103
Installshield for Embarcadero EX 10.1 Berlin 4 20
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now