Save std :: cout to file

0

I need to adapt some exits from the program Ripser . More precisely, I need to do the following:

1. Running the program in an example, we have:

./ripser examples/random16.lower_distance_matrix 

whose output is

distance matrix with 15 points
value range: [1,120]
persistence intervals in dim 0:
 [0,1)
 [0,2)
 [0,4)
 [0,5)
 [0,6)
 [0,7)
 [0,8)
 [0,9)
 [0,10)
 [0,11)
 [0,12)
 [0,14)
 [0,15)
 [0,35)
 [0, )
persistence intervals in dim 1:
 [54,56)
 [43,45)
 [37,51)
 [27,56)
 [22,52)
 [21,52)
 [20,59)
 [19,62)
 [18,62)
 [17,60)
 [16,33)

2. I have interest only in the outputs of the range form, that is, [a,b) or [a, ) . I would like to save them to a file (eg. output.txt ).

3. Looking at the code .cpp I searched for all lines that contain [ , ) , which I present below.

l. 639   std::cout << " [" << diameter << ", )" << std::endl << std::flush;
l. 651   std::cout << " [" << diameter << "," << death << ")" << std::endl << std::flush;
l. 929   if (get_diameter(e) > 0) std::cout << " [0," << get_diameter(e) << ")" << std::endl;
l. 939   if (dset.find(i) == i) std::cout << " [0, )" << std::endl << std::flush;

4. I note that some outputs occur within a loop, such as line 939, which can provide multiple intervals of the [ , ) form, depending on the input file used. Here is the loop block:

#ifdef PRINT_PERSISTENCE_PAIRS
        for (index_t i = 0; i < n; ++i)
            if (dset.find(i) == i) std::cout << " [0, )" << std::endl << std::flush;
#endif

My attempt

For each of the 4 lines mentioned above, I used the code below to write the output to a file, changing only the format within fprintf(fp, " ") .

//! [write to file]
FILE * fp;
fp = fopen ("./output", "a+");
fprintf(fp, "0 0 -1\n");
fclose(fp);

This has worked pretty well, except that within the loop, only one line is written to output, even in instances where they occur on the screen over and over.

Here is the loop block, adapted.

#ifdef PRINT_PERSISTENCE_PAIRS
        for (index_t i = 0; i < n; ++i)
            if (dset.find(i) == i)
                std::cout << " [0, )" << std::endl << std::flush;
                //! [write to file]
                FILE * fp;
                fp = fopen ("./output", "a+");
                fprintf(fp, "0 0 -1\n");
                fclose(fp);
#endif

One of the outputs I had was:

0 0 1.013644
0 0 2.838865
0 0 3.131685
0 0 4.077876
0 0 4.725016
0 0 4.818406
0 0 4.924389
0 0 -1

where we can see the last line, which occurred only once, but should have appeared a dozen times due to for (index_t i = 0; i < n; ++i) .

Any help is welcome.

    
asked by anonymous 17.11.2017 / 01:48

1 answer

2

If it's C ++, use C ++. See:

std::cout << " [" << diameter << ", )" << std::endl << std::flush;

then only:

#include <fstream>
// ...
std::fstream fs;
fs.open("output.txt", std::fstream::out | std::fstream::app);
fs << " [" << diameter << ", )" << std::endl;
fs.close();

The same thing that is printed on the console will be saved in the file.

Tip: In the loop, create the fstream instance and open the file before the loop, just put it inside the loop, and close the file (fs.close ()) when you exit the loop.

    
22.11.2017 / 17:09