read and write

A

asit dhal

hello friends,
can anyone explain me how to use read() write() function in C.

and also how to read a file from disk and show it on the monitor using
onlu read(), write() function ??????
 
D

Dave Vandervies

hello friends,
can anyone explain me how to use read() write() function in C.

By using the stdio input and output functions; on an OS where it makes
sense to use read() and write(), the standard library will almost
definitely call them to do the actual input and output.

and also how to read a file from disk and show it on the monitor using
onlu read(), write() function ??????

By using system-specific details that are beyond the scope of comp.lang.c.


dave
 
J

Joachim Schmitz

asit dhal said:
hello friends,
can anyone explain me how to use read() write() function in C.
No such functions in C89 or C99. They are in POSIX though, so the guye in
comp.unix.programmer would know about them
and also how to read a file from disk and show it on the monitor using
onlu read(), write() function ??????
No, won't work. At least open() would be needed in addition, close() too.
And standard C doesn't have the notion of disk nor monitor.
The closest thing to monitor would probably be stdout... and a process gets
it for granted.

Pseudo code (and without error handling):

file = open(filename, read)
do
bytesread=read(file, buffer, sizeof buffer)
write(stdout_fileno, buffer, bytesread)
until file hits EOF
close (file)

Implementation left as an exercise (homework?) to the OP...

Bye, Jojo
 
M

Martin Ambuhl

asit said:
hello friends,
can anyone explain me how to use read() write() function in C.

There are no functions read() and write() in C. There are various
functions with those names for different implementations and platforms,
the most common ones being the POSIX functions of those naems. To use
them you must be using an appropriate implementation and include
non-standard headers, identifying streams in a non-standard way.
and also how to read a file from disk and show it on the monitor using
onlu read(), write() function ??????

In C one can read an input stream, opened with fopen(), read it with
fread() among others, and write it to an output stream with fwrite()
among otheres.

Even in implementations supporting the POSIX read() and write()
functions what you ask for is impossible. Among other things, you must
somehow open the file you want to read, unless redirected on the command
line as the stdin stream, which requires a function other than read()
and write().
 
D

Dave Vandervies

Let be honest, then, there are no functions fopen, fclose, fread,
fwrite neither in C.

ISO 9899 disagrees with you.


dave

--
Dave Vandervies (e-mail address removed)
Just promise to never show up at a BOFHBOF in Spandex and it's all moot.
Being Canadian, my favourite specialty fabric is Thinsulate.
--Graham Reed and Anthony de Boer in the scary devil monastery
 
W

Walter Roberson

Darko said:
Let be honest, then, there are no functions fopen, fclose, fread,
fwrite neither in C. C is just a "shell", that contains only syntax.
open, close, write and read are functions supported by the O.S. as
part of drivers interface, and stdio functions use them implicitly,
cause there's no way to write/read, open/close any device without
these functions - they are embedded in the drivers directly, and given
by the O.S. through various interfaces (e.g. a C library)
Ask about these on comp.unix.programmer.

And if one is not using Unix?

You are making an assertion about how *all* systems work. That
assertion is not true for a number of systems. For example, there
are systems which have no drivers interface at all.
 
R

Richard Heathfield

Darko said:
Let be honest, then, there are no functions fopen, fclose, fread,
fwrite neither in C.

Should I believe you, or the ISO C Standard? Hmmm. Think think think...
 
K

Keith Thompson

Darko said:
There is no read nor write function in C. Look up such things as
fopen, fclose, fread, fwrite.
[signature snipped]

Let be honest, then, there are no functions fopen, fclose, fread,
fwrite neither in C. C is just a "shell", that contains only syntax.

Let's be correct as well as honest.

The C language is defined by the ISO C standard (either the 1990 or
the 1999 version; the differences are not relevant in this case).
That standard defines the fopen, fclose, fread, and fwrite functions
in section 7, along with a plethora of other functions. (Most of
these are required only for hosted implementations, but that's beside
the point.)

These functions are part of the C language, just as much as the syntax
*and semantics* defined in section 6.

