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

L

lokb

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
 
V

Victor Bazarov

lokb said:
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
 
L

lokb

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
 
V

Victor Bazarov

lokb said:
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
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
V

Victor Bazarov

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

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
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top