C++ fstream issue on XCode

Greetings,

A little background info. I have programmed C++ in Windows for the past 2 years, via DevCpp, before I switched to the Mac. I started using XCode v. 3.2. I started with simple test programs, to see if everything is working out.
I come across a little snag where I want to create an output file that spit out numerical values. The build was successful, no errors. However, when I use TextEdit, the values are not displayed/written. I have tried source file on a terminal, and it was a success (both build and the file displayed the values).

Here is the code:

#include <iostream>
#include <fstream>

using namespace std;

int main (int argc, char * const argv[]) {
char FileName20;
cout << "Enter File Name : " << endl;
cin >> FileName;
ofstream fout(FileName);

double a = 5;
double b = 6;

double c = a + b;

fout << "ALL YOUR BASE ARE BELONG TO US!\n";
cout << a << '\t' << b << '\t' << a-b << '\t' << c << endl;
fout << a << '\t' << b << '\t' << a-b << '\t' << c << endl;
return 0;
}
I would appreciate any suggestions/solutions to this scenario. Thank you.

Cheers
Charles

MacBook Pro, Mac OS X (10.6), XCode v. 3.2

Posted on Sep 6, 2009 12:18 AM

Reply
13 replies

Sep 6, 2009 11:07 PM in response to CharlesRussell3

CharlesRussell3 wrote:
How do I go about on checking the permission of the target directory in XCode?

By "checking ... in Xcode" it seems you want to check the perms programmatically (i.e. from the code in your program), is that correct? I guess I'm not sure because the reference to permissions in my previous post was in the context of checking manually--i.e. either using the Finder or the Terminal command line window.

In case you meant the latter, here's a short tutorial I recently posted on checking and setting perms with the Finder: [http://discussions.apple.com/thread.jspa?messageID=10060119&#10060119]. If you're familiar with Unix and wish to use a command in a Terminal window, type: +man chmod+ to see the manual page for the file mode utility.

I don't know if there's a class in the standard C++ library for managing file perms. If your program is just an exercise, and only needs to handle ASCII path names, just use access(), which is a C system call ( man 2 access):

#include <stdio.h>
#include <unistd.h>
// use access() to check for write permission on a directory
const char path[] = "/Users/Charles/Test";
if (access(path, W_OK) == 0)
cout << path << " are belong to us." << />/endl;
else
perror(path);

stat() is a heavier duty alternative ( man 2 stat), but still not what you'd want for an international app. To handle Unicode file names on OS X, you could link to the CoreServices framework and use FSGetCatalogInfo.

For a more informed opinion on the best way to manage file perms in C++, you might want to open a new thread on that subject. Info on how to close your current thread is here: [http://discussions.apple.com/help.jspa#answers]. Of course if you still have problems writing to a disk file with ofstream, please continue this thread. I don't think you said that problem has been resolved.

- Ray

Feb 16, 2010 7:17 PM in response to CharlesRussell3

Here is link that I find helpful when trying to remember how to get Xcode to copy an existing data file into the main bundle of the build:
http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/X codeBuildSystem/200-BuildPhases/bs_build_phases.html#//appleref/doc/uid/TP40002690-CJAHHAJI

Keep in mind that for C++ projects you probably would want the destination to be set to "Executables" instead of "Resources" like you would use in Objective-C projects.

Sep 6, 2009 3:27 AM in response to CharlesRussell3

Hi Charles, and welcome to the Dev. Forums!

I pasted your code directly from the forum into the Xcode C++ Tool template (File->New Project->Mac OS X->Command Line Utility) and it compiled and ran just fine (after I supplied the brackets around FileName\[20\] that had been eaten by the forum). Both the Console output and the disk file were as expected.

With only 20 chars in your buffer, I imagine you just typed a file name at run time instead of a full path, so you might have been looking in the wrong place for the output files. If you ran your program under Xcode (e.g. by clicking Build and Go), you can find the files as follows:
1) Open your project in Xcode;
2) Locate the Products folder in the Groups & Files tree on the left side of your Project window;
3) When you expand that folder, you should see the icon for the Unix Executable File (usually a black rectangle);
4) Ctrl-click on that icon and select _Reveal in Finder_ (If you accidentally choose Open With Finder, the Finder will start your program in a Terminal window);
5) Reveal in Finder should bring up a Finder window showing the contents of the folder which includes your executable. You should see your output files in that window.

If the above doesn't apply, remember you can always use Finder to search for the output files by name. If there really is no output, note that your code isn't trapping any file errors, so check the permissions for the target directory, check for a pre-existing write-protected file of the same name, etc.

Hope that helps!
\- Ray

p.s.: To format your code in future posts, see the yellow alert which is the first topic in the forum. You can see how your post will appear by clicking the Preview tab about the Reply editor panel. - R

Sep 17, 2009 6:00 PM in response to CharlesRussell3

Charles-

Today I had the EXACT same problem while using XCode 3.2 I'm a Mechanical Engineering student taking a C++ class and we wrote a program in class that would calculate 50 values of sine and cosine between zero and 2*PI and output the values to a .txt file created by the program. The file created contained no numerical values! When executed in the Terminal through Unix, the .txt file is created just fine. My teacher looked through everything (he's the one who wrote the program) and tried it both ways and we found that the XCode execution did not ever make the .txt file correctly. Here is the code for the program:

// ME 373 Class
// This program writes a data file with values of sine and cosine in it.
// 17 Sept. 2009

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

int main()
{
double x, dx;
const double PI(acos(-1.0));
double sinx, cosx;
ofstream trig("trig.txt");
//trig.open("trig.txt");

//trig << setw(10) << "X" << setw(10) << "sin(x)" << setw(10) << "cos(x)" << endl;
dx = 2*PI/50;
for (int i = 0; i < 51; i++)
{
x = i*dx;
sinx = sin(x);
cosx = cos(x);
trig << setw(10) << x << setw(10) << sinx << setw(10) << cosx << endl;
trig << "HI!";
cout << setw(10) << x << setw(10) << sinx << setw(10) << cosx << endl;
}

trig.close();
return 0;
}


This is obviously a bug in the compiler that Apple needs to fix, because I'm the second one to post the same error here on this forum. Let me know if you figure anything out Charles or anybody!

Brad

Sep 18, 2009 6:54 AM in response to Bradley.g

When you open a file using just the file name, as in:


ofstream trig("trig.txt");


the file will be created in the current directory. When running an application via Xcode or from the Finder, you don't know where that location will be. If you run from the terminal, your current directory is that shell's current directory. There may be some way to determine where Xcode or the Finder will put files, but it is pretty pointless to even try to figure it out.

Just change the above line to something like:

ofstream trig("/tmp/trig.txt");


and you're done.

Apr 15, 2010 3:45 PM in response to etresoft

The original problem does not seem to have anything to do with the path.
I just encountered the same problem (no numbers written to an fstream). Without putting a path into "file_out.open("data.txt",fstream::out);" the file is created where expected, it just doesn't get any numbers.
It started to work when build target is 10.5 (both debug and release) or 10.6 release. "10.6|debug" seems to be broken .

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

C++ fstream issue on XCode

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.