473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Jul 22 '05 #1
5 2743
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
Jul 22 '05 #2
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
Jul 22 '05 #3
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
Jul 22 '05 #4
-----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-----
Jul 22 '05 #5
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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Elmo Watson | last post by:
I'm using a database to enter calendar items, using the ASP.Net calendar - I've got it working with one item per date, using code on the net - - it's using a datareader to read the items, then...
1
by: sneha123 | last post by:
There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net pls help me we...
3
by: Dany P. Wu | last post by:
Hi everyone, One of my Windows forms contain two listbox controls, with Add and Remove buttons between them. The idea is to allow users to select multiple items from one ListBox, click the...
2
by: anchi.chen | last post by:
Hi People, Just wondering if any of you have ever come across any javascript examples that will allow one to drag and drop multiple items between lists? That is, users would be able to use the...
2
by: =?Utf-8?B?S3Jpc2huYQ==?= | last post by:
Hi, I am devloping one web application using .net framework 2.0.One page has 7 dropdown list control.When i update the values first bind the values to the drop down llist then selected text...
0
by: Dinkar Sh | last post by:
hi All, i m facing a big problem. i want to Insert Multiple Items at a time Using Grid In Asp.Net. is it possible. if yes then which grid is suitable for it. thanks DInkar
4
by: swethak | last post by:
hi i wrote a code to select multiple items in a drop down list.And i store all the items in my database.But in that i select multiple items and submit that items last item only stored.Please...
1
by: KrazyKasper | last post by:
Access 2003 – Multi-Column List Box – Select Multiple Items I have a multi-column (3 columns) list box that works well to select one set of records or all sets of records (based on the first field...
3
by: arjun007 | last post by:
i want to read multiple worksheets in a single program using php.i done it for one excel sheet by saving .csv file. but,iwant to read multiple worksheets.please help me.................
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.