Thread: _open(), _read(), etc

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    _open(), _read(), etc

    Are functions like _open(), _read(), and _lseek() C functions only? I assume they are since they are in io.h. I was wondering if C++ has a better way to read from files than fstreams, since I have always had problems with them (just too hard to use) Mainly the function I would like for C++ is _lseek() and I don't know of any equivalent...Or are these functions ok to use in C++ programs? File I/O has always been a problem for me and I just discovered these and it made it 1000% easier for me...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    >Are functions like _open(), _read(), and _lseek() C functions only?
    No, you can use them with C++ too.

    >I was wondering if C++ has a better way to read from files than fstreams
    You can use C++ streams, C FILE pointers, or Unix style file descriptors if your system supports them. I find FILE pointers to be the most flexible for my purposes while still maintaining portability, but I don't use C++ much. If you don't use stream objects then niceties like std::string will give you trouble.

    >Mainly the function I would like for C++ is _lseek() and I don't know of any equivalent.
    Stream objects have seekp and seekg member functions that do basically the same thing as lseek.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you don't use stream objects then niceties like std::string will give you trouble.
    Working with char* (s) is not a real problem with what I'm trying to do atm, so I think I will be ok. I guess my real question was "Are these still ok to use for C++"...It still feels wrong mixing a .h header with the new ones (these functions aren't found in any new headers, are they?) , but streams make me so frustrated I don't want to consider using them
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    root
    Join Date
    Sep 2003
    Posts
    232
    >I guess my real question was "Are these still ok to use for C++"
    Of course. Most C code is also valid C++ code.

    >It still feels wrong mixing a .h header with the new ones
    You're listening too hard to the portability lovers. The ANSI C headers are placed in namespace std by putting them in the <c*> headers, but you can still access the old ones and you will be until the end of time as long as C++ claims compatibility with C. Nonstandard headers will usually end with .h or something equivalent, so unless you always program in 100% ANSI you'll use them.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed