#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    7
    Rep Power
    0

    How to use read() & write() function in C++ which perform like in C?


    read() & write() function take 3 parameters in C programming language but 2 parameters only in C++.
    So how can I use these 2 functions as they are doing their jobs under C programming language?
    Is it using extern "C" ?
    for e.g.
    extern "C"{
    ssize_t read(int fd, void *buf, size_t count);
    ssize_t write(int fd, const void *buf, size_t count);
    }

    Pls let me know if this is correct.
    Thank u very much!
  2. #2
  3. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1511
    The first parameter in the C function is the file stream. In C++, you use a stream object, so instead of just saying read(...) you say mystream.read(...). The 'mystream' could be considered the third paramter.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,293
    Rep Power
    2223
    The read() and write() functions should take the same number and types of arguments in both C and in C++.

    f'lar raises the point that perhaps you are confusing the low-level I/O C functions with the methods of an I/O class.

    Now, in C++, you can use either low-level I/O (open, read, write, close available in both C and C++) or block I/O (fopen, fread, fwrite, fclose available in both C and C++) or iostreams (cout<<, cin>>, etc, available only in C++) or whatever other I/O class is provided (eg, CFile in MFC, obviously also only available in C++).

    Obviously, both in low-level and block I/O one of the function arguments must be the file descriptor or file pointer. However, in a method for an I/O object that argument is no longer needed because that information is implicit in the object itself.

    So just keep straight in your mind whether you are using the stand-alone function or an object's method.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    7
    Rep Power
    0
    Originally posted by f'lar
    The first parameter in the C function is the file stream. In C++, you use a stream object, so instead of just saying read(...) you say mystream.read(...). The 'mystream' could be considered the third paramter.
    So how should I write my statement in C++ if this is from C:
    num_written = write(sd, ptr, num_left);

    Thanks!
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,293
    Rep Power
    2223
    Originally posted by ericmar
    So how should I write my statement in C++ if this is from C:
    num_written = write(sd, ptr, num_left);

    Thanks!
    The exact same way!

    As a matter of fact, last semester in our Linux programming class, our instructor insisted that we write all of our programs in C++. He wanted us to stream to and from cout and cin for our console I/O, but all of our other I/O (disk I/O, pipes, sockets, etc) was with the low-level I/O functions open, read, write, close -- exactly identical to how we would have written it in C.

    Now, in your first post, you stated:
    read() & write() function take 3 parameters in C programming language but 2 parameters only in C++.

    Where exactly does it state that read() and write() only take 2 parameters in C++? Are you sure that your C++ source is talking about the functions read() and write() and that it is not talking about some class' methods that happen to have the same names?
    Last edited by dwise1_aol; October 2nd, 2003 at 06:23 PM.

IMN logo majestic logo threadwatch logo seochat tools logo