read and write

Discussion in 'C Programming' started by asit dhal, May 29, 2007.

  1. asit dhal

    asit dhal Guest

    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 ??????
    asit dhal, May 29, 2007
    #1
    1. Advertising

  2. In article <>,
    asit dhal <> wrote:
    >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

    --
    Dave Vandervies
    I _am_ consistent - if one of those other pointer guide writers came
    here and asked for comments, they'd get chewed out just as badly.
    --Richard Bos in comp.lang.c
    Dave Vandervies, May 29, 2007
    #2
    1. Advertising

  3. "asit dhal" <> schrieb im Newsbeitrag
    news:...
    > 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
    Joachim Schmitz, May 29, 2007
    #3
  4. asit dhal wrote:
    > 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().
    Martin Ambuhl, May 29, 2007
    #4
  5. In article <>,
    Darko <> wrote:
    >On May 29, 10:52 pm, CBFalconer <> wrote:


    >> There is no read nor write function in C. Look up such things as
    >> fopen, fclose, fread, fwrite.


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


    ISO 9899 disagrees with you.


    dave

    --
    Dave Vandervies
    >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
    Dave Vandervies, May 30, 2007
    #5
  6. In article <>,
    Darko <> wrote:

    >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.

    --
    If you lie to the compiler, it will get its revenge. -- Henry Spencer
    Walter Roberson, May 30, 2007
    #6
  7. Darko said:

    > On May 29, 10:52 pm, CBFalconer <> wrote:
    >> 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.

    >
    > 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...

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 30, 2007
    #7
  8. Darko <> writes:
    > On May 29, 10:52 pm, CBFalconer <> wrote:
    >> 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.

    [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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 30, 2007
    #8
  9. 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.
    Martin Ambuhl, May 30, 2007
    #9
  10. asit dhal

    Chris Torek Guest

    In article <>
    Darko <> wrote:
    >... 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.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, May 30, 2007
    #10
  11. asit dhal

    Tor Rustad Guest

    Darko wrote:

    >
    > 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

    --
    Tor <torust [at] online [dot] no>
    Tor Rustad, May 30, 2007
    #11
  12. Darko <> writes:
    > On May 30, 6:24 am, Clark Cox <> wrote:
    >> On 2007-05-29 17:12:02 -0700, Darko <> said:
    >> > On May 29, 10:52 pm, CBFalconer <> wrote:
    >> >> 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?)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 30, 2007
    #12
  13. Darko <> writes:
    [...]
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 5, 2007
    #13
  14. Darko <> writes:
    > On Jun 5, 11:30 pm, CBFalconer <> wrote:
    >> Darkowrote:
    >>
    >> ... snip about 150 lines ...
    >>
    >> > 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 ;-)

    >>
    >> You obviously haven't bothered to read the standard. The standard
    >> library is a part of that specification.
    >>
    >> Please snip your quotes.


    [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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 7, 2007
    #14
  15. CBFalconer <> writes:
    > Keith Thompson wrote:
    > ... snip ...
    >>
    >> 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.

    >
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 7, 2007
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page