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

kristianmoss

macrumors newbie
Original poster
Mar 17, 2011
5
0
Hey I'm not able to write to file in Xcode 4.0 with c++

I'm completely new to both Xcode and C++. But it seems to me
like it might be some settings that I have wrong.
When I'm writing stuff to be prompted it works fine, but I can't get
Xcode to give me an output. Can you help me?

Simple code:
Code:
#include <fstream.h>

int main () {
    ofstream examplefile ("test.txt");
    
    if (examplefile.is_open())
    {
        examplefile << "This is a line.\n";
        examplefile << "This is another line.\n";
        examplefile.close();
    }
    return 0;
}

This compiles, but there is no output.
 
Last edited by a moderator:

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
That should create a file in your current working directory, which is where Xcode placed the executable it compiled.

Try changing the filename to "/tmp/test.txt" and look in the "/tmp" folder to see if it really isn't being created.

NOTE: Please use CODE tags (the hash mark in the edit window) around any code you post, it makes it much easier to read.

B
 

kristianmoss

macrumors newbie
Original poster
Mar 17, 2011
5
0
Brilliant! Thanks a lot that worked perfectly.

Is there anyway of changing the working directory so it drops the output file
the same folder as the project?

Edit:
I Found out that changing the derived data gets me half the way there. But If I just want to have the output files put back into the project folder and not all the other stuff is there a way of doing that?
 
Last edited:

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
Brilliant! Thanks a lot that worked perfectly.

Is there anyway of changing the working directory so it drops the output file
the same folder as the project?

The C standard library provides (via unistd.h) getcwd and chdir to get and set the current directory. Using those you can a) figure out where your cwd is b) set it to wherever you want.

I'm not sure what you mean by "the same folder as the project" as there are likely many folders within your project.

NOTE: This is one reason why many folks suggest not learning a language and an IDE at the same time. If you wer learning how to use g++ from the command line this would not be an issue. ;)

EDIT: What does "changing the derived data" mean to you? It doesn't mean much to me. ;)

B
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
Is there anyway of changing the working directory so it drops the output file
the same folder as the project?

To the set the current working directory when a program is launched in XCode 4:
  1. Project | Edit scheme
  2. Select Run command on the left
  3. Under Info tab
  4. Use custom working directory
  5. Enter absolute path to the directory. Tildes (~) work.

if you have multiple schemes, you'll need to edit each scheme. That is, your different schemes can have different working directories. Similarly you might need to set the custom working directory on the profile command of your schemes as well.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
So does that effectively
Code:
cd $customcwd
$targetdir/a.out
Where $targetdir is where the executable is located?

Yes. getcwd reports the custom working directory after this change. But the executable is still built under ~/Library/Developer/Xcode/DerivedData/<projectname>-<base64code>/Build/Products/Debug.

BTW I've just found that if you want to go back to the XCode 3.x style of having a build directory under your project directory:

  1. Xcode | Preferences
  2. Locations
  3. Build Location: Place build products in locations specified by targets.
 
Last edited:

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
Yes. getcwd reports the custom working directory after this change. But the executable is still built under ~/Library/Developer/Xcode/DerivedData/<projectname>-<base64code>/Build/Products/Debug.

Thanks, and that explains what the OP was saying about Derived Data. ;)

I will have to d/l Xcode 4 sometime. Unfortunately, the hard drives on all three of my Macs are full to capacity. :(

B
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.