Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top