Re: write



In comp.os.linux.development.system, Bill Cunningham wrote:

I tried to write with this call and I don't know what happened. I
really
don't know much about it.
Here's my code -

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main (void) {
int file;
char buf[]="hello world\n";
size_t c;
c=20000;
write(file,buf,c);
}

Now there must be a call somewhere that C compilers call when using
fopen. This call didn't open anything for me. Also this call must only
write binary and not text. I ran this code it compiled great and did
nothing apparent. So I used dd to write zeros to a file called "file". An
empty file. Then I ran the code and used linux's file keyword to see what
type of file it was and it was data. I used a text editor to open the file
and it didn't work.

What are the uses and limitations of write() and I assume read() is
the
same. C's fopen in text mode must call another syscall.


The C standard supports certain I/O functions that behave in a way defined
in the standard:
fopen() opens a named file and returns a "FILE *" to be used
in the other I/O functions,
fread() reads a block of data, using the "FILE *" returned by fopen(),
fwrite() writes a block of data, using the "FILE *" from fopen(),
fclose() closes the file, using using the "FILE *"from fopen(),
etc

POSIX defines another set of I/O functions that can be used instead (or
alongside) of the C standard I/O functions.These functions are only
available on POSIX-compliant environments, and are /not/ defined by the C
standard.

open() is analogous to fopen(); it opens a named file and returns
a "descriptor" to be used in the other POSIX I/O functions,
read() is analogous to fread(); it reads a block of data, using
the "descriptor" returned by open(),
write() is analogous to fwrite(); it writes a block of data, using
the "descriptor" returned by open(),
close() is analogous to fclose(); it closes the file, using
the "descriptor" returned by open(),

Like the C standard I/O functions, you cannot correctly use the POSIX I/O
read(), write(), and close() functions without first open()ing a file. This
is your problem; you have not open()ed a file to write() your data to. You
want to read the documentation on open(), read(), write(), and close().

As the POSIX file I/O functions are implemented in the kernel, and not in a
userspace standard library, they are called "syscalls" ("system calls") and
documented under the "System Calls" section of the online manual. For
open(), you want the open(2) manual page ("man 2 open"); for read(), you
want the read(2) manual page ("man 2 read"); for write(), you want the
write(2) manual page ("man 2 write"); and for close(), you want the
close(2) manual page ("man 2 close").

To correct your program...

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main (void) {
int file;
char buf[]="hello world\n";
size_t c;
/*
c = 20000;
*/

if ((file = open("my_file",O_WRONLY | O_CREAT | O_TRUNC,0644)) != -1)
{
c = sizeof(buf);
write(file,buf,c);
close(file);
}
}

Hope this helps
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------


.