从文件中读取数据 [英] Reading data from files

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

问题描述



如何从文件到结构读取数据(具有更多元素
元素的结构)

感谢ad /ance
/>
-

通过 http://dbforums.com <发布/ a>



最初由Burne C发表

" krishna_v"在消息新闻中写道:3150950.1059105697@dbforums.-
comnews:31 **************** @ dbforums.com ...

如何从文件到结构读取数据(结构有


更多

元素)
谢谢你的意思
-
发表于

http://dbforums.com/http://dbforums .com

如果您自己将结构写入文件,并且您必须稍后(在同一台机器上)阅读它,请尝试以下方法:

#include

/ *一些结构* /
typedef struct mystruct_tag
{var / int int var2;
int var2;
long var3;
} mystruct;

int main(void)
{
/ *创建文件* /
FILE * fp = tmpfile();
mystruct mst1;
mystruct mst2 = {10,20,30};

/ *写结构* /
fwrite(& ; mst2,sizeof(mystruct),1,fp);

倒带(fp);

/ *尝试将其读回* /
fread(& ; mst1,sizeof(mystruct),1,fp);

printf(" struct contains%d%d%d
\ n",mst1.var1,mst1.var2 ,mst1.var3);

fclose(fp);

返回0;



}

-

通过 http://dbforums.com




最初由Zoran Cutura发表

krishna_v写道:


如何从文件到结构读取数据(结构有


更多

元素)



这几乎取决于如何将数据存储到
文件中,这就是人们称之为文件格式的内容。
请注意,结构不能随便写入和从文件中读取
如bl ocks,因为它们可能包含系统相关的东西,比如
padding
bytes。因此,如果您正在讨论以二进制形式写入
文件的结构数据作为块,您可能会烧伤手指。
将结构的每个元素存储为其值拥有并阅读它
返回就是这个。

-
Z(Zo**********@daimlerchrysler.com)
LISP值得学习深刻的启蒙经验
当你终于得到它时你将拥有;这段经历会让你在余下的日子里成为一名更好的程序员。 - Eric S. Raymond




HI

谢谢。


-

通过 http://dbforums.com 发布



How to read data from files to structures(structure having more
elements)
Thanks in adavance

--
Posted via http://dbforums.com

解决方案


Originally posted by Burne C

"krishna_v" wrote in message news:3150950.1059105697@dbforums.-
comnews:31****************@dbforums.com...

How to read data from files to structures(structure having


more

elements)
Thanks in adavance
--
Posted via


http://dbforums.com/http://dbforums.com

If you write the structure to the file by yourself and you have to
read it later (on the same
machine), try this method :

#include

/* some structure */
typedef struct mystruct_tag
{
int var1;
int var2;
long var3;
}mystruct;

int main(void)
{
/* create a file */
FILE *fp = tmpfile();
mystruct mst1;
mystruct mst2 = {10,20,30};

/* write the structure */
fwrite(&mst2,sizeof(mystruct),1,fp);

rewind(fp);

/* try to read it back */
fread(&mst1,sizeof(mystruct),1,fp);

printf("The struct contain %d %d %d
\n",mst1.var1,mst1.var2,mst1.var3);

fclose(fp);

return 0;


}

--
Posted via http://dbforums.com



Originally posted by Zoran Cutura

krishna_v wrote:


How to read data from files to structures(structure having


more

elements)



That pretty much depends on how the data is stored into the
file, which
is what people call file formats.
Note that structures cannot be portable written and read from a file
as blocks, because they may include system dependent stuff like
padding
bytes. So if you''re talking about structure data that is written to a
file in binary as a block, you''ll probably burn your fingers.
Store every element of the structure as a value of its own and read it
back is this.
--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond



HI
Thank you.

--
Posted via http://dbforums.com


这篇关于从文件中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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