PDA

View Full Version : C++ ofstream not outputting variables




upperlacon
Nov 7, 2009, 07:01 AM
Hi, I signed up because I was having no luck finding an answer with this simply with google searches.

My code is in C++ it compiles, runs and outputs fine in Visual Studio 2008 on a PC. Ive put it in xcode, it compiles and runs fine, but does not seem to want to output any of my double variables to the text files. To test anything would output, I simply got it to output some text to the same file. Worked fine, just seems not to want to output variables, frustrating as it worked fine on VS2008.

Is there something I'm missing here?

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main()
{

cout << "Started" << endl;

//euler method
// declaring variables
double y0 = 0.1, y1 = 0, g = 0, h = 0.1, y00, y11, t = 0, en;

//energy
en = (1 - cos(y0) ) + ( ( y1*y1 ) / 2);

//output
ofstream euler("euler.txt");
ofstream write("write.txt");
write << y0 << endl;
euler << t << '\t' << y0 << '\t' << y1 << '\t' << en << endl;

for(int i = 0; i < 1000; i++){

//storing nth
y00 = y0;
y11 = y1;

//calculating n+1th
t += h;
y0 += ( h * y11 );
y1 -= ( ( h * y00 ) + ( h * g * y11 ) );

//energy
en = (1 - cos(y0) ) + ( ( y1*y1 ) / 2);

//output
euler << t << '\t' << y0 << '\t' << y1 << '\t' << en << endl;

}

cout << "Euler Method Complete" << endl;
}



Ti_Poussin
Nov 7, 2009, 11:19 AM
I try it on my computer since I couldn't find the problem visually.

There's no problem it work, make sure you have the permission to create those file inside the folder where you run your program. You can verify the file openned correctly:
if(!euler.is_open())
{
std::cout << "File cannot be created" << std::endl;
}

Here's a few thing, you have a int main() that return nothing = BAD, return 0 at the end please.

using namespace std;

You should not use it this way, it's a bad pratice:
std::cout << ...
std::cin >> ...

You don't close the file at the end
euler.close()
write.close()

Ti_Poussin
Nov 7, 2009, 11:27 AM
If the file is created and the double aren't output and it's the only problem, I would like to know how you compiled your code. I simply did a
g++ -Wall -pendantic-errors code.cc -o test

-Wall : warn on most warnings which are mostly bad code
-pendantic-errors : turn warning into error, cause it's often the case!

They should teach to use those flag at school, code that doesn't pass this often leak and/or have problem. It's rare particular case that may trigger a false warning, but believe me, that it's extremely rare.

I don't know what problem you encountered, cause your code is working. Ok, some beginner errors in it but still working, try to move it to /tmp folder and test it there to ensure permission are ok.

ncl
Nov 7, 2009, 02:02 PM
If you're using Xcode 3, your problem may be caused by gcc 4.2 and debug mode. Here is a way to fix it:
http://forums.macrumors.com/showpost.php?p=8590820&postcount=8

Here's a few thing, you have a int main() that return nothing = BAD, return 0 at the end please.
(...)
You don't close the file at the end
euler.close()
write.close()
Note that none of these things are necessary. However, the return 0 is probably considered a good practice.

upperlacon
Nov 8, 2009, 05:11 AM
If you're using Xcode 3, your problem may be caused by gcc 4.2 and debug mode. Here is a way to fix it:
http://forums.macrumors.com/showpost.php?p=8590820&postcount=8

Note that none of these things are necessary. However, the return 0 is probably considered a good practice.

legend!

i followed your advice and that has sorted my problem.



Here's a few thing, you have a int main() that return nothing = BAD, return 0 at the end please.

using namespace std;

You should not use it this way, it's a bad pratice:
std::cout << ...
std::cin >> ...

You don't close the file at the end
euler.close()
write.close()

I don't really get taught much of the reasons of coding in a particular way, because I study physics and they put less of an emphasis on that. However I would rather try not to fall into bad habits so out of interest why is it better to use std:: instead of using namespace std at the beginning? Also why do you need to close the file?

Thank you both for your help!

ScoobyMcDoo
Nov 8, 2009, 08:26 AM
legend!

why is it better to use std:: instead of using namespace std at the beginning?



In a program this size, the "using" statement is fine. In larger projects, it's better to fully qualify namespaces in each line of code because you may be using multiple libraries which may have duplicate symbols. The reason for namespaces is to deal with the problems of having duplicate symbols in different libraries - the "using" statement defeats the purpose.

legend!

Also why do you need to close the file?



Just good practice, however when using the stl library as you are, it is unnecessary as the file object will close the file for you when it goes out of scope, which in your case is when the program ends.

One other piece of advice I would give you is don't use the variable name "write" since "write" is a function name in the standard c library.

Sander
Nov 11, 2009, 03:39 PM
We're entering the realm of personal preference here, but I'm inclined to say that a "using namespace std;" on top of your .cpp file is just fine. A problem arose when people thought "I'm typing that at the top of all my .cpp files, I might as well put it in the header file." And that, of course, defeats the purpose of namespaces.

My view: Namespaces are there to solve a problem (name clashes). If you're not having the problem, you don't need to be bothered with them.