Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old May 18, 2013, 04:12 PM   #1
macrumors 603
ArtOfWarfare's Avatar
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Access Source File's Location in C Source File?

I have a directory that looks like this:

(somewhere)/Project/code/(all my source files)
(somewhere)/Project/input/(all my input files)
(somewhere)/Project/output/(empty right now)

Normally I just have Xcode copy all of my input files when it builds and any output goes to the console, but this time I want to output an output file for each input file.

(somewhere) is going to change because I'll be zipping this project up and submitting it as a school assignment. How can I ensure that the output folder that's alongside my project is properly output to?

I figure I need to be able to access the source code's location during the build. This sounded to me like a task for the preprocessor. Somehow it's capable of finding files via #include without being told the full path, which suggests to me there must be somewhere that it stores what the full path to the source file is, but thus far Googling hasn't turned up anything helpful.


Edit: Wait... maybe __FILE__ is exactly what I want? Does this include the complete path name?

It's working on my computer with Clang, and from what I can tell it works with MinGW too, so I'm going to mark this as resolved.

2X Edit:

The exact solution to my problem:

I dropped in this header file called tinydir.h:

Then in my program (this was a C++ project... I'm not sure how difficult changing it to straight C would have been.)

const unsigned int FOR_LOOP_MAXIMUM = 500; // a watchdog incase something is going wrong in the for loop

    string sourceFilePath(__FILE__);
    string projectPath = sourceFilePath.substr(0, sourceFilePath.rfind("code"));
    tinydir_dir inputDir;
    tinydir_open(&inputDir, (projectPath + "input").c_str());
    for (unsigned int i = 0; i < FOR_LOOP_MAXIMUM && inputDir.has_next; i++)
        tinydir_file inputFile;
        tinydir_readfile(&inputDir, &inputFile);
        string inputFilePath(inputFile.path);
        if (inputFilePath.find(".input") != string::npos)
                // read in the file
                ifstream fin(inputFilePath);
                knapsack knapsack(fin);

                // solve the problem
                // when it returns, put the output in a file
                string outputFilePath(inputFilePath);
                replaceLast(outputFilePath, "input", "output");
                replaceLast(outputFilePath, "input", "output");
                ofstream fout(outputFilePath, ofstream::trunc);
            } catch (exception e) {
                // I put the try-catch inside the for loop so that if one file
                // throws an exception, the entire program won't abort
                cout << "Exception Caught! Continuing..." << endl;
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)

Last edited by ArtOfWarfare; May 18, 2013 at 10:37 PM.
ArtOfWarfare is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Missing Source File - Lightroom 5 pilotkid Digital Photography 4 Mar 15, 2014 05:05 PM
Input source selector does not show when switching the input source Raizen.Z09 OS X Mavericks (10.9) 0 Mar 12, 2014 07:49 AM
source of a file AbdullahTammour OS X 10.8 Mountain Lion 7 Aug 19, 2012 07:44 PM
keep getting error when trying to make cmus from source: file was built for i386 whic mandorle OS X 1 Jul 1, 2012 11:30 PM

Forum Jump

All times are GMT -5. The time now is 07:15 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC