swarm999
swarm999

Reputation: 55

C++ fprintf over many functions to a single text file

I am having difficulty in printing to a text file from chosen locations within a large C++ code project.

Using C++ I am using a function X which is called multiple times.

This is called from a function Y

I wish to output the results of function X to a single text file and have done so by continously using declarations, fopen, fprintf, fclose set of functions - this works – albeit very slowly.

However, I only wish to print results to file when X is called from a specific area of the host function Y.

I am looking to do so, whilist being minimally invasive with the current code (i.e. I wouldn’t like to add another argument to the function X, nor would I like to declare global variables).

Is their a way a unique methods to effectively ‘tell’ the code and child functions when to start printing to file and when to stop.

(p.s. I have post-processed my results using VBA however this workaround is found to be inefficient).

Any ideas or code constructs would be most welcome!

swarm

Below is the child function X:

void `X`() {    
    FILE *f2 = NULL;     
    f2 = fopen("log.txt", "a");     
    // Calculate a result: `result`    
    fprintf(f2, "%.4e   ", result);    
    fclose (f2);
}

Below is the main calling function Y:

void Y Y(){    
    for i=1:100{    
        X();      
        X();      
        X(); // <-- Wishing to print only this line to a text file
    }
}

Upvotes: 0

Views: 770

Answers (3)

j_kubik
j_kubik

Reputation: 6181

If you don't want to keep binary compliance, than you can also transform X function to structure/class with overloaded operator() - then add field or method specyfying if you should print it or not. - however this is quite similar to another global variable. Except overloading X, i doubt there is any other method that doesn't use globals or something similar.

How are you keeping your FILE* pointer between X calls? global? stativ variable in function?

Upvotes: 0

TonyK
TonyK

Reputation: 17114

Whatever you do. it will probably be logically equivalent to declaring a global variable. But you can ease the pain of this in various ways:
If X is a class member, you can declare the variable as a static member of X's class.
If X belongs to a namespace, your global variable can belong to that namespace.
You can declare it as a static variable, local to X's source file, and use a function in the source file to set its value. And so on.

Upvotes: 0

Lou Franco
Lou Franco

Reputation: 89172

Since you're in C++, you can add an overload of X that takes an argument of when to do it, and not have to change any callers.

You have

  void X(args);

Add

  void X(args, bool doIt);

Then, move the code in the original X to the new one, checking doIt.

In the original X, call X(args, false)

Somehow the boolean state of whether to actually log has to be passed. Choices are: an argument, a global, an member variable (static or instance), a thread local variable, or a file.

Upvotes: 1

Related Questions