(A minor quibble: the standard's section 6, describing syntax and
semantics, is titled "Language", and section 7 is titled "Library",
but they're both part of the C standard, and part of C.)
open, close, write and read are functions supported by the O.S. as
part of drivers interface, and stdio functions use them implicitly,
cause there's no way to write/read, open/close any device without
these functions - they are embedded in the drivers directly, and given
by the O.S. through various interfaces (e.g. a C library)

That depends on the operating system. On some C implementations, the
fopen, fclose etc. functions might be implemented using some other
lower-level functions; the OS might not even have functions called
"open" and "close", or it may have functions with those names that do
something entirely different than what the POSIX-specified functions
do. On yet other C implementations, fopen and fclose might even be
implemented by directly acessing the hardware.
Ask about these on comp.unix.programmer.

.... where you're more likely to get accurate answers.
 
M

Martin Ambuhl

Darko wrote:
Usenet account fromhttp://www.teranews.com
Let be honest, then, there are no functions fopen, fclose, fread,
fwrite neither in C.

Let us be honest, then. The above is just plain crap. If "Darko" is a
student, he should learn better. If he claims to be a programmer, he
should be fired.
C is just a "shell", that contains only syntax.

Let us be honest, then. The above either is meaningless or is pure crap.
 
C

Chris Torek

... These quotes also clearly distinguish the language from the library.

What I think you have missed here is that the C standards (C89 and
C99 both) split the world into two: "hosted implementations" and
"freestanding". A "hosted" implementation is *required* to have
the full Standard C Library, and -- though this is not "required",
merely "allowed" -- a hosted compiler can assume that a call to,
e.g., printf() or sqrt() does only what the C Standard says it
does, and *remove the call* from a compiled program, replacing it
with something that suffices given the actual arguments.

Some compilers do in fact do this. In an extreme case,

printf("%f\n", sqrt(4.0));

could compile to the same machine code as:

puts("2.000000");

(in practice this particular line usually still calls printf(),
but on some compilers, passes 2.0 directly, without first calling
sqrt(); some other printf() calls are turned into puts() calls
though -- and note that the newline is removed from the string when
printf() is changed into puts()).
It seems that it all comes down to the question what "C" is ...

It is what the C Standard says it is, provided all parties agree to
the C Standard (which then brings up the case of "*which* C standard"
of course :) ):
- the library, the library and the language, or the language.

Going by the C Standard (either C89 or C99), it is never just "the
library"; it is usually "the library and language combined", but
for "freestanding" systems, it is "the language, plus at least a
few parts of the library as listed, plus anything the freestanding
system includes anyway". (See the Standard's section on conformance.)
So, the standard is extensible, because it contains libraries aside
from the language itself. Let's make an example - can some of the
following headers (mentioned in the document, as part of the standard
library) be considered a part of "C":
<signal.h> <setjmp.h>

<signal.h> is not required in a freestanding implementation, but
is required (along with its corresponding functions) in any hosted
implementation. Note that the functions can be quite trivial,
e.g., signal() can simply return SIG_ERR much of the time.

<setjmp.h> is similar, except that setjmp and longjmp cannot be
implemented trivially (a correct call to longjmp() must always
perform the requested "go to").

On the other hand, for instance, <stddef.h> is always required,
even in freestanding implementations.
 
T

Tor Rustad

Darko said:
Let be honest, then, there are no functions fopen, fclose, fread,
fwrite neither in C.


This takes the prize of the most clueless post I have read in a very
long time! ROFL
 
K

Keith Thompson

Darko said:
asit dhal wrote:
can anyone explain me how to use read() write() function in C.
and also how to read a file from disk and show it on the monitor
using onlu read(), write() function ??????
There is no read nor write function in C. Look up such things as
fopen, fclose, fread, fwrite. [snip]
Let be honest, then, there are no functions fopen, fclose, fread,
fwrite neither in C.

Umm, the C standard says otherwise.

I am sorry to have disturbed emotions of so many people here, I did
not intend to do that. Luckily, my employer is not so tough and strict
as some of you folks are, so I think I won't get fired soon. Maybe
later.

