how to touch a file in C? 
Author Message
 how to touch a file in C?

Quote:

> Hi all,

>         I am trying to writing a function that would touch a certain while every
> few minutes or so. I wonder what is the easiest way to touch a file in the
> code? There doesn't seems to be functions that can allow you to change the
> date of a file. To make it more difficult, more function has to work on
> both DOS and Macintosh. Can anyone help?

Hi Ken Chan,

You could open the file for reading/writing, read the first byte, write
it again, then close the file. That should do the trick and update the
"modified" time of the file.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sat, 29 Jan 2000 03:00:00 GMT  
 how to touch a file in C?

Hi all,

        I am trying to writing a function that would touch a certain while every
few minutes or so. I wonder what is the easiest way to touch a file in the
code? There doesn't seems to be functions that can allow you to change the
date of a file. To make it more difficult, more function has to work on
both DOS and Macintosh. Can anyone help?

Thanks

Ken



Sat, 29 Jan 2000 03:00:00 GMT  
 how to touch a file in C?


Quote:
>    I am trying to writing a function that would touch a certain while every
>few minutes or so. I wonder what is the easiest way to touch a file in the
>code? There doesn't seems to be functions that can allow you to change the
>date of a file. To make it more difficult, more function has to work on
>both DOS and Macintosh. Can anyone help?

#include <sys\utime.h>
int utime( const char *path,
           const struct utimbuf *times );

struct utimbuf {
    time_t   actime;    /* access time */
    time_t   modtime;   /* modification time */

Quote:
};

Description:

The utime function records the access and modification times for the
file identified by path.

The _utime function is identical to utime.  Use _utime for ANSI naming
conventions.

If the times argument is NULL, the access and modification times of
the file or directory are set to the current time.  Write access to
this file must be permitted for the time to be recorded.

If the times argument is not NULL, it is interpreted as a pointer to a
utimbuf structure and the access and modification times of the file or
directory are set to the values contained in the designated structure.
The access and modification times are taken from the  actime and
modtime fields in this structure.

Returns:

The utime function returns zero when the time was successfully
recorded.  A value of -1 indicates an error occurred.

Errors:

When an error has occurred,  errno contains a value indicating the
type of error that has been detected.

Constant

Meaning

EACCES

Search permission is denied for a component of path or the times
argument is NULL and the effective user ID of the process does not
match the owner of the file and write access is denied.

EINVAL

The date is before 1980 (DOS only).

EMFILE

There are too many open files.

ENOENT

The specified path does not exist or path is an empty string.

Example:

#include <stdio.h>
#include <sys\utime.h>

void main( int argc, char *argv[] )
  {
    if( (utime( argv[1], NULL ) != 0) && (argc > 1) ) {
       printf( "Unable to set time for %s\n", argv[1] );
    }
  }

Classification:

utime is POSIX 1003.1

It may also be sufficient to just open the file ( maybe for writing )
and close it, but I didn't verfiy this.

ByE, MarKus.

( To reply by e-mail remove the 'R' in my e-mail address )



Thu, 03 Feb 2000 03:00:00 GMT  
 how to touch a file in C?

Quote:


> >       I am trying to writing a function that would touch a certain while every
> >few minutes or so. I wonder what is the easiest way to touch a file in the
> >code? There doesn't seems to be functions that can allow you to change the
> >date of a file. To make it more difficult, more function has to work on
> >both DOS and Macintosh. Can anyone help?
> #include <sys\utime.h>
> int utime( const char *path,
>            const struct utimbuf *times );
> struct utimbuf {
>     time_t   actime;    /* access time */
>     time_t   modtime;   /* modification time */
> };
> Description:
> The utime function records the access and modification times for the
> file identified by path.
> The _utime function is identical to utime.  Use _utime for ANSI naming
> conventions.

Nrither of these is ANSI/ISO Standard and is not guraenteed to work on
any particular platform/OS.

- Show quoted text -

Quote:
> If the times argument is NULL, the access and modification times of
> the file or directory are set to the current time.  Write access to
> this file must be permitted for the time to be recorded.
> If the times argument is not NULL, it is interpreted as a pointer to a
> utimbuf structure and the access and modification times of the file or
> directory are set to the values contained in the designated structure.
> The access and modification times are taken from the  actime and
> modtime fields in this structure.

> Returns:

> The utime function returns zero when the time was successfully
> recorded.  A value of -1 indicates an error occurred.

> Errors:

> When an error has occurred,  errno contains a value indicating the
> type of error that has been detected.

> Constant

> Meaning

> EACCES

