469,344 Members | 6,443 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,344 developers. It's quick & easy.

Reading data from files


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

--
Posted via http://dbforums.com
Nov 13 '05 #1
2 2786

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
Nov 13 '05 #2

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
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Olivier Maurice | last post: by
4 posts views Thread by Erpman | last post: by
6 posts views Thread by arne.muller | last post: by
10 posts views Thread by Tyler | last post: by
3 posts views Thread by MF AHMED | last post: by
13 posts views Thread by swetha | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.