As Keith Thompson noticed, the ISO 9899 C standard also lists
functions in the Library section, but I wouldn't dare calling it a
"minor quibble". Since, if one took to read the whole standard or at
least to look at it in detail, would see some key sentences when the
author(s) clearly distinguished the Language and the Library.
Furthermore, besides the Standard Library, there are other things that
this document talks about, which are also not a part of the language.
[snip]

Your original statement (see above) was that

"... there are no functions fopen, fclose, fread, fwrite neither
in C."

You didn't distinguish between the language and the library, you just
said "in C". Both the "language" (described in section 6 of the
standard) and the "library" (described in section 7) are indisputably
part of C. Your statement was quite simply incorrect, and any
distinction between "language" and "library" has nothing to do with
it.

For that matter, the standard document as a whole is titled
"Programming Languages -- C". The term "language" is ambiguous; it
can refer either to what's described in section 6 of the standard, or
to what's described by the standard as a whole, including the library.

And I'm afraid that your error was a particularly blatant one. It's
almost as if you had claimed that C doesn't have pointers, or that it
doesn't have functions.

I'm not saying this to be personally offensive. I'm merely trying to
help you understand why there was such a strong reaction.

This isn't about anger, or courage, or "disturbed emotions", or
anything like that. This newsgroup is a community that values
*correctness* above almost everything else. I've made mistakes here
myself; no doubt some of them have been particularly boneheaded (no
need to search for examples, thank you very much). One of the best
things about this newsgroup is that mistakes are corrected.

And now, you seem to be trying to justify your original statement,
rather than simply admitting that you were mistaken.

The reaction to your error may have seemed excessive, but it's a
consequence of the way Usenet works. Usenet is fundamentally an
asynchronous medium. Most of the respones were written before the
responders had seen any of the other responses. I understand that it
seemed like everyone was ganging up on you, but that wasn't the
intent.

[...]
I am sorry again to have annoyed you, I know I'm offending your sense
of being the smartest and the completest programmers, but there is
such thing as fair and civilised argument that's not supposed to have
words "crap" in it and rude behaviour.

Again, that's not what this is about. As for rudeness, I seriously
suggest you try to grow a thicker skin. Yes, there's some rudeness
here; it's best to ignore it and pay attention to the underlying
message.

[...]
So, instead of saying "sod off" to all of you, which I should, I will
just let you be what you are. Have a nice day.

Well, I'm sure we're all grateful that you merely *insinuated* saying
"sod off" to all of us, rather than coming out and saying it. The
quotation marks really help to soften the blow. (Did you know that
the word "disingenuous" isn't in the dictionary?)
 
K

Keith Thompson

Darko said:
OK, I would just like to sum up for some people here that noone really
thought that there are no functions printf/scanf/... in C, if the term
includes the standard library as well. What I thought when I said "C"
was C language, not C as in "C language and standard C library", and I
still stand behind the fact that there is no printf in the C language.
As for "clueless", "fired", "plain crap", massive sarcasm etc. the
"sod off" but the disingenuousness too are still on ;-)

*Please* trim quoted material to what's relevant to your followup.

The term "C" does refer to the language and the standard library. See
the C standard document, or a draft of it, to confirm this fact.

Being simultaneously very rude and very wrong does not give a good
impression.
 
K

Keith Thompson

[signature snipped]
No, YOU obviously haven't read the whole topic - read it first then
reply.

Once again, please snip your quotes. You just quoted CBFalconer's
rather lengthy signature block, which is irrelevant to your reply.
We're asking you, when you post a followup, to delete any quoted text
that isn't relevant to your followup. Signatures should always be
snipped unless you're actually commenting on them, which you're not.

I've read this entire thread, and I believe I've understood what
everyone has written. You're wrong. The C standard library is part
of C.
 
K

Keith Thompson

CBFalconer said:
Keith Thompson wrote:
... snip ...

PoO: Actually my usual sig is only one line over the
recommendation, and the lines are under 67 chars wide. Teranews
adds another few with their added signature, which is not under my
control.

Understood. I was criticizing Darko for failing to snip your
signature in his followup, not you for having a relatively large one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top