tags:

views:

756

answers:

2

Here is a simple program to output to a text file:

#include <iostream>
#include <fstream>

using namespace std;

int main() {
double myNumber = 42.5;
fstream outfile("test.txt", fstream::out);
outfile << "The answer is almost " << myNumber << endl;
outfile.close();
}

All that ends up being wrote to my text file is, "The answer is almost " and the data is not displayed at all. What am I doing wrong? or could it be a problem with Xcode since I am using that as an IDE.

+1  A: 

There is no problem with your code. It could be a problem with Xcode.

adatapost
Any idea what that problem may be? Everything will print just fine to a console window so I have no clue whats wrong..
+1  A: 

I'm not sure what the problem is. Is it that it's never executed or that it's writing to the wrong path. To shed light on this try include unistd.h and insert this snippet.

char* s = getcwd(NULL, 256);
printf("im running and pwd is: %s\n", s);

Inside xcode hit CMD-SHIFT-R to open the console and see if it prints anything.

neoneye
it printed "im running and pwd is: and then listed the file directory of my debug folder within my project
Im pretty sure I figured it out haha being new to Xcode and ill i didnt pay attention to the release vs. debug compile because when i changed it to release it worked
great. that have happened to me several times as well :-)
neoneye
Why shouldn't this program run in debug mode?
sbi
Maybe the OP was looking at the file before the stream was flushed (just guessing). The debugger may be holding the program open or somthing???
Martin York