> Search permission is denied for a component of path or the times
> argument is NULL and the effective user ID of the process does not
> match the owner of the file and write access is denied.
> EINVAL
> The date is before 1980 (DOS only).
> EMFILE
> There are too many open files.
> ENOENT
> The specified path does not exist or path is an empty string.
> Example:
> #include <stdio.h>
> #include <sys\utime.h>

> void main( int argc, char *argv[] )

main returs and int.  We try to avoid this particular non-standard
declaration, especially since it only 'works' on one platform, you
should have:

int main( int argc, char **argv )
or
int main( void )

If only for the newbies who might confused and try to use something like
this.  I posted this response for just this reason.

Here you should check for the number of arguments:

   if( argc < 2 ) show_syntax(); /* I leave the coding of this function
as an
                                   exercise for the reader. */

Quote:
>   {
>     if( (utime( argv[1], NULL ) != 0) && (argc > 1) ) {
>        printf( "Unable to set time for %s\n", argv[1] );
>     }
>   }
> Classification:
> utime is POSIX 1003.1
> It may also be sufficient to just open the file ( maybe for writing )
> and close it, but I didn't verfiy this.

If the poster has utime() this concept will work.  It is NOT guarenteed
to be portable.

The bottom line is that manipulating file times is an OS specific
function, you will probably need to customize a different function for
each OS you port to.

For more in-depth information try:
 comp.os.msdos.programmer           DOS O/S Related Issues
 comp.os.ms-windows.programmer.misc MS/Windows Programming
 comp.os.os2.programmer.misc        OS/2 Programming
 comp.sys.mac.programmer.misc       Macintosh Programming
 comp.unix.programmer               General Unix Questions

--
*********************************************************

* Supporter of the campaign against grumpiness on c.l.c *
*********************************************************
* A Womans Guess is more accurate than a Mans certainty *
*                                       Rudyard Kipling *
*********************************************************
The money spent on an {*filter*},
as any woman may find out,
can better be spent on other things,
like taking the kids to an amusment park, or on vacation.



Thu, 03 Feb 2000 03:00:00 GMT  
 how to touch a file in C?



Quote:

>>       I am trying to writing a function that would touch a certain while every>>few minutes or so. I wonder what is the easiest way to touch a file in the
>>code? There doesn't seems to be functions that can allow you to change the
>>date of a file. To make it more difficult, more function has to work on
>>both DOS and Macintosh. Can anyone help?

>#include <sys\utime.h>

Later on you mention you are talking about the POSIX utime() function. For
that the correct headers to include are:

#include <sys/types.h>
#include <utime.h>

Note that the POSIX path separator is / not \.

Whether DOS or Macintosh implementations follow the POSIX semantics of
utime() is of course another matter (as it what relevance this has to
comp.lang.c).

Quote:
>int utime( const char *path,
>           const struct utimbuf *times );

>struct utimbuf {
>    time_t   actime;    /* access time */
>    time_t   modtime;   /* modification time */
>};

>Description:

>The utime function records the access and modification times for the
>file identified by path.

>The _utime function is identical to utime.  Use _utime for ANSI naming
>conventions.

Which of course is rubbish, ANSI has no such naming convention. uname()
is the correct name to use and is entirely compatible with ANSI C (because
a non-standard header has been included).

...

Quote:
>It may also be sufficient to just open the file ( maybe for writing )
>and close it, but I didn't verfiy this.

--
-----------------------------------------


-----------------------------------------


Thu, 03 Feb 2000 03:00:00 GMT  
 how to touch a file in C?

On Tue, 12 Aug 1997 12:34:51 +0200, Stephan Wilms

Quote:

>You could open the file for reading/writing, read the first byte, write
>it again, then close the file. That should do the trick and update the
>"modified" time of the file.

I would be cautious on suggesting this unless you know the compiler
the asker is using.  I have seen a compiler which would truncate the
file to one byte if your suggestion was followed.  At this time,  I
can't remember which compiler it was,  or even when it happened,  but
I remember that there was a compiler I had used in th past which would
flag EOF immediately following the last byte written to a file,  so in
the case described above,  it would flag the file with an EOF
following the first byte of the file,  which is probably not what the
user wanted.

A suggestion which *should* work for *all* compilers would be to open
the file in append mode,  position to EOF,  back-up one character,
read it,  backup again,  and write it back out.  This way,  the last
write to the file would truly be at its end,  eliminating the problem
I mentioned...



Sat, 05 Feb 2000 03:00:00 GMT  
 how to touch a file in C?

Quote:


