Getting the modification date of a file

Discussion in 'Mac Programming' started by ruimac, Oct 1, 2014.

  1. ruimac macrumors newbie

    Joined:
    Sep 24, 2014
    #1
    I have a file in a folder. I have the path to it in a string.
    How can I get the modification date of that file?

    Is there any platform independent solution?
    Because I need to make it work on MacOS and Windows (32bit and 64bit).
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
  3. ruimac thread starter macrumors newbie

    Joined:
    Sep 24, 2014
    #3
    Oh, sorry, I forgot to tell.
    I'm using C++.
    I'm coding Cinema 4D plugins. And I need to compile them in Xcode, for Mac and in Visual Studio, for Windows.
    So, any cross platform solution is preferable.
    Will this work in C++?

    Oh, by the way, my path is in this format:

    /Applications/Maxon/Cinema4D 14 - DEV/plugins/PolyPaintTag/res/reg.txt
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    C functions can be called from C++.

    I googled the search terms windows file modification date and it readily found the WinAPI function GetFileTime:
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms724320(v=vs.85).aspx

    I suggest writing a function that contains a #if/#elif/#endif construct (standard C & C++; you can look it up in any decent reference). Then put each OS's platform-specific code in the appropriate section. That way you've encapsulated the platform-dependent part into a single function, and all the rest of your code calls a single function with a single API.

    This wrapper function should also account for any differences in how the date/time values are represented. That is, the Posix time representation will differ from the Windows date/time representation. Here's the Posix info. You'll have to look up the Windows info.
    http://en.wikipedia.org/wiki/Unix_time

    To summarize, write your own function and use the usual well-known techniques for encapsulating the platform dependencies.
     
  5. ruimac thread starter macrumors newbie

    Joined:
    Sep 24, 2014
    #5
    It will be harder than what I expected, then :-(
    But I will not give up.
    On my Mac, I have this now:
    Code:
    struct tm* clock;
    struct stat attrib;
    string sfilename;
    String reg_filename;
    	
    Filename reg_filename = GeGetPluginPath() + Filename("res") + Filename("reg.txt"); // returns a String...
    	
    sfilename=tostr(reg_filename.GetString()); // convert it to string
    	
    // I get an error in the next line.
    // the error is: No matching function for call to 'stat'
    stat(sfilename, &attrib);	
    clock = gmtime(&(attrib.st_mtime));
    
    What could be wrong?
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    First, stat() is a C function. Therefore it's in the "C" namespace, as I recall. You didn't show any includes or declarations for it, so I have no way of knowing how you declared the function. If you didn't declare it, the compiler won't know what you're referring to.

    Second, you should include the relevant header file: see the man page I previously linked to. The #include must also ensure that the functions declared therein are in the "C" namespace, otherwise you'll still get an error.
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    std::string is not a c string. Use .c_str() to get a c string you can pass to stat.

    -Lee
     

Share This Page