954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem in ofstream

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();
}
Ahmed2
Newbie Poster
15 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

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.

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

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.

Ahmed2
Newbie Poster
15 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

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.

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

thanks alot it is perfect now ^_^

Ahmed2
Newbie Poster
15 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: