LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-16-2007, 03:15 PM   #1
ilnli
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Slackware 10.0, SUSE 9.1, RH 7, 7.3, 8, 9, FC2
Posts: 413

Rep: Reputation: 32
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
 
Old 08-16-2007, 05:42 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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:

Code:
C tutorial
You should also be aware that the data you write will not be portable from one architecture to another.

Hope this helps.
 
Old 08-16-2007, 07:56 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.
 
Old 08-16-2007, 09:28 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 08-16-2007, 11:00 PM   #5
JoeyAdams
Member
 
Registered: Jun 2006
Distribution: Kubuntu Hardy
Posts: 94

Rep: Reputation: 15
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.
 
Old 08-17-2007, 12:20 AM   #6
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
Old 08-17-2007, 02:43 PM   #7
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Quote:
Originally Posted by wjevans_7d1@yahoo.co View Post
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.
 
Old 08-17-2007, 06:09 PM   #8
ilnli
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Slackware 10.0, SUSE 9.1, RH 7, 7.3, 8, 9, FC2
Posts: 413

Original Poster
Rep: Reputation: 32
Thanks all of you guys you have really helped me.

regards,
Imran
 
Old 08-20-2007, 09:54 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading and Writing integers to binary file oulevon Programming 2 02-26-2006 12:27 AM
writing and reading from a file! sahel Programming 1 12-27-2005 01:33 PM
vb.net reading and writing to a text file simultaneously mrobertson Programming 3 09-08-2005 12:30 PM
C File reading and writing AbhishekSamuel Programming 3 05-03-2005 03:59 PM
Help reading and writing to file xiste Programming 1 04-15-2005 12:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration