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

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein