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
}
file.close();
outData.close();
}

Recommended Answers

All 4 Replies

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.

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.

thanks alot it is perfect now ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.