阅读二进制文件.... [英] Reading a Binary File....

查看:77
本文介绍了阅读二进制文件....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI Every1,

我在阅读二进制文件时遇到问题。

实际上我想要一个C程序从一个文件中读取数据

是二进制格式,我想更新其中的值。

该文件由类型结构组成----


struct记录{

int acountnum;

char name [20];

浮动值;

char phone [10 ];

}


我写了一个程序,它将这些值读入我在程序中创建的
结构中。在打印值时,我得到了名称。和

电话正确的值(因为它们在char中)....但是我没有

获取accountnum和value fields.Iam得到一些仲裁

values.SO可以帮助我在这个如何正确检索值

......


我的代码是......


结构记录{

int acctnum;

char name [20];

浮动值;

char phone [10];

int age;

};


结构记录数据;

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

while(fread(& data,sizeof(struct record),1,file)== 1)

{

printf("名称:%s \ nAccount编号:%d \ nAge:%d \ nPhone:%s \ nValue:%f \\ \\ n",

data.name,data.acctnum,data.age,data.phone,data.value);

}

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.
The file consists of structures of type----

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

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;
};

struct record data;
FILE *file = fopen("db07", "rb");
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);
}

推荐答案

在< 23 ********************************* * @ s36g2000prg。 googlegroups.comswetha< la ******* @ gmail.comwrites:
In <23**********************************@s36g2000prg. googlegroups.comswetha <la*******@gmail.comwrites:

我写了一个程序,将这些值读入一个结构中在我的程序中创建了
。在打印值时,我得到了名称。和

电话正确的值(因为它们在char中)....但是我没有

获取accountnum和value fields.Iam得到一些仲裁

values.SO可以帮助我在这个如何正确检索值

......
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 ......



原始数据文件是否在另一台计算机上创建?


-

John Gordon A是Amy,他从楼梯上摔下来
go **** @ panix.com B代表巴兹尔,被熊袭击

- 爱德华·戈瑞,The Gashlycrumb Tinies

Was the original datafile created on a different computer?

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


swetha写道:
swetha wrote:

HI Every1,

我遇到问题读取二进制文件。

实际上我想要一个C程序从一个文件中读取数据,其中b / b是二进制格式的,我想更新其中的值。 br />
该文件由类型----


struct r的结构组成ecord {

int acountnum;

char name [20];

浮动值;

char phone [10 ];

}


我写了一个程序,它将这些值读入我在程序中创建的
结构中。在打印值时,我得到了名称。和

电话正确的值(因为它们在char中)....但是我没有

获取accountnum和value fields.Iam得到一些仲裁

values.SO可以帮助我在这个如何正确检索值

......
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.
The file consists of structures of type----

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

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 ......



[OP的代码在EOM]


/ *您没有说明这些文件的创建方式。在下面的示例

*中,我添加了这些文件的写入。请注意,您文件中的

*结构与您在程序中使用

*的结构不同。另请注意,对于10位数的电话号码(或

* 20个字符的名称),您的会员姓名和电话不是字符串,

*因为它们不是空终止* /


#include< stdio.h>

#include< stdlib.h>


struct file_record

{

int acountnum;

char name [20];

浮动值;

char phone [10];

};


struct internal_record

{

int acctnum;

char name [20];

浮动值;

char phone [10];

int age;

};


void show_char_array(const char * label,size_t n,const char * s)

{

size_t i;

printf("%s:",label);

for(i = 0; i< n&& s [i]; i ++)

putchar(s [i]);

putchar(''\ n'');

}


int main(无效)

