Telling when file was last accessed in C [duplicate] - c

This question already has an answer here:
Determining if file has been copied or not in C [closed]
(1 answer)
Closed 9 years ago.
In Windows, if you go to a file's properties it shows the last access time right under the time last modified. This changes when I copy it.
How do I view this in C?

You can use the GetFileTime() function to get it. This MSDN article has more details about file times.

The portable way of getting the last modified timestamp is by using fstat or stat. If you want to go the Windows-only route (by directly calling a Windows API), see #xxbbcc's answer.
See How can I get a file's size in C++? for a short piece of sample code that uses stat/fstat - only change for your purpose is that you'll want to read the time_t st_mtime field.

Related

fortran function to find available file unit [duplicate]

This question already has answers here:
getting free unit number in fortran
(2 answers)
Closed 7 years ago.
I can write a FORTRAN function to find an available file unit, but I was certain there was already an intrinsic. But if there is, I can't find anything about it. Is there such a thing or am I dreaming?
UPDATE: Apologies for the duplicate. Did a search, but it didn't show up.
I guess, you are looking for newunit (available with F2008, shown at the bottom of that link in the Fortran Wiki).
Ups, has already been answered.
maybe you were thinking of inquire?

What tool do you use to metric performance? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's your favorite profiling tool (for C++)
Instead of do it directly inside the C code, I want a some tool to do it for me. e.g, given some C code, it returns how long time it's was executed. Something like LinqPad and most client that given a SQL-query,it returns how time the query was executed in *conds.
You many try
1)GNU profiler (gprof) for function level profiling
http://www.cs.utah.edu/dept/old/texinfo/as/gprof.html#SEC2
2)For overall time statistics you may use the command,
time
Example
3)You can also try parsing the files in /proc (/proc/[pid]/stat) for a particular process,
proc manual

Obtain filename from file pointer in C [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Getting Filename from file descriptor in C
How get fileName having FILE*?
Is there any way where I can find the file_name from a file-pointer in C?
fp = fopen(file,"r");
From fp, is it possible to get the file name which I have opened?
I believe not, because a file* could be to something that doesn't even have a name. There might be a platform dependent way, depending I. Your system, so if you don't care about portability, try looking at your compiler's definition of FILE if it has one. Odds are that your only way is to save the name when it's opened.
There are some other potentially hacky ways as well- check this link:
http://bytes.com/topic/c/answers/218921-how-get-filename-file-pointer
Cheers!
See here for why this is hard, and can't be done in a portable way.
http://discuss.fogcreek.com/joelonsoftware5/default.asp?cmd=show&ixPost=179112

how to get the memory access type in C/C++ in Linux [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a better way than parsing /proc/self/maps to figure out memory protection?
I have a pointer and I want to check if the referenced memory is readable, writeable and/or executable. How can I do this?
You will have to parse the file /proc/self/maps which contains memory mappings of your process, and check which of the mappings lie within the value of your pointer (if any). The /proc/self/maps file contains the memory protection (read/write/execute).
See this question for more info about the maps file.

Find latest file name in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
Would like to fopen() the latest file in a directory
(with naming scheme file1.txt, file2.txt, file3.txt, etc.)
Is there an API function in Visual Studio for this?
If not, is it a good idea to read in all the .txt file names and sort to get the one I need? Is there a better algorithm I could be pointed to?
Thanks.
I'm going to assume by "latest" you mean "most recently modified file."
There is a C run time library function _fstat and _fstati64 (for large files > 4GB). The function signature for _fstat is:
int _fstat(int file_handle, struct _stat *file_info);
The _stat structure has a bit of useful information about the file, but you likely want the st_mtime member, which has the last modified time as a time_t (time in seconds since 00:00:00 UTC, January 1, 1970).
It may work to use the win32 functions FindFirstFile() and FindNextFile() to walk the directory, store the files in an array of a structure (containing the file name modified time) and then call qsort_s() on the array, sorting by time, in descending order.
I hope that helps.

Resources