Solved

Fake FILE*

Posted on 2002-05-14
12
542 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

786 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