Xcode 4, C++, Importing files?

Discussion in 'Mac Programming' started by kristianmoss, Jun 14, 2011.

  1. macrumors newbie

    Joined:
    Mar 17, 2011
    #1
    Hey

    I'm having trouble importing files into my C++ code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <assert.h>  
    #include <math.h>
    #include "Matrix.h"
    
    using namespace std;
    
    int main() { 
        // Open File
        string line;
        
        ifstream myfile;
        
        myfile.open("SizeImage.txt");
        
        
        if (myfile.good())
        {
            while ( ! myfile.eof())
                {
                    myfile >> line;
                    cout << line << endl;
                }
            myfile.close();
        }
        
        else cout << "Unable to open file"<< endl; 
    
            return 0;
            
    }
    The trouble is probably that "SizeImage.txt" is not in the right folder(But I thought it was). So my question is where in Xcode 4 do I specify the folder that I want to use for my "input" folder, And if you could tell me where I specify the output folder as well that would be great.
     
  2. chown33, Jun 14, 2011
    Last edited: Feb 12, 2013

    macrumors 603

    Joined:
    Aug 9, 2009
    #2
    No, the real trouble is that the folder where your SizeImage.txt file resides isn't the current working directory when your program runs.

    Make sure you understand the concepts of working directory and relative pathname (which your code is using).
    http://en.wikipedia.org/wiki/Working_directory
    http://en.wikipedia.org/wiki/Path_(computing)


    There's no such thing as an "input folder" or "output folder" unless you write code that implements those ideas.

    To set the working directory in Xcode 4, try searching Xcode's builtin documentation for keywords working directory.

    You will need to edit the Scheme and set the Working Directory to the folder where your file resides. See this Xcode 4 reference doc, under the subheading "Run Your Application to Debug It":
    http://developer.apple.com/library/...eptual/Xcode4UserGuide/Building/Building.html
    See Figure 5-3.



    You might also use the C function getcwd() to confirm at runtime that the working directory is what you expect.

    For an example, see my replies here:
    http://forums.macrumors.com/showthread.php?t=1162921


    EDIT (2013-02-12)

    The links originally given are broken (now in strikeout). Try this link:
    http://developer.apple.com/library/mac/#recipes/xcode_help-scheme_editor/Articles/SchemeProfile.html

    If When that breaks, try google search terms:
    xcode set working directory site:developer.apple.com
    This will search for the given words, confined to the site developer.apple.com.
     
  3. macrumors newbie

    TastyCocoa

    Joined:
    Mar 21, 2011
    #3
    myfile.open("filepathhere/SizeImage.txt");

    Will also work

    Also make sure you set xcode to compile in release, not debug mode. The GCC iostream debug libraries don't work very well (or at all sometimes).
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Have you tried <cmath>? In C isnan is a macro, in C++ it's an overloaded function. Try cmath and see how it goes.

    -Lee
     

Share This Page