473,839 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing a structure

Is it possible to write a structure to a file in c...as in c++...??
is it using fwrite??
thanx
glen
Nov 14 '05 #1
29 3586

"Glen" <gl********@yah oo.co.in> wrote in message
news:eb******** *************** ***@posting.goo gle.com...
Is it possible to write a structure to a file in c...as in c++...??
Yes.
is it using fwrite??


That's what you'd use to write an exact binary
image, yes. Another way would be to write
a textual version, one member at a time.

-Mike
Nov 14 '05 #2

"Glen" <gl********@yah oo.co.in> wrote in message
news:eb******** *************** ***@posting.goo gle.com...
Is it possible to write a structure to a file in c...as in c++...??
Yes.
is it using fwrite??


That's what you'd use to write an exact binary
image, yes. Another way would be to write
a textual version, one member at a time.

-Mike
Nov 14 '05 #3
Mike Wahler wrote:
"Glen" <gl********@yah oo.co.in> wrote in message
news:eb******** *************** ***@posting.goo gle.com...
is it using fwrite??


That's what you'd use to write an exact binary
image, yes. Another way would be to write
a textual version, one member at a time.


The second option would be better, because, AFAIK an exact binary image
may have problems with the size of the members of the structure, with
the alignment choosen by the compiler for the members of the structure
and even byte-endianness, if the output file is to be used across
different platforms.

I may be wrong, thought, and I would love to be corrected here if I am.

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Rogério Brito - rb****@ime.usp. br - http://www.ime.usp.br/~rbrito
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Nov 14 '05 #4
Mike Wahler wrote:
"Glen" <gl********@yah oo.co.in> wrote in message
news:eb******** *************** ***@posting.goo gle.com...
is it using fwrite??


That's what you'd use to write an exact binary
image, yes. Another way would be to write
a textual version, one member at a time.


The second option would be better, because, AFAIK an exact binary image
may have problems with the size of the members of the structure, with
the alignment choosen by the compiler for the members of the structure
and even byte-endianness, if the output file is to be used across
different platforms.

I may be wrong, thought, and I would love to be corrected here if I am.

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Rogério Brito - rb****@ime.usp. br - http://www.ime.usp.br/~rbrito
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Nov 14 '05 #5
In <eb************ **************@ posting.google. com> gl********@yaho o.co.in (Glen) writes:
Is it possible to write a structure to a file in c...as in c++...??
is it using fwrite??


There is more than one way of doing it. The two most popular approaches
are:

1. Use a text file, and convert each field of the structure to a textual
representation, using fprintf.

2. Use a binary file and dump the binary representation of the
structure value with fwrite.

The first approach is more portable (the value can be read on a different
platform, or even with a program written in a different language), but
uses more disk space and CPU cycles.

The second approach uses less disk space and CPU cycles, but the resulting
binary file can be read only by a C program compiled with the same
compiler, on the same platform.

There are also compromises between the two methods, like XDR (a platform
independent binary data format).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
In <eb************ **************@ posting.google. com> gl********@yaho o.co.in (Glen) writes:
Is it possible to write a structure to a file in c...as in c++...??
is it using fwrite??


There is more than one way of doing it. The two most popular approaches
are:

1. Use a text file, and convert each field of the structure to a textual
representation, using fprintf.

2. Use a binary file and dump the binary representation of the
structure value with fwrite.

The first approach is more portable (the value can be read on a different
platform, or even with a program written in a different language), but
uses more disk space and CPU cycles.

The second approach uses less disk space and CPU cycles, but the resulting
binary file can be read only by a C program compiled with the same
compiler, on the same platform.

There are also compromises between the two methods, like XDR (a platform
independent binary data format).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
Rogério Brito wrote:
Mike Wahler wrote:
Glen wrote:
is it using fwrite??


That's what you'd use to write an exact binary image, yes.
Another way would be to write a textual version,
one member at a time.


The second option would be better, because, AFAIK,
an exact binary image may have problems
with the size of the members of the structure,
with the alignment chosen by the compiler
for the members of the structure and even byte-endianness,
if the output file is to be used across different platforms.

I may be wrong, thought, and I would love to be corrected here if I am.


The "textual version" may have problems
with precise representation of floating-point numbers.

The range and precision of fixed (integral) and floating-point types
may vary from platform to platform
resulting in truncation or rounding
as quantities are read from a text file.

Nov 14 '05 #8
Rogério Brito wrote:
Mike Wahler wrote:
Glen wrote:
is it using fwrite??


That's what you'd use to write an exact binary image, yes.
Another way would be to write a textual version,
one member at a time.


The second option would be better, because, AFAIK,
an exact binary image may have problems
with the size of the members of the structure,
with the alignment chosen by the compiler
for the members of the structure and even byte-endianness,
if the output file is to be used across different platforms.

I may be wrong, thought, and I would love to be corrected here if I am.


The "textual version" may have problems
with precise representation of floating-point numbers.

The range and precision of fixed (integral) and floating-point types
may vary from platform to platform
resulting in truncation or rounding
as quantities are read from a text file.

Nov 14 '05 #9
> Another way would be to write
a textual version, one member at a time.

-Mike


well i did it in textual ...well fwrite isnt working..err in the way i want...
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5635
by: DanielEKFA | last post by:
Hey there :) I was once told that the STL classes had member functions to write their data to disk and to restore that data. Searching google (and why are there no "stl" or "map" manpages?), it seems like someone was pulling my leg, because I really can't find anything about it. So, was I misinformed? Or perhaps this person was talking about some other classes you could use to do such a thing?
15
340
by: Glen | last post by:
Is it possible to write a structure to a file in c...as in c++...?? is it using fwrite?? thanx glen
9
1970
by: curious_one | last post by:
All, I have a struct struct { char a; char b; }some_struct; I have a shared memory that can contain 16bit wide data, I find that when writing an 8bit value in to char "a" the same value is over-written onto "b".
8
2501
by: mojozoox | last post by:
Could you tell me scenarios in which core's dumped when writing to a pipe. write (pipefd,&Struct,structsize); on windows too this give a memory "read" violation. Could anyone tell me why. thanks in advance.
2
1772
by: DBC User | last post by:
Hi Sharpies, I have a C program I am converting it into C#. Everything is fine except this process creates a 6K byte binary file. This file initially filled with 6K null and then start populating only the fields with value in specified locations(3 seperate structures). The way the C does is by creating a structures, which will be filled with null initially. Then selectivly populating only the fields with values and only up to the length...
5
4352
by: Phil Kelly | last post by:
Hi I need to write the contents of a structure to a binary file - there is one string and 2 integers, but I can't seem to figure out how to write the data correctly. If I am simply writing text to a file there is no problem - that starts when I attempt to write the structure. Can someone help me out, please?
1
2107
by: tim | last post by:
Someone using Python Midi Package from http://www.mxm.dk/products/public/ lately? I want to do the following : write some note events in a midi file then after doing that, put some controllers at the beginning of the midifile (because I want to be able to make those dependant on what notes were just written)
6
5280
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
3819
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file. Here is my part of my code. struct cust_data { int nCAFID; char *szFirstName;
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10297
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7021
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5683
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4493
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4066
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.