Hi there

I'm trying to ofstream (output) data to an excel file (csv), and I keep getting this error "file was not declared in this scope". Can you guys help me on this? where is the error?

This is the output function,

void FileOutput()
    int t;
    static float totallength=0;
ofstream outData("Output1.csv");
if (file.good())
    outData << t <<","<<totallength<<endl; //writing
5 Years
Discussion Span
Last Post by Ahmed2

You want to check the stream - which is outData in your case. That should look like:

if (outData.good ()) {
   // ...
outData.close ();

The file variable is not declared anywhere and that is what the coimpiler is complaining about.

Edited by L7Sqr: n/a


Thank you very much L7Sqr the code writes data fine, but I've got another problem now which is in excel file, where everytime the code writes data, it will overwite what was written in the excel file "firstline". what I wanted is to write data in the first line then the second line and so, where I have the function call in a loop.


You can open the stream in append mode. Something like

ofstream outData("Output1.csv", std::ios::out | std::ios::app);

The std::ios::app will set the insertion point to the end of the file to add to whatever is already there.

Edited by L7Sqr: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.