Re: Reading a Binary File....



In article
<23190e4c-d740-44d7-a905-a79832cb78bf@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
swetha <laptop545@xxxxxxxxx> wrote on Tuesday 20 Nov 2007 2:54 am:

HI Every1,
I have a problem in reading a binary file.
Actually i want a C program which reads in the data from a file which
is in binary format and i want to update values in it.

If this file has been created on a different system to the one in which
it is being read, or if a different program has created it than the one
doing the reading, or if your program does not know the exact details
of the file's format, subtle errors may occur.

This is why it is often better to deal with text files unless there are
compelling reasons for doing otherwise. For many programs on modern
desktop systems, which need not be performance critical, it is just
fine to store data in text files.

The file consists of structures of type----

struct record {
int acountnum;
char name[20];
float value;
char phone[10];
}

You are missing a semi-colon to terminate the structure declaration.
Please copy and paste code. Retyping has been proven, time and again,
to result in annoying typos and incomplete code.

I wrote a program which reads in these values into a structure which i
created in my program. While printing the values i got the "name" and
"phone" values correctly ( as they are in char) ....but iam not
getting the accountnum and value fields.Iam getting some arbitary
values.SO can any1 help me out in this of how to retrieve the values
correctly ......

My code is...

struct record{
int acctnum;
char name[20];
float value;
char phone[10];
int age;
};

Which structure is the one you are actually using? This one or the one
previous? This once again illustrates why code should be, wherever
possible, cut and pasted.

struct record data;
FILE *file = fopen("db07", "rb");

You might use a name other than 'file' for readability reasons. Have two
identifiers differring only in case, though legal in C, is not a very
good programming practise. And it might lead to conflicts if the
identifiers happen to have external linkage _and_ you happen to use a
very old or primitive linker. Better to be safe and avoid the
possibility altogether.

while ( fread(&data, sizeof (struct record), 1, file) == 1 )
{
printf("Name:%s\nAccount Number :%d\nAge:%d\nPhone:%s\nValue:%f\n",
data.name, data.acctnum,data.age,data.phone,data.value);
}

A likely possibility is that the data file was created on a system other
than the one it is under now, or that it was created by a program
compiled with certain compiler options that this program (i.e., the one
reading the file) has not taken into account.

If it is indeed one of these possibilities then you need to know the
exact layout of the file's structure objects before you can read it in
correctly.

.



Relevant Pages

  • [9fans] Questions on notes
    ... top of that there always seems to be a race condition between somebody reading on /proc/n/note ... Speaking of constant polling: the following hangs 9vx for good on my system: ... void door_bell(void* dummy, char* note) ... int main ...
    (comp.os.plan9)
  • Re: What is the gain of "inline"
    ... int bla{ ... char p; ... /* do lots of things on p - reading that many characters from a file ...
    (comp.lang.c)
  • Re: Reading a Binary File....
    ... I have a problem in reading a binary file. ... int acountnum; ... char phone; ...
    (comp.lang.c)
  • RE: Crystal FTP Pro 2.8 PoC
    ... --> Reading USER...OK! ... unsigned char reverseshell[] = ... void auth; ... void handle_cmd (int s, int connfd, char* ip); ...
    (Bugtraq)
  • Re: Reading a Binary File....
    ... I have a problem in reading a binary file. ... int acountnum; ... float value; ... char phone; ...
    (comp.lang.c)