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

Reply
 
Thread Tools Search this Thread Display Modes
Old May 18, 2013, 05:12 PM   #1
ArtOfWarfare
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
(somewhere)/Project/project.xcodeproj
(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.

Suggestions?

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:
https://github.com/cxong/tinydir

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

Code:
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)
        {
            try
            {
                // read in the file
                ifstream fin(inputFilePath);
                knapsack knapsack(fin);

                // solve the problem
                greedyKnapsack(knapsack);
                
                // 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);
                knapsack.putSolutionIn(fout);
                fout.close();
            } 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;
            }
        }
        
        tinydir_next(&inputDir);
    }
    
    tinydir_close(&inputDir);
}
__________________
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 11:37 PM.
ArtOfWarfare is offline   0 Reply With Quote

Reply
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 06:05 PM
Input source selector does not show when switching the input source Raizen.Z09 OS X Mavericks (10.9) 0 Mar 12, 2014 08:49 AM
source of a file AbdullahTammour OS X 10.8 Mountain Lion 7 Aug 19, 2012 08:44 PM
keep getting error when trying to make cmus from source: file was built for i386 whic mandorle OS X 1 Jul 2, 2012 12:30 AM

Forum Jump

All times are GMT -5. The time now is 04:09 PM.

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

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