ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Tiny 'Touch' 32 bits program in C (3 kb)

(1/6) > >>

Gerome:
Hello,

For people who are aware of having an Unix like 'Touch' program, here are the sources + into the zipped attachement, a BAT to compile the source, the compiled source (a 3Kb one) :)


--- Code: C ---// ********************************// Author : Gerome GUILLEMIN// Coded in pure C using LCC Win32// Date : 29 th of September 2006// ********************************#include <windows.h>#include <stdio.h> BOOL SetFileToCurrentTime(HANDLE); int main(int argc, char **argv){        HANDLE lngHandle = NULL;        char szFileName[MAX_PATH+1];        if ( argc == 2 ) {                strcpy( szFileName, argv[1] );            lngHandle = CreateFile(szFileName, GENERIC_WRITE,                                        FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);                if (lngHandle && lngHandle != INVALID_HANDLE_VALUE) {                        SetFileToCurrentTime( lngHandle );                        CloseHandle( lngHandle );                        return 1; // => 1 will be the OK return                }        }return 0; // => 0 will be the KO return} BOOL SetFileToCurrentTime(HANDLE hFile){  FILETIME ft;  SYSTEMTIME st;   GetSystemTime(&st);                 // gets current time  SystemTimeToFileTime(&st, &ft);     // converts to file time format  return SetFileTime(hFile,           // sets last-write time for file         (LPFILETIME)NULL, (LPFILETIME)NULL, &ft);}

f0dder:
You really should return '0' since that's the standard returncode for success...

Gerome:
Hello,

You really should return '0' since that's the standard returncode for success...
-f0dder (September 29, 2006, 04:50 PM)
--- End quote ---

Now you got the sources, you can do it :)
Personally I prefer telling 1 that is equal to True when successfull and 0 that is equal to 'False' when failed... It's just another point of view!

f0dder:
Now you got the sources, you can do it :)
Personally I prefer telling 1 that is equal to True when successfull and 0 that is equal to 'False' when failed... It's just another point of view!
-Gerome (September 29, 2006, 06:57 PM)
--- End quote ---
Not if you ever have to deal with makefiles or the like.
(emphasis is mine).

Jibz:
The idea in returning 0 on success and a non-zero value in case of failure is of course that if the program succeeds we generally aren't interested in why, whereas with a failure it's nice to know why.

So a program can return 1 for running out of memory, 2 for pointer error, 3 for .. etc. That way you can check if there is an error by looking at if the value is zero or non-zero, and if you need to know more about what caused the error you can look at the actual value returned :Thmbsup:.

The C standard defines two macros, EXIT_SUCCESS and EXIT_FAILURE, which can be used as appropriate return values to the operating system. They are respectively 0 and 1.

Navigation

[0] Message Index

[#] Next page

Go to full version