> >       I am trying to writing a function that would touch a certain while every
> >few minutes or so. I wonder what is the easiest way to touch a file in the
> >code? There doesn't seems to be functions that can allow you to change the
> >date of a file. To make it more difficult, more function has to work on
> >both DOS and Macintosh. Can anyone help?
> #include <sys\utime.h>
> int utime( const char *path,
>            const struct utimbuf *times );

There's a problem with this....

Quote:
> void main( int argc, char *argv[] )

Oops!  If this is cut and pasted from the compiler help (as it looks)
then I'm rather disappointed to see that it doesn't use the standard,
guarenteed to work

int main(int argc, char *argv[])

Quote:
> Classification:
> utime is POSIX 1003.1

This is the problem.  Not all systems are POSIX systems.  In fact,
some POSIX systems may complain about your code (it \ as a directory
seperator, which may confuse them).  If you're going to post POSIX
help, mark it as such and point out a better place to get help (like
comp.unix.programmer or a POSIX group).

--

            http://www.geocities.com/SiliconValley/Lakes/7537/



Sat, 05 Feb 2000 03:00:00 GMT  
 how to touch a file in C?

Quote:

> On Tue, 12 Aug 1997 12:34:51 +0200, Stephan Wilms

> >You could open the file for reading/writing, read the first byte, write
> >it again, then close the file. That should do the trick and update the
> >"modified" time of the file.

> I would be cautious on suggesting this unless you know the compiler
> the asker is using.  I have seen a compiler which would truncate the
> file to one byte if your suggestion was followed.  At this time,  I
> can't remember which compiler it was,  or even when it happened,  but
> I remember that there was a compiler I had used in th past which would
> flag EOF immediately following the last byte written to a file,  so in
> the case described above,  it would flag the file with an EOF
> following the first byte of the file,  which is probably not what the
> user wanted.

Then the compiler is broken or you did it incorrectly.  This should
work provided the file is nonempty if you open it for "r+b".

Quote:

> A suggestion which *should* work for *all* compilers would be to open
> the file in append mode,  position to EOF,  back-up one character,
> read it,  backup again,  and write it back out.  This way,  the last
> write to the file would truly be at its end,  eliminating the problem
> I mentioned...

This will only work if the compiler is broken; it will duplicate the
last character in the file.  All output to a stream openned in append
mode goes to the end of the file, regardless of intervening calls to
fseek().

Michael M Rubenstein



Sun, 06 Feb 2000 03:00:00 GMT  
 how to touch a file in C?


|> On Tue, 12 Aug 1997 12:34:51 +0200, Stephan Wilms
|>
|>
|> >You could open the file for reading/writing, read the first byte, write
|> >it again, then close the file. That should do the trick and update the
|> >"modified" time of the file.
|>
|> I would be cautious on suggesting this unless you know the compiler
|> the asker is using.  I have seen a compiler which would truncate the
|> file to one byte if your suggestion was followed.  At this time,  I
|> can't remember which compiler it was,  or even when it happened,  but
|> I remember that there was a compiler I had used in th past which would
|> flag EOF immediately following the last byte written to a file,  so in
|> the case described above,  it would flag the file with an EOF
|> following the first byte of the file,  which is probably not what the
|> user wanted.
|>
|> A suggestion which *should* work for *all* compilers would be to open
|> the file in append mode,  position to EOF,  back-up one character,
|> read it,  backup again,  and write it back out.  This way,  the last
|> write to the file would truly be at its end,  eliminating the problem
|> I mentioned...

  Careful ...

  If you open the file in _append_ mode, you can't actually do what
  you suggest. You _can_ carry out the steps listed above, but you'll
  end up extending the file by a byte which contains a redundant copy
  of what used to be its final byte ...

  The original poster is probably stuck with using one of the system-
  dependent approaches that others have suggested. ('utime()' would be
  my choice, on any system which supported it.)

--
 Ed Hook                              |       Copula eam, se non posit
 Computer Sciences Corporation        |         acceptera jocularum.
 NASA Langley Research Center         | Me? Speak for my employer?...<*snort*>



Sun, 06 Feb 2000 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. resx files needed for cs - files ??

3. CS files display in VS6 like CPP files ?

4. Touching a file (set to current time and date)

5. Include code in other Cs files

6. Reuse of cs files, namespace, arch advice pls

7. How to Generate .cs file at Runtime

8. Two CS files (using namespaces)

9. My .cs files were deleted!?

10. manipulating server controls in the Codebehind .cs file

11. *.~cs files

12. Custom Class.cs default file

 

 
Powered by phpBB® Forum Software