How can I use the fstream library (C++) in Xcode?

Discussion in 'Mac Programming' started by MihaiBucataru, Mar 5, 2011.

  1. MihaiBucataru macrumors newbie

    Joined:
    Mar 5, 2011
    #1
    I'm new in programming and I've switched from Windows to Mac but I have a problem in programming, I have to do my homework for school in c++ using input and output files :(. Can I do it with Xcode?
     
  2. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    Of course you can. Just choose the c++ template in Xcode.

    Edit: Pick application in the left pane of your "New Project" window, then pick "Comman Line Tool" and lastly, change the type to C++ stdc++.
     
  3. MihaiBucataru thread starter macrumors newbie

    Joined:
    Mar 5, 2011
    #3
    I already did that but I don't know where I can put my input and output files I tried New File/C++ Header File but I don't know why it doesn't work.
     
  4. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    You will need to provide a file path to the files when you opening them for reading. But, honestly with only one source file it's probably less confusing to just use the command line and the g++ compiler.
     
  5. MihaiBucataru thread starter macrumors newbie

    Joined:
    Mar 5, 2011
    #5
    Heh, I would like to be able to do that but the only way to work for school and verify my programs is to send them at school's online evaluator but for that I have to use the fstream library, on MinGW(Windows) I was just
    #include <fstream.h>
    ifstream fin("NAME.in");
    ofstream fout("NAME.out");
    And then add 2 header files NAME.in and NAME.out, I tried the same on Xcode but it's not working :(.
     
  6. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    I does work. The problem you get is that the file is placed where the executable is. Now, you choose to work with an IDE, which means that you executable is inside a folder structure called a project. It's there to keep track of all resources of your enormous project.

    Look inside your project folder, you will find an executable file in there (with a black exec icon) put your files there.

    But as I said, you can use the terminal instead and compile manually.
     
  7. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    Especially since the OP is using MinGW on Windows and not an IDE like Visual Studio.

    OP: What is your workflow on Windows/MinGW?

    Here's a recent thread about going this way. http://forums.macrumors.com/showthread.php?t=1090449

    B
     
  8. MihaiBucataru thread starter macrumors newbie

    Joined:
    Mar 5, 2011
    #8
    You mean at Products? I also tried there but still my output file is empty:(.
     
  9. subsonix, Mar 5, 2011
    Last edited: Mar 5, 2011

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #9
    No. When you create an Xcode project you get a folder somewhere, (desktop, documents or where ever you choose to save it) named after you project.

    Or, you can simply drag out the main.cpp file to your desktop and throw away the project. Then compile manually. Edit: In this case you can still use Xcode as an editor, no problem.
     
  10. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #10
    Exactly, and in this case the OP has to learn both working with C++ and files, AND Xcode.
     

Share This Page