# Root Crashes When Using Ofstream

**URL:** <https://root-forum.cern.ch/t/root-crashes-when-using-ofstream/47356>\
**Category:** Newbie\
**Tags:** io, root\
**Created:** [October 21, 2021, 8:12pm UTC](https://root-forum.cern.ch/t/root-crashes-when-using-ofstream/47356 "2021-10-21T20:12:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spencer-ercanbrack](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spencer-ercanbrack/32/27672_2.png) [@spencer-ercanbrack](https://root-forum.cern.ch/u/spencer-ercanbrack)\
**Post date:** [October 21, 2021, 8:12pm UTC](https://root-forum.cern.ch/t/root-crashes-when-using-ofstream/47356/1 "2021-10-21T20:12:29Z")

</div>

ROOT Version: 6.24.02  
Platform: Windows OS (Windows 10)  
Compiler: MSVC 19.23.28107.0

I am trying to get my code to output some data to a spreadsheet (e.g. I want all of the “time” data points in one one column or row and all of the “reactivity” and “precursors” (etc.) data points in another row so the data can be parsed automatically).

To be honest I just need to get maximum values at their specified times, but as far as I have learned, C++ has no way of retrieving only a maximum or minimum, and I am stuck using the I/O system.

When I try and write the specified variables to a spreadsheet (in my macro) and run ROOT, it crashes. I’ve included a snippet of my code below.

Does anyone have any suggestions? Thank you very much, and I apologize if this is not very well articulated.

```auto
for(long j = 1; j < timeSteps / plotPoints; j++){
			tempTime[j] = tempTime[j - 1] + deltaTime;
			tempPower[j] = tempPower[j - 1] +
								 tempPower[j - 1] * ((tempReactivity[j - 1] - dnf1) / ngt) * deltaTime +
								 tempPrecursors1[j - 1] * pdc1 * deltaTime+tempPrecursors2[j-1]*pdc2*deltaTime;
			tempPrecursors1[j] = tempPrecursors1[j - 1] + tempPower[j - 1] * (dnf1 / ngt) * deltaTime -
								 tempPrecursors1[j - 1] * pdc1 * deltaTime;
			tempPrecursors2[j] = tempPrecursors2[j - 1] + tempPower[j - 1] * (dnf2 / ngt) * deltaTime -
								 tempPrecursors2[j - 1] * pdc2 * deltaTime;
			tempReactivity[j] = initReactivity -
								 tcr * (tempTemperature[j - 1] - initTemperature);
			tempTemperature[j] = tempTemperature[j - 1] +
								 rhc * (tempPower[j - 1]) * deltaTime -
								 htr * (tempTemperature[j - 1] - initTemperature) * deltaTime;

			//cout << "Time: " << tempTime[j] << "\r\n";
			//cout << "Power: " << tempPower[j] << "\r\n";
			//cout << "Precursors1: " << tempPrecursors1[j] << "\r\n";
			//cout << "Precursors2: " << tempPrecursors2[j] << "\r\n";
			//cout << "Reactivity: " << tempReactivity[j] << "\r\n";
			//cout << "Temperature: " << tempTemperature[j] << "\r\n";
			//cout << "\r\n";
			ofstream outfile;
			outfile.open ("example.csv");
			outfile << "time:" <<tempTime[j] << "\r\n";
			outfile.close();
		}

```

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [October 21, 2021, 8:32pm UTC](https://root-forum.cern.ch/t/root-crashes-when-using-ofstream/47356/2 "2021-10-21T20:32:36Z")

</div>

Hi Spencer,

> [@spencer-ercanbrack](#):
>
> it crashes

How does it crash? What is the exact error you see?

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [October 22, 2021, 2:51am UTC](https://root-forum.cern.ch/t/root-crashes-when-using-ofstream/47356/3 "2021-10-22T02:51:50Z")

</div>

If it is a segmentation error it’s probably due to array indices going out of range, but it’s better, as already mentioned, if you provide the error you are getting.

Other things:

> [@spencer-ercanbrack](#):
>
> `for(long j = 1; j < timeSteps / plotPoints; j++)`

is that ratio an integer? while it may not give an error, it may not do the number of iterations you want.

And here:

```auto
    ofstream outfile;
    outfile.open ("example.csv");

    outfile.close();

```

you are opening and closing the file at every iteration, and overwriting it, meaning only the last iteration values will be saved, which may slow down things a lot, and I don’t know if it could be an issue given the speed and number of iterations. In any case, better put these lines outside (before/after as appropriate) the ‘for’ loop.

---

<div class="post-metadata">

**Author:** ![spencer-ercanbrack](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spencer-ercanbrack/32/27672_2.png) [@spencer-ercanbrack](https://root-forum.cern.ch/u/spencer-ercanbrack)\
**Post date:** [October 27, 2021, 6:05am UTC](https://root-forum.cern.ch/t/root-crashes-when-using-ofstream/47356/4 "2021-10-27T06:05:01Z")

</div>

yus,  
my apologies for the ambiguous statement. The root window closes unexpectedly.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [October 27, 2021, 10:18am UTC](https://root-forum.cern.ch/t/root-crashes-when-using-ofstream/47356/5 "2021-10-27T10:18:11Z")

</div>

Hi Spencer,

> [@spencer-ercanbrack](#):
>
> The root window closes unexpectedly.

Do you mean some sort of graphics window (with a histogram maybe) by the “root window”? If so, then the code snippet you posted in the first message does not have anything to do with it, it does not even deal with any histograms.

Do you do anything special to keep the graphics window open? Do you have a minimal reproducer for your issue?