{

struct file_record make [3] = {

{1,first guy,72.3,3123375032},

{2,second guy,333.72," 2128836234"},

{3," third guy", - 62," 6182213"}

};

struct internal_record data;

FILE *文件;

size_t写的;


if(!(file = fopen(" db07"," wb"))){

fprintf(stderr,无法打开输出文件\ n);

退出(EXIT_FAILURE);

}

written = fwrite(& make,sizeof * make,3,file);

printf("%zu records written.\\\
,写);

fclose(file);


if(!(file = fopen(" db07"," rb"))){

fprintf (stderr,无法打开输入文件\ n);

退出(EXIT_FAILURE);

}

printf("请注意,年龄成员不在输入中。\ n"

"我已将其设置为26 \ n \ n");

data.age = 26;

while(fread(& data,sizeof * make,1,file)){

show_char_array(" Name" ;,20,data.name);

printf("帐号:%d \ nAge:%d \ n",data.acctnum,data.age);

show_char_array(" Phone",10,data.phone);

printf(" Value:%f\\\
\ n",data.value);

}

fclose(文件);

删除(" db07");

返回0;

}


[输出]

3条记录。

请注意,年龄成员不在输入。

我把它设置为26


姓名:第一个人

帐号:1

年龄:26

电话:3123375032

价值:72.300003


姓名:第二个家伙

账号:2

年龄:26

电话:2128836234

价值:333.720001


姓名:第三个家伙

账号:3

年龄:26

电话: 6182213

价值:-62.000000

[OP'代码]

[OP''s code is at EOM]

/* You don''t indicate how these files are created. In the example
* below I have added the writing of these files. Note that the
* structures in your file are not the same as those that you are using
* in your program. Also note that for 10-digit phone numbers (or
* 20-character names) your members name and phone are not strings,
* since they are not null terminated */

#include <stdio.h>
#include <stdlib.h>

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

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

void show_char_array(const char *label, size_t n, const char *s)
{
size_t i;
printf("%s:", label);
for (i = 0; i < n && s[i]; i++)
putchar(s[i]);
putchar(''\n'');
}

int main(void)
{
struct file_record make[3] = {
{1, "first guy", 72.3, "3123375032"},
{2, "second guy", 333.72, "2128836234"},
{3, "third guy", -62, "6182213"}
};
struct internal_record data;
FILE *file;
size_t written;

if (!(file = fopen("db07", "wb"))) {
fprintf(stderr, "could not open output file\n");
exit(EXIT_FAILURE);
}
written = fwrite(&make, sizeof *make, 3, file);
printf("%zu records written.\n", written);
fclose(file);

if (!(file = fopen("db07", "rb"))) {
fprintf(stderr, "could not open input file\n");
exit(EXIT_FAILURE);
}
printf("Note that the age member is not present in the input.\n"
"I have set it at 26\n\n");
data.age = 26;
while (fread(&data, sizeof *make, 1, file)) {
show_char_array("Name", 20, data.name);
printf("Account Number :%d\nAge:%d\n", data.acctnum, data.age);
show_char_array("Phone", 10, data.phone);
printf("Value:%f\n\n", data.value);
}
fclose(file);
remove("db07");
return 0;
}

[Output]
3 records written.
Note that the age member is not present in the input.
I have set it at 26

Name:first guy
Account Number :1
Age:26
Phone:3123375032
Value:72.300003

Name:second guy
Account Number :2
Age:26
Phone:2128836234
Value:333.720001

Name:third guy
Account Number :3
Age:26
Phone:6182213
Value:-62.000000
[OP''s code]


>

我的代码是......


结构记录{

int acctnum;

char name [20];

浮动值;

char phone [10];

int age;

};


结构记录数据;

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

while(fread(&) ; data,sizeof(struct record),1,file)== 1)

{

printf(" Name:%s \\\
Account Number:%d \\ \\ nAnge:%d \ nPhone:%s \ nValue:%f \ n",

data.name,data.acctnum,data.age,data.phone,data.value) ;

}
>
My code is...

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

struct record data;
FILE *file = fopen("db07", "rb");
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);
}


swetha写于11/19/07 16:24,:
swetha wrote On 11/19/07 16:24,:

HI Every1,

我在读取二进制文件时遇到问题。

实际上我想要一个C程序从中读取数据

的文件是二进制形式at和我想要更新它中的值。

该文件由类型----


struct record {
$ b的结构组成$ b int acountnum;

char name [20];

浮动值;

char phone [10];

}


我写了一个程序,它将这些值读入我在程序中创建的
结构中。虽然打印了值,但我得到了 ;名称"和

电话正确的值(因为它们在char中)....但是我没有

获取accountnum和value fields.Iam得到一些仲裁

values.SO可以帮助我在这个如何正确检索值

......


我的代码是......


结构记录{

int acctnum;

char name [20];

浮动值;

char phone [10];

int age;

};


结构记录数据;

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

while(fread(& data,sizeof(struct record),1,file)== 1)

{

printf("名称:%s \ nAccount编号:%d \ nAge:%d \ nPhone:%s \ nValue:%f \\ \\ n",

data.name,data.acctnum,data.age,data.phone,data.value);

}
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.
The file consists of structures of type----

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

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;
};

struct record data;
FILE *file = fopen("db07", "rb");
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);
}



任何一件事 - 或者说它们的组合 - 都可能是问题的原因。以下是您应该采取的一些方面

一看:


1)为什么您的程序会尝试阅读年龄?元素

在你的文件格式描述中没有提到?

如果你读到的布局与布局不匹配的结构

文件,你不能指望发生任何有用的事情。


2)文件写在与

兼容的系统上一个'试图读它?系统在许多方面有所不同,

,例如数字(int,float)的表示方式,

数量以及结构中填充的位置,等等。 />
填充差异会产生类似(1),

和表示差异(例如,endianness)的问题,即使是棘手的:
这意味着123到一台机器可能很容易对另一台机器来说意味着-2231369728。


3)名称和电话元素:文件中实际存在20或10

个字符,即使字符串

与Zaphod一样短。和不适用?或者文件只保留了重要的价值。字符? (并且看看Martin Ambuhl的

警告这些物品可能没有串联。)


前几百的十六进制转储文件的字节,

以及他们应该的内容代表和你实际从你的程序获得的
可能会有所帮助。


-
Er ********* @ sun.com


这篇关于阅读二进制文件....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