Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

thedude563855

macrumors newbie
Original poster
Hey, so I'm new to C++ and XCode and I'm wondering why the following code won't work:

#include <iostream>
#include <fstream>

using namespace std;

int main (int argc, char * const argv[]) {
ofstream file;
file.open("test.txt");
file << 30;
}

Nothing is being output to the file. If I replace the int 30 with a string "30", it works fine. Everything I can find online says that this should work, so I have no idea what the problem is. Thanks.
 
Works for me.

Which version of OS and Xcode?

When you say "nothing", do you mean no file is created, or the file is created but empty?

How do you run the program?

Where are you looking for the file? Is the program's working directory what you think it is?

If unsure what your working directory is, add:
Code:
#include <unistd.h>

...
char buf[1024];
cout << getcwd(buf,sizeof(buf)) << endl;
...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.