# Writing into file from CommandIterationUpdate

**URL:** <https://discourse.itk.org/t/writing-into-file-from-commanditerationupdate/2467>\
**Category:** Uncategorized\
**Tags:** registration\
**Created:** [December 3, 2019, 2:27pm UTC](https://discourse.itk.org/t/writing-into-file-from-commanditerationupdate/2467 "2019-12-03T14:27:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [December 3, 2019, 2:27pm UTC](https://discourse.itk.org/t/writing-into-file-from-commanditerationupdate/2467/1 "2019-12-03T14:27:36Z")

</div>

Hey Guys, I am running ITK registration examples V4 (`ImageRegistration4.cpp` and the other on a roll), and I have a question:

_How can I write the **outputs** in_ the`CommandIterationUpdate` class at each iteration step into a txt file?  
The outputs are:  
`std::cout << optimizer->GetCurrentIteration() << " ";`  
`std::cout << optimizer->GetValue() << " ";`  
`std::cout << optimizer->GetCurrentPosition() << std::endl;`

I tried creating and opening a file inside `int main()`, but the class `CommandIterationUpdate` does not recognize it.  
Can you guys give-me a help?

Thanks!

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 3, 2019, 2:38pm UTC](https://discourse.itk.org/t/writing-into-file-from-commanditerationupdate/2467/2 "2019-12-03T14:38:55Z")

</div>

Adapted [from this IJ source code](https://www.insight-journal.org/browse/publication/640):

```auto
template<class TFilter>
class CommandIterationUpdate : public itk::Command
{
public:
    typedef CommandIterationUpdate Self;
    typedef itk::Command Superclass;
    typedef itk::SmartPointer<Self> Pointer;
    itkNewMacro(Self);
protected:
    CommandIterationUpdate() {};
public:

    void Execute(itk::Object* caller, const itk::EventObject& event)
    {
        Execute((const itk::Object*) caller, event);
    }

    void Execute(const itk::Object* object, const itk::EventObject& event)
    {
        const TFilter* filter =
            dynamic_cast<const TFilter*>(object);
        if (typeid(event) != typeid(itk::IterationEvent))
        {
            return;
        }

        std::cout << " Iteration " << filter->GetElapsedIterations()
            << " (of "
            << filter->GetMaximumNumberOfIterations()[filter->GetCurrentLevel()]
            << "). ";
        std::cout << " Current convergence value = "
            << filter->GetCurrentConvergenceMeasurement()
            << " (threshold = " << filter->GetConvergenceThreshold()
            << ")" << std::endl;

        // here you could access outputs like this:
        filter->GetOutput(0);
        // I haven't tried this so it might not work
    }
};

int main()
{
    typedef itk::N4MRIBiasFieldCorrectionImageFilter<ImageType, MaskImageType,
        ImageType> CorrecterType;
...
    typedef CommandIterationUpdate<CorrecterType> CommandType;
    typename CommandType::Pointer observer = CommandType::New();
    correcter->AddObserver(itk::IterationEvent(), observer);
}

```

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [December 3, 2019, 3:02pm UTC](https://discourse.itk.org/t/writing-into-file-from-commanditerationupdate/2467/3 "2019-12-03T15:02:38Z")

</div>

I see. But I still did not get how can I write such values into a .txt file.  
I got how `class CommandIterationUpdate` but I do not know how can I say to that class to write such information at each iteration step.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 3, 2019, 3:04pm UTC](https://discourse.itk.org/t/writing-into-file-from-commanditerationupdate/2467/4 "2019-12-03T15:04:41Z")

</div>

That is more of a [C++ question](https://stackoverflow.com/questions/2393345/how-to-append-text-to-a-text-file-in-c) than ITK one.

---

<div class="post-metadata">

**Author:** ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)\
**Post date:** [December 3, 2019, 3:08pm UTC](https://discourse.itk.org/t/writing-into-file-from-commanditerationupdate/2467/5 "2019-12-03T15:08:10Z")

</div>

Yes it sounds so. I’ll look around to see if I can find it.  
Thanks!
