0

I added some commands to produce an output file in methods of a given class. This worked perfectly well, and output file was produced during execution. Now that I made major changes to the code (but not at all to the output file commands), I am not producing output correctly anymore? Where can this come from? The code I changed seems not have any connexion to output commands. Constructor looks like

solverMethod::solverMethod(solverInput*inp_):solverMethod(inp_) { ndim = m_input->getNbParams(); bestFuncEval = DBL_MAX; NMAX = m_input->getMaxIter(); FTOL = m_input->getTolerance(); NITER = 0; logMode = true; osOutput.open("F://Output.txt") ; 

}

where member boolean logMode decide whether comments are active or not.

In different methods, I have code like

if(logMode) { osOutput << "\n"; osOutput << " - - BUILD "; osOutput << "\n"; osOutput << "INITIAL"; osOutput << "\n"; for(int k=0;k<npts;k++) { for(int j=0;j<m_ndim;j++) { osOutput << s_[k][j] ; osOutput << ", "; } } osOutput << "\n"; } 

to produce comments

and then I have at the end of major method:

 // .... osOutput.close(); return true; } 

Do you have any further info

4
  • 1
    Let me get this clear, you open the file in the constructor, but close it in some other function? After calling that function (which closes the file), do you try to write to the file again? Commented Apr 5, 2012 at 6:36
  • 1
    It is hard to answer this question without a complete example. Try throwing everything out of your code that does not relate to managing (that means opening and closing) the output-file. This should reduce your code to a size you can post here. Commented Apr 5, 2012 at 6:42
  • 1
    solverMethod::solverMethod(solverInput*inp_):solverMethod(inp_) - this looks strange does this even work? Do you have a member named "solverMethod" or a base class which is named "solverMethod"? If I try that with g++ it displays an error. What compiler are you using? Commented Apr 5, 2012 at 6:46
  • 1
    It isn't just because you're using forward slashes in your path instead of backslashes? That is, try "F:\\Output.txt" instead of F://Output.txt" - or is it just a typo? Commented Apr 5, 2012 at 7:22

1 Answer 1

1

What is the status of the stream after the open? You almost always want to check whether the open succeeded (and whether all of the writes succeeded after the close). You might try something like:

osOutput.open(...); if ( !osOutput.is_open() ) { std::cerr << "Cannot create ..., error was: " << strerror( errno); } 

This will give you more information about what is wrong.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.