Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-16-2007, 03:15 PM
|
#1
|
|
Member
Registered: Jul 2004
Location: Pakistan
Distribution: Slackware 10.0, SUSE 9.1, RH 7, 7.3, 8, 9, FC2
Posts: 406
Rep:
|
struct reading and writing to file
Hi,
I need to write a struct in a file and then later on need to read this structure from the file. Can any one provide me an example that how can I do this.
following is the example structure that I want to write
struct myIP
{
char header;
int length;
char data;
};
thank you
|
|
|
|
08-16-2007, 05:42 PM
|
#2
|
|
Member
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Rep:
|
Just use the regular write() and read() functions. If you're not familiar with those, do this at the command line:
Code:
man 2 open
man 2 close
man 2 read
man 2 write
man 3 errno
man 3 perror
You'll be using the C & operator to make a pointer to the struct in question. If you're not familiar with the & operator, google this:
You should also be aware that the data you write will not be portable from one architecture to another.
Hope this helps.
|
|
|
|
08-16-2007, 07:56 PM
|
#3
|
|
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,466
Rep:
|
It is also true that it will most likely not be able to be read by other programs using read. Also it may be eazier and possibly safer to use fread and fwrite rather then the low level file operators.
|
|
|
|
08-16-2007, 09:28 PM
|
#4
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,967
Rep: 
|
Also, pointers won't remain valid and any dynamic memory associated won't carry over. The only reliable way is to decompose the structure into its components and write to the file in some parseable format. For example, separate data by spaces or some other symbol using fprintf, then parse with fgets, strtok, and the strto* functions. The real problem comes if you have non-string pointers. Then you are talking about an entire file format and parser.
ta0kira
|
|
|
|
08-16-2007, 11:00 PM
|
#5
|
|
Member
Registered: Jun 2006
Distribution: Kubuntu Hardy
Posts: 94
Rep:
|
When writing structs to a file, you need to consider a couple things:
1. Endianness- When numbers are stored in memory, they are stored in either big endian or little endian. PowerPC processors (and others) use big endian, while Intel processors (and others) use little endian. (actually, PowerPC can be either big endian or little endian, but it's big endian by default). Suppose you want to store the number 0x12345678. If you do this:
char c[4];
unsigned long n=0x12345678;
*(unsigned long*)c = n;
In big endian, it will be this:
c[0]=0x12;
c[1]=0x34;
c[2]=0x56;
c[3]=0x78;
In little endian, like this:
c[0]=0x78;
c[1]=0x56;
c[2]=0x34;
c[3]=0x12;
Register shifts won't be affected (i.e. 0x12345678<<8 will still be 0x34567800). When working with numbers mathematically, it's the same on big endian or little endian platforms, but when you break numbers down into smaller units (like bytes) it certainly matters. Therefore, writing structs (or numbers larger than char) to a file on a big endian system will result in different byte ordering than doing so on a little endian system.
2. Even alignment- I don't think this is present in mainstream processors (i.e. PowerPC and Intel), but it is in the Motorola 68k processors. The 68k can't write shorts or longs at odd addresses without writing them one byte at a time, so when you say:
struct myIP
{
char header;
int length;
char data;
};
It might be stored literally as:
struct myIP
{
char header;
char paddingforheader;
int length;
char data;
char paddingforlength;
};
Therefore, group your chars together to save space on 68k platforms.
Overall, if you're worried about cross-architecture compatibility, heed these notes. Otherwise, at least know this issue exists so that when you do need to write a cross-architecture program, you'll know how.
|
|
|
|
08-17-2007, 12:20 AM
|
#6
|
|
Member
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Rep:
|
Quoth the highly esteemed exvor:
Quote:
|
It is also true that it will most likely not be able to be read by other programs using read.
|
Fear not. As long as the structure doesn't include pointers and is being run always on the same architecture with (basically) the same compiler, you're fine.
Quote:
|
it may be eazier and possibly safer to use fread and fwrite rather then the low level file operators.
|
Be not uncertain. Doubt not. There is nothing to be gained with respect to writing and reading structures through fread() and fwrite() instead of read() and write().
The most likely complication is that you can't always know that a later program (perhaps the same one) might be compiled with a different compiler. As JoeyAdams says, this might mess up alignment of items in a struct, giving you problems.
All you gain from this is saving disk space (how much would you really be saving, anyway?) and speed. Unless writing and reading these records consumes a large percentage of your processing time, go with ta0kira's idea of writing the data in human-readable form. It will be easier to maintain the code if your eyes can see the data.
|
|
|
|
08-17-2007, 02:43 PM
|
#7
|
|
Member
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740
Rep:
|
Quote:
Originally Posted by wjevans_7d1@yahoo.co
Be not uncertain. Doubt not. There is nothing to be gained with respect to writing and reading structures through fread() and fwrite() instead of read() and write().
|
fwrite and fread are, also, generally faster, since they buffer buffer the input/output. You can do the same with read and write, but you usually end up reimplementing fread/fwrite, only not as robust or safe.
Edit: It is also ANSI C.
-
OP:
For now, stick to the basics. Try using both ta0kira's method, and just straight binary reading/writing as well. You're learning, so it is all good for you and you'll learn a lot both ways. When you start getting a little more advanced then you can start dealing with more advanced serialization.
Last edited by 95se; 08-17-2007 at 03:44 PM.
|
|
|
|
08-17-2007, 06:09 PM
|
#8
|
|
Member
Registered: Jul 2004
Location: Pakistan
Distribution: Slackware 10.0, SUSE 9.1, RH 7, 7.3, 8, 9, FC2
Posts: 406
Original Poster
Rep:
|
Thanks all of you guys you have really helped me.
regards,
Imran
|
|
|
|
08-20-2007, 09:54 AM
|
#9
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
also, you may have endian problems if you transfer between processors,
e.g. sparc and intel and power PC and VAX
so if you intend to do it properly you need to address this issue
it's generally easier to save ascii if you can, and humans can read it too.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:32 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|