By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,408 Members | 968 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,408 IT Pros & Developers. It's quick & easy.

Some basic Q for help!

P: n/a
1.
In a program, there will be several files needed to be accessed. Is it
necessary to design a class that handles reading/writing files?

2.
For a struct data type,

struct mystruct
{
int int1;
char b;
int int2;
}

How to using >> and << for writing and reading objects of struct Date?

3.
Needs to read from 1000 ints from a binary file of integers.

Code 1:

ifstream instream;

int B[1000];

unsigned k = 0;
while(instream.eof() && m<1000){
instream >> B[k];
++k;
}
Code 2:

void read4bytes(void *p)
{
instream.read((char*)p, 4);
}

unsigned k = 0;
int* ptr;
while(k<1000)
{
read4bytes(ptr);
B[k] = *ptr;
++k;
}

Is Code 1 higher level of reading files compared to Code 2?
Is Code 1 faster compared to Code 2?
Should always use coding like Code 1 for reading files?

Thanks for your help!
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Birt wrote:
1.
In a program, there will be several files needed to be accessed. Is it
necessary to design a class that handles reading/writing files?

That's the purpose of fstream. If you mean for semantic parsing (fill
objects with data), you need to provide your own...
2.
For a struct data type,

struct mystruct
{
int int1;
char b;
int int2;
}

How to using >> and << for writing and reading objects of struct Date?
Just like you would any other class

struct mystruct{
....
friend std::ostream& operator<<(std::ostream& os, const mystruct& s);
friend std::istream& operator>>(std::istream& is, mystruct& s);
};
3.
Needs to read from 1000 ints from a binary file of integers.

Code 1:

ifstream instream;

int B[1000];

unsigned k = 0;
while(instream.eof() && m<1000){
instream >> B[k];
++k;
} I may be wrong about this, but unless your ints are separated by spaces,
this code will not work as you expect.

Code 2:

void read4bytes(void *p)
{
instream.read((char*)p, 4);
}

unsigned k = 0;
int* ptr;
while(k<1000)
{
read4bytes(ptr);
B[k] = *ptr;
++k;
}
This only works if you serialzed all your ints in hexadecimal form (or
through ofstrea::write.
Is Code 1 higher level of reading files compared to Code 2? I have no idea of what you mean.
Is Code 1 faster compared to Code 2? I doubt it. Code 2 is more specialized (read exactly four bytes,
rinterpret the read bytes as hex ints.
Code 1 has to go through some hoops to get to the data.

BTW, I don't think it will make much difference to whatever algorith,
you want to feed...
Should always use coding like Code 1 for reading files?

Depends on the application.

JLR
Jul 22 '05 #2

P: n/a
Jorge Rivera <jo*****@rochester.rr.com> wrote in message
news:HB*******************@twister.nyroc.rr.com...
Needs to read from 1000 ints from a binary file of integers.

Code 1:

ifstream instream;

int B[1000];

unsigned k = 0;
while(instream.eof() && m<1000){
instream >> B[k];
++k;
}

I may be wrong about this, but unless your ints are separated by spaces,
this code will not work as you expect.


Code 2:

void read4bytes(void *p)
{
instream.read((char*)p, 4);
}

unsigned k = 0;
int* ptr;
while(k<1000)
{
read4bytes(ptr);
B[k] = *ptr;
++k;
}


This only works if you serialzed all your ints in hexadecimal form (or
through ofstrea::write.


Thanks Jorge for your help!

Why did you think Code 1 only works when the ints in the file is separated
by spaces? How to change Code 1 to get rid of such a requirement on
"spaces"?

"serialzed all your ints in hexadecimal form"? Does this mean that you can
serialize ints in other forms? What are the other forms? How to serialize
in other forms? ofstream::write() can only be used in hexadecimal form?
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.