Solved

Fake FILE*

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

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

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 30

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 30

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

12 Experts available now in Live!

Get 1:1 Help Now