I’m trying to use an Aug20th 2005, how to set file access time, here on DaniWeb.

I need to modify the file times for a “C”, not C++, Visual Studio 5, console ap that I am working on.

Here is the exact code for the project:

#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <string.h>

void main(void)
{
	FILE *fd;
	int ok;
	HANDLE hFile;
	FILETIME createTime;
	FILETIME accessTime;
	FILETIME writeTime;
	fd = fopen("test.txt","a+");
	fclose(fd);

	hFile = CreateFile((LPCTSTR)"test.txt", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0); 
	printf(" %d %d\n",hFile,GetLastError());
	GetFileTime(hFile,&createTime,&accessTime,&writeTime);
	writeTime.dwHighDateTime -= 1000L;
	accessTime = writeTime; 
	ok = SetFileTime(hFile,&createTime,&accessTime,&writeTime);

	printf(" %d %d\n",ok,GetLastError());

	CloseHandle(hFile);

}

The fopen/fclose does create the file.

The printf after the CreateFile shows a handle of -1 and an error of 2 (File not found), but the file is there!

I’ve run the code with and without the LPCTSTR casting. It makes no difference.

The last printf naturally returns 0 (fail) 6 (Illegal handle) because create file failed!

Need to be fully qualified?

Your program needs additional error checking -- it works ok for me

#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <string.h>
#pragma warning(disable: 4996)

int main(void)
{
	FILE *fd;
	int ok;
	HANDLE hFile;
	FILETIME createTime;
	FILETIME accessTime;
	FILETIME writeTime;
	fd = fopen("test.txt","a+");
	fclose(fd);

	hFile = CreateFile((LPCTSTR)"test.txt", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0); 
	if( hFile == INVALID_HANDLE_VALUE)
	{
	printf("CreateFile failed: %d %d\n",hFile,GetLastError());
	}
	else
	{
	if( GetFileTime(hFile,&createTime,&accessTime,&writeTime) != 0)
	{
		writeTime.dwHighDateTime -= 1000L;
		accessTime = writeTime; 
		ok = SetFileTime(hFile,&createTime,&accessTime,&writeTime);
		if( ok != 0)
		{
			printf("SetFileTime success\n");
		}
		else
		{
			printf("SetFileTime error: %d\n",GetLastError());

		}
	}
	CloseHandle(hFile);
	}
}

It shouldn't have to be, but I will test that.

it shouldn't have to be what??? You always need to do error checking to make sure win32 api functions succeed.

What operating system did it work on? I found out about this problem on a Windows 7 64 bit system. Haven't tried it at home, XP 32 bit. I'll do that tonight.

Sorry, it shouldn't have to be a fully qualified filename, C:\...\filename" I was answering the previous post

I work on 64-bit Windows 7

Maybe it's a security issue.
We are totally secure here. Everythign locked down

Have you tried running my version of your program? If yes, did it work? Since the file is created in the current working directory I don't see how it could be a security issue. If it was a security issue then you would not be able to compile the program.

When you created your project, was it an empty project or with pre-compiled headers?

Mine was empty. The source I posted was the only source in the project.

Mine did not use precompiled headers. I normally always turn those off. But your problem seems to be a runtime, not compiletime, so that won't make any difference. Also, I'm using VC++ 2010 Express, not vc 5. If that's the compiler you are using then upgrade it because that compiler is 20 years old now and full of bugs.

I just ran it twice, actually 3 times.

Once as is with my original empty project, then with a new project with pre-compiled headers, and then with the pre-compiled headets and changed the filenames to "C:\\test.txt". fopen created the file, but all 3 fail. Has to be something special about our security stuff, the way our operating system is set up here.

Like I said, I'll try it again at home tonight.

Andrew Tannenbaum said it best:

"The nice thing about standards is that there are so many to choose from."

Don't put the file in c:\ root directory because Windows 7 doesn't like it. Make a subfolder and put the file there. I always do my development work from c:\dvlp folder. Don't like having it in c:\Users\<name> because the path is just too long.

I just changed both paths to "C:\\\\Users\\My.Name\\Desktop\\LogFiles\\test.txt"

The file appeared in the directory on my desktop, fopen did that, but still failed.

Like I said, I'll try it again at home tonight.

thanks for your help, but this is a wierd one. Here is the whole story:


This is an old "C", not even "C++", console ap that runs fine on Windows XP. I gave it to a friend who has used it a while then switched to Window's 7 which does not update last access time without a system patch.

It is an SSI (Server Side Include) emulator. You modify full HTML files in the Client directory, then “Update” your site. It compares the timestamps of the SHTM files in the Server directory to HTM files in the Client directory to figure out what you did. It re-extracts the “flagged” included files, and then uses timestamps of all the files to figure out which Client files require re-building.

Windows 7 doesn’t update access times to minimized disk IO. I need to be in control of the file timestamps so I don’t have to rely on OS changes.

It gives you all the benefits of SSI without the need for a server to preview your pages. If anyone is interested in it, just let me know.

This has to be the operating system here. I just created a parallel project, different name, in Visual Studio 2008. Same results using a .cpp file. Visual Studio 2005 used a .c file.

Has to be somethign about permission of something.

Works fine at home, will investigate further

Sorry for not following up!
The code works fine at home on XP, but fails at my 9 to 5 on Windows7. Ours is a 64 bit system and totally locked down, so that is probably the issue.
If I do find somethign that works, I will post it here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.