How to read multiple items using read ifstream in C++

Discussion in 'C++' started by lokb, Jul 7, 2004.

  1. lokb

    lokb Guest

    Hi,
    I have a C function fread which does reading of multiple items as
    num = fread(code,1,ilen-4,ifile);

    which is reading 1 byte information ilen-4 times and updating to code
    and returns a number of bytes read each time i.e 1.

    What is the similar function in case of C++ read

    ifile.read(code,1) - i want to specify no of items i.e ilen-4 . How to
    do that.

    Plz guide.
    Lokb
     
    lokb, Jul 7, 2004
    #1
    1. Advertising

  2. lokb wrote:
    > I have a C function fread which does reading of multiple items as
    > num = fread(code,1,ilen-4,ifile);


    What's "code"? How's it declared? In C pointers to other things
    are easily converted to pointers to void (which is what 'fread'
    first argument is).

    You could still use 'fread' in C++, it's just as standard as it is
    in C.

    >
    > which is reading 1 byte information ilen-4 times and updating to code
    > and returns a number of bytes read each time i.e 1.
    >
    > What is the similar function in case of C++ read
    >
    > ifile.read(code,1) - i want to specify no of items i.e ilen-4 . How to
    > do that.


    You'd usually write

    ifile.read(code, ilen-4);

    and to see whether the operation succeeded, you need to check the
    'ifile' object:

    if (!ifile.good()) {
    // error
    }

    A good book on the C++ standard library would also help...

    V
     
    Victor Bazarov, Jul 7, 2004
    #2
    1. Advertising

  3. lokb

    lokb Guest

    Hi Victor,
    Thanks for the reply,
    What i want to do is to read multiple items of 1 byte length.
    The fread ( code, 1, ilen-4, ifile)
    would read 1 byte of information ilen-4 times,

    I suppose if i write
    for (int i=0;i<ilen-4;i++)
    ifile.read(code,1)

    is that makes sense?
    ===================================================
    You'd usually write

    ifile.read(code, ilen-4);

    and to see whether the operation succeeded, you need to check the
    'ifile' object:

    if (!ifile.good()) {
    // error
    }
    ========================================================
    In the obove statemant,just to confirm does it read ilen-4 size of bytes
    in to code. which is same as reading 1 bytes ilen-4 times.

    Thanks,
    Lokb
     
    lokb, Jul 7, 2004
    #3
  4. lokb wrote:
    > What i want to do is to read multiple items of 1 byte length.
    > The fread ( code, 1, ilen-4, ifile)
    > would read 1 byte of information ilen-4 times,
    >
    > I suppose if i write
    > for (int i=0;i<ilen-4;i++)
    > ifile.read(code,1)
    >
    > is that makes sense?


    Reading it in a loop is less efficient than reading ilen-4 bytes
    in one call to that function.

    > ===================================================
    > You'd usually write
    >
    > ifile.read(code, ilen-4);
    >
    > and to see whether the operation succeeded, you need to check the
    > 'ifile' object:
    >
    > if (!ifile.good()) {
    > // error
    > }
    > ========================================================
    > In the obove statemant,just to confirm does it read ilen-4 size of bytes
    > in to code. which is same as reading 1 bytes ilen-4 times.


    It reads ilen-4 bytes from the stream and places them one after
    another in the memory pointed to by 'code'.

    Yes, eating 20 spoons of soup is just like eating 1 spoon 20 times.

    V
     
    Victor Bazarov, Jul 7, 2004
    #4
  5. lokb

    Evan Carew Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    lokb wrote:
    > Hi,
    > I have a C function fread which does reading of multiple items as
    > num = fread(code,1,ilen-4,ifile);
    >
    > which is reading 1 byte information ilen-4 times and updating to code
    > and returns a number of bytes read each time i.e 1.
    >
    > What is the similar function in case of C++ read
    >
    > ifile.read(code,1) - i want to specify no of items i.e ilen-4 . How to
    > do that.
    >

    Well, one option is to write an extractor for your class such that you
    can simply write:

    ifstream >> myobj;
    // repeat as necessary.

    Evan
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.0.6 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFA7L1Poo/Prlj9GScRArJ1AJ9vLzUyEgAoJUugDkyW+/Y9MeiR2QCdHAdN
    /WjIACsmSMts7SN2hMaSmuQ=
    =5g7B
    -----END PGP SIGNATURE-----
     
    Evan Carew, Jul 8, 2004
    #5
  6. Evan Carew wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > lokb wrote:
    >
    >> Hi,
    >> I have a C function fread which does reading of multiple items as
    >> num = fread(code,1,ilen-4,ifile);
    >> which is reading 1 byte information ilen-4 times and updating to code
    >> and returns a number of bytes read each time i.e 1.
    >> What is the similar function in case of C++ read
    >> ifile.read(code,1) - i want to specify no of items i.e ilen-4 . How to
    >> do that.
    >>

    > Well, one option is to write an extractor for your class such that you
    > can simply write:
    >
    > ifstream >> myobj;
    > // repeat as necessary.


    The difference between 'read' and 'operator>>' is that the former is
    _unformatted_ input, the latter is _formatted_. So, the behaviour
    of 'read' is just what the OP needed to perform exactly the same
    operation as 'fread' does.

    Just FYI

    V
     
    Victor Bazarov, Jul 8, 2004
    #6
    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.
Similar Threads
  1. Gunnar

    ifstream.read manual

    Gunnar, Jul 27, 2003, in forum: C++
    Replies:
    2
    Views:
    8,833
    Jim Fischer
    Jul 28, 2003
  2. Nils Wogatzky

    ifstream - read - problem

    Nils Wogatzky, Sep 8, 2003, in forum: C++
    Replies:
    5
    Views:
    674
    Kevin Goodsell
    Sep 8, 2003
  3. Clint Ruen
    Replies:
    4
    Views:
    575
    Richard Herring
    Dec 7, 2004
  4. Ben
    Replies:
    2
    Views:
    954
  5. ela
    Replies:
    12
    Views:
    381
    Uri Guttman
    Apr 6, 2009
Loading...

Share This Page