Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to read multiple items using read ifstream in C++

Reply
Thread Tools

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

 
 
lokb
Guest
Posts: n/a
 
      07-07-2004
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-07-2004
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
 
Reply With Quote
 
 
 
 
lokb
Guest
Posts: n/a
 
      07-07-2004
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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-07-2004
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
 
Reply With Quote
 
Evan Carew
Guest
Posts: n/a
 
      07-08-2004
-----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-----
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-08-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
resolve single line with multiple items into mutliple lines, single items ela Perl Misc 12 04-06-2009 06:47 PM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
ifstream - read - problem Nils Wogatzky C++ 5 09-08-2003 09:14 PM
ifstream.read manual Gunnar C++ 2 07-28-2003 03:37 AM



Advertisments