Using C++ in XCode 4 - .out file?

Discussion in 'Mac Programming' started by ArtOfWarfare, Sep 14, 2011.

  1. ArtOfWarfare, Sep 14, 2011
    Last edited: Sep 14, 2011

    ArtOfWarfare macrumors G3


    Nov 26, 2007
    Hi, I've already learned C and Obj-C, but I'm taking a class in C++ in school right now. The instructor wants us to send him 3 things:
    1.) The .cpp source code file.
    2.) A screenshot of the output.
    3.) The .out file.

    #1 and 2 are easy enough to get, but I'm confused about #3. What is a .out file and how do I get one in Xcode 4?

    Here's the code, I've highlighted the lines pertaining to making the file in blue.
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    #define HEIGHT 7.0
    int main()
    	[color=BLUE]ofstream outfile; //declare variable to write to file"hw1.out");[/color]
    	double side[2], area, volume;
    	side[0] = 2.0;
    	side[1] = 3.0;
    	area = side[0]*side[1]/2.0;
    	volume = area*HEIGHT/3.0;
    	cout<<"Side 1 is "<<side[0]<<"cm and side 2 is "<<side[1]<<"cm\n\n";
    	cout<<"First pyramid volume is "<<volume<<" cm^3\n\n";
    	[color=BLUE]outfile<<"Side 1 is "<<side[0]<<"cm and side 2 is "<<side[1]<<"cm\n\n";
    	outfile<<"First pyramid volume is "<<volume<<" cm^3\n\n";[/color]
    	cout<<"Enter new lengths for the two sides:\n";
    	area = side[0]*side[1]/2.0;
    	volume = area*HEIGHT/3.0;
    	cout<<"Second pyramid volume is "<<volume<<" cm^3\n\n";
    	cout<<"Side 1 is "<<side[0]<<" cm and side 2 is "<<side[1]<<" cm\n\n";
    	cout<<"The volume of the pyramid to six decimals is:\n\n";
    	cout<<fixed<<setprecision(6)<<volume<<" cm^3\n\n";
    	[color=BLUE]outfile<<"Second pyramid volume is "<<volume<<" cm^3\n\n";
    	outfile<<"Side 1 is "<<side[0]<<"cm and side 2 is "<<side[1]<<"cm\n\n";
    	outfile<<"The volume of the pyramid to six decimals is:\n\n";
    	outfile<<fixed<<setprecision(6)<<volume<<" cm^3\n\n";
    	return volume;
  2. gnasher729 macrumors P6


    Nov 25, 2005
    Your program creates a file named hw1.out.

    Use Spotlight to find it.
  3. ArtOfWarfare thread starter macrumors G3


    Nov 26, 2007
    No results are found.

    (That's not quite true. This webpage shows up as a result, as does my .cpp file.)

    Edit: I changed the search to include system files, it found it in...

    <Home> > Library > Developer > Xcode > DerivedData > <Project> > Build > Products > Debug > hw1.out
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    The same rules apply as for C or Objective-C programs.

    The pathname for your output file is a relative path. It's relative to the current working directory. So if you know the working directory, you know where the file should be. I say "should be" because I don't see any error-checking, and it's possible the output file can't be created or written to. So it's possible that the output file won't exist (or won't be overwritten), even if you know the working directory.

    To find where you set the working directory for an executable running from Xcode, try entering the term into Xcode's help. Or just open the Executable's options window (Xcode < 4) and look carefully in the different tab panes.

    If you don't know what a working directory is, look it up on Wikipedia (along with relative path vs. absolute path).

    Here's a simple C program to print the working directory, pretty much like the 'pwd' command does:
    #include <stdio.h>
    #include <unistd.h>
    // prints working directory
    int main()
    	char buf[ FILENAME_MAX ];
    	char * result = getcwd( buf, sizeof( buf ) );
    	if ( result )
    		printf( "%s\n", result );
    		perror( NULL );
    	return result == NULL;  // 0 on success, 1 on failure
    If you don't know what getcwd() or perror() do, you can look them up in Xcode's API help, too. Or google them, since they're standard Posix C functions.
  5. jiminaus macrumors 65816


    Dec 16, 2010
    For Xcode >=4, you can specify a custom working directory under the options of the run step of the current scheme.
  6. robvas macrumors 68030

    Mar 29, 2009
  7. ArtOfWarfare thread starter macrumors G3


    Nov 26, 2007
    Just a little update for anyone with a similar problem...

    As of Lion, the libraries for users are hidden by default.

    Not just that, but Spotlight won't find things within the hidden libraries even when "System Files are Included".

    It's really irritating how difficult Apple is making it to actually be a developer starting out. Just make the learning curve steeper and steeper...

    Anyways, open terminal and type

    chflags nohidden ~/Library/
    And user libraries will appear again you'll be able to find the files within using Spotlight again (of course, you still need to explicitly say that System Files are Included)
  8. jiminaus, Dec 4, 2011
    Last edited: Dec 4, 2011

    jiminaus macrumors 65816


    Dec 16, 2010
    It's easier with Xcode 4.2. Go into XCode's preferences, go into the locations section, under the locations tab change Derived Data from Default to Relative. Then you'll have a DerivedData subdirectory inside your project directory. All your build artefacts will be under there.

    For example, after changing the preference, if I create a C command-line project called CmdLineTest in my Documents directory, then the executable is at:


    And alternative technique, which also works in XCode 4.0, is to go into the Organizer. In the projects section, click your project. It will show you the path of that project's derived data directory. Click the arrow on the circle next to that path to reveal the directory in Finder. The executable is in the Build/Products/Debug subdirectory.

Share This Page