473,398 Members | 2,343 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,398 software developers and data experts.

fread seems to be a byte illiterate

i've been given a binary file to open and donot know what it contains except that it has floats, ints and char[x] in it. I've built a struct to hold the data. just do not know how to read it. The first and second time through the loop works good. Get funcky stuff after that.

#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;

#using <mscorlib.dll>

using namespace System;

struct record
{

int s_Nums;
char s_Desc[25];
float s_Cost;
float s_Time;
int s_Made;
int s_Part;

};

void main()
{

FILE *f_Ptr;

struct record s_Test;

f_Ptr = fopen("V.DAT","rb");

for (int x = 0; x <= 5; x++)
{

fread(&s_Test, sizeof(struct record), 1, f_Ptr);

printf("\n\t %i ", s_Test.s_Nums);
printf("\n\t %s ", s_Test.s_Desc);
printf("\n\t %.2f ", s_Test.s_Cost);
printf("\n\t %.2f ", s_Test.s_Time);
printf("\n\t %i ", s_Test.s_Made);
printf("\n\t %i \n", s_Test.s_Part);

printf("\n\t\n");

}// End of for

fclose(f_Ptr);

}// End of main




this is the file data inside of the binary file called "V.DAT"




 compressor 面面面面面面面面R@33A  y
housing 面面面面面面面面面面q=A 9 piston 面面面面面面面面面面)\A J
gear n 面面面面面面面面面面?  generator 面面面面面面面面爺QTA楊Av  |
housing 面面面面面面面面面面q=A clamp g 面面面面面面面面面面瀦h?  pad p g 面面面面面面面面面面>  coil g 面面面面面面面面面面廝@ J
gear g 面面面面面面面面面面? f gear g 面面面面面面面面面面H:? " mixer tor 面面面面面面面面33A楊A?  
housing 面面面面面面面面面面G5A clamp g 面面面面面面面面面面瀦h?  moter g 面面面面面面面面面面襭A  coil g 面面面面面面面面面面廝@ J
gear g 面面面面面面面面面面? f gear g 面面面面面面面面面面H:? ' flashlight 面面面面面面面面 ffF@ 
housing 面面面面面面面面面面
?  bulb ng 面面面面面面面面面面G=  lense g 面面面面面面面面面面=
W> 2 grinder ht 面面面面面面面面 333?   morter 面面面面面面面面面面q=?  pestle 面面面面面面面面面面H
@ : radio r ht 面面面面面面面面爺Q?ff@4   capacitor 面面面面面面面面面瀦?  resister 面面面面面面面面面?  resister 面面面面面面面面面q=?  resister 面面面面面面面面面R? r speaker 面面面面面面面面面=
@  push button 面面面面面面面面>
Mar 5 '08 #1
2 2002
Banfa
9,065 Expert Mod 8TB
It looks to me like the strings are not all the same length and are certainly not 25 characters long in which case you can not read the data into a structure like that.

Anyway reading a file like that is not a good idea because it is not portable, on a different computer with different structure packing rules or different size integers or different endianness of integers you file read routine will fail.

The best thing is to read the individual bytes of data from the file and use the to reconstruct your data type

For instance reading a (32 bit) integer do not do this

Expand|Select|Wrap|Line Numbers
  1.     int i;
  2.  
  3.     fread ( &i, sizeof i, 1, fin);
  4.  
do this

Expand|Select|Wrap|Line Numbers
  1.     int i;
  2.     unsigned char data[4];
  3.  
  4.     fread ( data, 1, sizeof data, fin);
  5.  
  6.     i = (int)(((unsigned)data[0] << 24)
  7.             | ((unsigned)data[1] << 16)
  8.             | ((unsigned)data[2] << 8)
  9.             | (unsigned)data[3]);
  10.  
Then all you have to worry about is if the platform that wrote the file has the same number of bits in the byte as the platform you are using to read it.
Mar 6 '08 #2
thank you for replying. I needed a direction to go into and you have given it to me. thank you.
Mar 6 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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...
5
by: syntax | last post by:
hi here is my test code include<stdio.h> #include<stdlib.h> int main() {
6
by: Patrice Kadionik | last post by:
Hi all, I want to make a brief comparison between read() and fread() (under a Linux OS). 1. Family read and Co: open, close, read, write, ioctl... 2. Family fread and Co: fopen, fclose,...
8
by: M. hman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)?...
13
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...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
4
by: Giacomo | last post by:
Hello.. i'm using php on linux --version: PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies and...
20
by: xiao | last post by:
Hi~ every one~ I have a queston about fread function. if i have a code like this: (nscrdh and data are defined as two dementional arrays and both of them were stored in the same binary file) ...
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...
0
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...
0
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 projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.