Not able to write to file in Xcode with C++

Discussion in 'Mac Programming' started by kristianmoss, Mar 17, 2011.

  1. kristianmoss, Mar 17, 2011
    Last edited by a moderator: Mar 17, 2011

    kristianmoss macrumors newbie

    Joined:
    Mar 17, 2011
    #1
    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.
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    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
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    Where are you expecting output? In exactly which folder?
     
  4. kristianmoss, Mar 17, 2011
    Last edited: Mar 17, 2011

    kristianmoss thread starter macrumors newbie

    Joined:
    Mar 17, 2011
    #4
    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?
     
  5. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    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
     
  6. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    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.
     
  7. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    So does that effectively
    Code:
    cd $customcwd
    $targetdir/a.out
    Where $targetdir is where the executable is located?

    B
     
  8. jiminaus, Mar 17, 2011
    Last edited: Mar 17, 2011

    jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #8
    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.
     
  9. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #9
    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
     

Share This Page