Some basic Q for help!

B

Birt

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!
 
J

Jorge Rivera

Birt said:
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::eek:stream& operator<<(std::eek:stream& 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
 
B

Birt

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?
 

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

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top