473,804 Members | 3,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using fread alternative in C++

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.

Aug 28 '07 #1
6 4348
ry******@gmail. com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 28 '07 #2
On Aug 28, 2:59 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
ryans...@gmail. com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

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.

Aug 28 '07 #3
ry******@gmail. com wrote:
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_cas t<char *>( & res ), sizeof( res ) );
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.
Aug 28 '07 #4
On 2007-08-28 22:49, ry******@gmail. com wrote:
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::ist ream& 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.

--
Erik Wikström
Aug 28 '07 #5
On 2007-08-28 17:14:58 -0400, Erik Wikström <Er***********@ telia.comsaid:
On 2007-08-28 22:49, ry******@gmail. com wrote:
>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::ist ream& 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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 28 '07 #6
On 2007-08-29 01:30, Pete Becker wrote:
On 2007-08-28 17:14:58 -0400, Erik Wikström <Er***********@ telia.comsaid:
>On 2007-08-28 22:49, ry******@gmail. com wrote:
>>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::ist ream& 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.
Ah, I knew there was a reason it seemed so simple, my apologies.

--
Erik Wikström
Aug 29 '07 #7

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

Similar topics

8
15360
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same location in the file. What is happening, is I can read the file, either in entirety or only part of it. And no matter what I try setting the position to, using fsetpos, it always gets set back to the very beginning of the file. I
22
1876
by: Rajshekhar | last post by:
Hi , i am writing a simple prgm to read a .txt file then store the contents into the array... program as follows: -------------------------- #include<stdio.h> int main() { FILE *fp1;
6
3908
by: Claude Yih | last post by:
Hi, everyone. I noticed an interesting thing about fread() this afternoon. Well, I can't see why so I post this message in the hope of getting some explanation. Please help me. I wrote the following code in Windows 2k and compiled it with the gcc(version: 3.2.3) contained in MinGW: #include <stdio.h> #include <stdlib.h> #include <string.h>
3
3355
by: cinsky | last post by:
Hi, While reading ISO C Standard, I found follow text in 7.19.8.1: size_t fread(void *restrict ptr, size_t SIZE, ...) ... For each object, SIZE calls are made to the fgetc function and the results stored, in the order read, in an array of unsigned char exactly overlaying the object.
13
3552
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could discern, fread all the sudden kept returning the same byte over and over as if it were no longer advancing in the file. I used a hex editor to determine the address of the last byte read in the file. CF was the last address, D0 was not ever read...
6
1913
by: Lars Erik Bryld | last post by:
Hello group I am trying to convert a (supposedly) working python script into php. The script reads a colour gif-picture and produces a greyscale copy of it. The method is to first copy the gif header, then establish the size of the colour palette, then do a simple averaging of each colour byte triplet into a grayscale equivalent, and finally to copy the remaining part of the gif file unaltered over into the new file. I believe, it's
3
5472
by: rajasekar.karthik | last post by:
Hi Guys I need a php script which will merge two wave files as i have already done a script which will will join the two wave files and play one after the other, can anyone advice me how to merge the two wave file and the two files has to play simultaneously. i have placed my script here <?php $content = joinwavs(array('1.wav','2.wav')); header('Content-Type: audio/x-wav'); //echo $content;
30
14288
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
24
6022
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h> #include<string.h> void main(){ FILE *fp,*ftemp;
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.