📄 io.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:45 2005 by faqproc version 2.7 --><!-- from source file struct.sgml dated Wed Dec 21 16:15:32 2005 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/struct/io.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:46 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 2.11</title><link href="anonstruct.html" rev=precedes><link href="padding.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="anonstruct.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="padding.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../index-2.html"><img src="../images/buttontop.gif" alt="top/contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><hr><p><!-- qbegin --><h1>comp.lang.c FAQ list<font color=blue>·</font><!-- qtag -->Question 2.11</h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I read/write structures from/todata files?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It is relatively straightforwardto write a structureoutusing <TT>fwrite</TT>:<pre> fwrite(&somestruct, sizeof somestruct, 1, fp);</pre>and a corresponding<TT>fread</TT> invocation can read it back in.What happens here is that<TT>fwrite</TT> receives a pointer to the structure,and writes(or <TT>fread</TT> correspondingly reads)the memory image of the structureas a stream of bytes.The <TT>sizeof</TT> operator determineshow many bytes the structure occupies.</p><p>(Thecall to <TT>fwrite</TT> above is correctunder an ANSI compileras long as a prototype for <TT>fwrite</TT> is in scope,usually because <TT><stdio.h></TT> is <TT>#include</TT>d.</p><p>However,data fileswrittenas memory imagesin this waywill<em>not</em>beportable,particularlyif they contain floating-point fields or pointers.The memory layout of structures ismachine andcompiler dependent.Different compilers may use different amounts of padding(see question <a href="padding.html">2.12</a>),andthe sizes and byte orders of fundamental typesvary across machines.Therefore,structures written as memory imagescannot necessarily be read back inby programs running on other machines(or even compiled by other compilers),and this is animportant concern ifthe data files you're writingwill ever be interchanged between machines.See alsoquestions<a href="padding.html">2.12</a>and<a href="../misc/binaryfiles.html">20.5</a>.</p><p>Also,if the structure contains any pointers(<TT>char *</TT> strings,or pointers to other data structures),only the pointer values will be written,and they are most unlikely to be valid when read back in.Finally,notethatfor widespread portabilityyou must use the <TT>"b"</TT> flagwhen opening the files;see question <a href="../stdio/fopenbinary.html">12.38</a>.</p><p>A more portable solution,though it's a bit more work initially,is to write a pair of functionsfor writing and reading a structure,field-by-field,in a portable(perhaps even human-readable)way.</p><p>References:H&S Sec. 15.13 p. 381<br></p><!-- aend --><p><hr><a href="anonstruct.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="padding.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../questions.html"><img src="../images/buttontop.gif" alt="contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><br><!-- lastfooter --><a href="../about.html">about this FAQ list</a> <a href="../eskimo.html">about eskimo</a> <a href="../search.html">search</a> <a href="../feedback.html">feedback</a> <a href="copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></body><!-- Mirrored from c-faq.com/struct/io.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:46 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -