Appending timestamp on the end of file.

Appending timestamp on the end of file.

Post by FunctionCreep on Thu Dec 02, 2010 5:45 pm
([msg=49833]see Appending timestamp on the end of file.[/msg])

So as the title suggests I'm trying to append a timestamp using time.h on a file that I input as a parameter. What happens is that even though my printf works just fine only the first 3 letters are appended on the file. Eg. Fri Dec 3 00:25:51 2010, only Fri is appended.

Code: Select all
#include "myheaders.h"

int main(int argc, char** argv)
{
int fd;
if ((fd = open(argv[1],O_RDWR|O_APPEND|O_CREAT, 0777)) <0 )
{
perror("open");
exit(1);
}//end if

time_t ltime;
ltime=time(NULL);

char* hm = asctime(localtime(&ltime));

write(fd,hm,sizeof(char));  //<- Only appends 3 first letters.

printf("%s",asctime(localtime(&ltime))); //<- Works fine.

}//end main


Run with "./time test.txt"

The above is what I've done and quite honestly I've found a bunch of info on how to display time etc, but minimal on how to output that data onto a file.

Thanks in advance.
"I hope for nothing. I fear nothing. I am free." ~ Nikos Kazantzakis
User avatar
FunctionCreep
Experienced User
Experienced User
 
Posts: 92
Joined: Tue May 18, 2010 6:19 pm
Blog: View Blog (0)


Re: Appending timestamp on the end of file.

Post by thetan on Thu Dec 02, 2010 6:52 pm
([msg=49838]see Re: Appending timestamp on the end of file.[/msg])

terminal: man 2 write
Code: Select all
WRITE(2)                   Linux Programmer's Manual                  WRITE(2)

NAME
       write - write to a file descriptor

SYNOPSIS
       #include <unistd.h>

       ssize_t write(int fd, const void *buf, size_t count);

DESCRIPTION
       write()  writes  up  to  count bytes from the buffer pointed buf to the
       file referred to by the file descriptor fd.

       The number of bytes written may be less than  count  if,  for  example,
       there  is  insufficient space on the underlying physical medium, or the
       RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)),  or  the
       call was interrupted by a signal handler after having written less than
       count bytes.  (See also pipe(7).)

       For a seekable file (i.e., one to which lseek(2) may  be  applied,  for
       example,  a  regular file) writing takes place at the current file off‐
       set, and the file offset is incremented by the number of bytes actually
       written.   If  the file was open(2)ed with O_APPEND, the file offset is
       first set to the end of the file before writing.  The adjustment of the
       file offset and the write operation are performed as an atomic step.

       POSIX  requires  that  a  read(2)  which can be proved to occur after a
       write() has returned returns the new data.  Note that not all file sys‐
       tems are POSIX conforming.



in your code you have:
Code: Select all
write(fd,hm,sizeof(char));


and on most systems, sizeof(char) == 1 byte. So the part i find amusing is if you're telling write() to write 1 byte of data, why is it writing 3?

anyways, you want the following:
Code: Select all
include <string.h>
......
......
write(fd,hm,strlen(hm));


EDIT:
oh yeah and the man page for strlen():
Code: Select all
STRLEN(3)                  Linux Programmer's Manual                 STRLEN(3)

NAME
       strlen - calculate the length of a string

SYNOPSIS
       #include <string.h>

       size_t strlen(const char *s);

DESCRIPTION
       The  strlen()  function  calculates  the  length  of  the string s, not
       including the terminating '\0' character.

RETURN VALUE
       The strlen() function returns the number of characters in s.

CONFORMING TO
       SVr4, 4.3BSD, C89, C99.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: Appending timestamp on the end of file.

Post by FunctionCreep on Thu Dec 02, 2010 7:21 pm
([msg=49840]see Re: Appending timestamp on the end of file.[/msg])

write(fd,hm,sizeof(char));


You are indeed right, char is only 1 byte long and writes "F", with sizeof(long int) I was able to get more. (don't ask)

I'll experiment some more with strlen.

Thanks alot Thetan :)
"I hope for nothing. I fear nothing. I am free." ~ Nikos Kazantzakis
User avatar
FunctionCreep
Experienced User
Experienced User
 
Posts: 92
Joined: Tue May 18, 2010 6:19 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron