Create a File - Troubles

Hello everybody!

I am new to c++ and have a (curious) problem while writing a file using the ofstream class. I am working on Monte Carlo simulation but my problem only concerns writing numbers in file. Indeed I have no problem for writing text in a file but I can't do anything with numbers (without having an error while compiling). Here is my code :

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


int main (int argc, char * const argv[]) {

double Nb=100;

ofstream outf;
outf.open("test.txt");
outf << "The number is: " << Nb << endl;
outf.close();

return 0;
}

When I do that, I just have "The number is:" written in my file. So I first thought the ofstream class didn't allow to write two "expressions" in one command line, so I wrote the next file:

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


int main (int argc, char * const argv[]) {

double Nb=100;

ofstream outf;
outf.open("test.txt");
outf << Nb << endl;
outf.close();

return 0;
}

And I have absolutely nothing in my file!!! Simply great! So I don't really understand where the problem is. Could anyone help me? Please!! One thing that might be useful to know is that I use Xcode to code (with a native GNU/GCC compiler).

Thanks for your help!!
I compiled your code on my PC and it works as expected (i.e. the number is correctly written in the file).

Can you try to remove the unnecessary #include lines and leave only <fstream> ? It shouldn't impact the result but we never know...

Anyway this is probably a bug in the compiler or the C++ library you are using. Try to update...




Last edited on
Thanks to have tried to solve my problem! I still don't know how to fix it but I found another way to get my results quiet easily.
Topic archived. No new replies allowed.