redirecting file IO to nowhere

Hi,

I have a situation where my existing program prints the output to a file. However, I want to redirect the file IO to nowhere. Ideally, I would like to avoid the file IO transactions altogether. For example,

fw_t = fopen("C_ST_B.txt","w") ;
fp_t = fopen("CTP_ST_B.txt","w") ;

My intension is to change the pointer fw_t to point to nowhere. How can I do this? And if I am able to point it to "nowhere", will the performance improve because there is no file I/O?

I appreciate your help in advance.

Thanks,
You will have to check it yourself, probably by setting it to NULL. It will definitely be faster though, since I/O is generally the slowest operation you can perform.
Just a reminder: if you set the file pointer to null and you're actually calling some member functions of FILE somewhere in your code WITHOUT checking whether fw_t is null, you'll get errors, instead of performance boost.
Last edited on
I don't see why you can't just open "/dev/null"
1
2
3
4
5
6
7
8
std::ofstream ofs("/dev/null");

ofs << "This goes to nowhere...";

// And with the C libraries:

FILE* fw_t = fopen("/dev/null", "w");
// ... etc ... 
Topic archived. No new replies allowed.