Using fread alternative in C++

R

ryanselk

Hey all, I have a c function im putting into c++ (dont ask why, tell
you the truth, I really dont know!). I am having a bit of problem
deciding on how to use a new method of fread.

Here is the (working) C code:

float read_p(FILE *file, int datatype)
{
float res;
unsigned char u, b1, b2;

switch(datatype)
{
case 1:
fread(&u, sizeof(unsigned char), 1, file);
res = u;
break;
case 4:
fread(&res, sizeof(float), 1, file);
break;
default:
printf ("\n Error \n\n");
res = 0.0;
exit(1);
}
return res;
}


Here is what I have so far.. (I had to re-do some of the file IO).

float read_p(ifstream &file1, int datatype)
{
float res;
unsigned char u, b1, b2;

switch(datatype)
{
case 1:
fread(&u, sizeof(unsigned char), 1, &file1);
res = u;
break;
case 4:
fread(&res, sizeof(float), 1, &file1);
break;
default:
cout<<"Error"<<endl;
res = 0.0;
exit(1);
}
return res;
}

Anyone have a comment on how I could use something like fread? Sorry I
am not very good at c++! Havnt used it in years.
 
V

Victor Bazarov

Hey all, I have a c function im putting into c++ (dont ask why, tell
you the truth, I really dont know!). I am having a bit of problem
deciding on how to use a new method of fread.

Huh? "A new method of fread"? You can't use 'fread' with C++ I/O
streams. They have their own 'read' member function for that.

Get yourself a decent book. The sooner, the better.

V
 
G

Gianni Mariani

Hey all, I have a c function im putting into c++ (dont ask why, tell
you the truth, I really dont know!). I am having a bit of problem
deciding on how to use a new method of fread.

Here is the (working) C code: .... snipped
Here is what I have so far.. (I had to re-do some of the file IO).

float read_p(ifstream &file1, int datatype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If anything, this should be:

float read_p(istream &file1, int datatype)

Use and istream reference instead of an ifstream reference so you can
use any istream based type.
{
float res;
unsigned char u, b1, b2;

switch(datatype)
{
case 1:
fread(&u, sizeof(unsigned char), 1, &file1); what do you do about errors ?
res = u;
break;
case 4:
fread(&res, sizeof(float), 1, &file1);
what do you do when there are not enough bytes in the file ?

You could use :
file1.read( reinterpret_cast said:
break;
default:
cout<<"Error"<<endl;
res = 0.0;
exit(1);
}
return res;
}

Anyone have a comment on how I could use something like fread? Sorry I
am not very good at c++! Havnt used it in years.

Use it more often.
 
R

ryanselk

Hey all, I have a c function im putting into c++ (dont ask why, tell
you the truth, I really dont know!). I am having a bit of problem
deciding on how to use a new method of fread.

Huh? "A new method of fread"? You can't use 'fread' with C++ I/O
streams. They have their own 'read' member function for that.

Get yourself a decent book. The sooner, the better.

V


Just phrased that poorly. What function would be good to use to read
the stuff in instead of fread? read?
I dont want to buy a book for a couple of lines of code.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hey all, I have a c function im putting into c++ (dont ask why, tell
you the truth, I really dont know!). I am having a bit of problem
deciding on how to use a new method of fread.

Here is the (working) C code:

float read_p(FILE *file, int datatype)
{
float res;
unsigned char u, b1, b2;

switch(datatype)
{
case 1:
fread(&u, sizeof(unsigned char), 1, file);
res = u;
break;
case 4:
fread(&res, sizeof(float), 1, file);
break;
default:
printf ("\n Error \n\n");
res = 0.0;
exit(1);
}
return res;
}


Here is what I have so far.. (I had to re-do some of the file IO).

float read_p(ifstream &file1, int datatype)
{
float res;
unsigned char u, b1, b2;

switch(datatype)
{
case 1:
fread(&u, sizeof(unsigned char), 1, &file1);
res = u;
break;
case 4:
fread(&res, sizeof(float), 1, &file1);
break;
default:
cout<<"Error"<<endl;
res = 0.0;
exit(1);
}
return res;
}

float read_p(std::istream& file, int datatype)
{

switch (datatype)
{
case 1:
unsigned char c;
file >> c;
return c;
case 4;
float f;
file >> f;
return f;
default:
// ...
}

And add extensive error checking.
 
P

Pete Becker

float read_p(std::istream& file, int datatype)
{

switch (datatype)
{
case 1:
unsigned char c;
file >> c;
return c;
case 4;
float f;
file >> f;
return f;
default:
// ...
}

This won't work. Presumably, the file contains binary data, which is
why the original code uses fread. The stream extractors read formatted
text, which is an entirely different animal.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

This won't work. Presumably, the file contains binary data, which is
why the original code uses fread. The stream extractors read formatted
text, which is an entirely different animal.

Ah, I knew there was a reason it seemed so simple, my apologies.
 

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
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top