I have code which outputs .txt files using ofstream in C++ via a helper function. It works fine when no optimisation is set in GCC but when I choose any level of -O1, -O2, or -O3 the debugger in VSCode flags a call stack error (shown in attached image) and fails to write to the specified file.
The function I am using to do this is:
{
std::ofstream name2;
name2.open("C:/Users\name\example_path\"+filedir + name + ".txt");
if(!name2) return 1;
for (start; start<Nx*Ny; start+= step)
{
name2.width(15); name2 << data[start] << std::endl;
}
name2.close();
return 0;
}```
Importantly, the problems are caused **only** when trying to output using this writeToDisk function - if I just write out this code within int main() it works as intended but that is overly verbose and I am outputting many different files at once.
Any help is appreciated, thanks.
[call stack error message](https://i.stack.imgur.com/rFWIu.png)
New contributor
Arthur Scott is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.