<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'fwrite() &amp; fprintf() -- binary or text ???' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'fwrite() &amp; fprintf() -- binary or text ???' posted on the 'C and C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2012 Programmers Heaven</copyright>
    <pubDate>Wed, 23 May 2012 00:45:44 -0700</pubDate>
    <lastBuildDate>Wed, 23 May 2012 00:45:44 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>fwrite() &amp; fprintf() -- binary or text ???</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/340766/340766/fwrite--fprintf----binary-or-text-/</link>
      <description>hi,&lt;br /&gt;
&lt;br /&gt;
I've noticed that fwrite() writes data in binary format and fprintf()&lt;br /&gt;
writes data in text format regardless to the mode I use in fopen() to&lt;br /&gt;
open the file.&lt;br /&gt;
In this situation, what the mode argument of fopen() readlly does????&lt;br /&gt;
I've examined this, with DEV-C++ and Digital Mars compilers and both&lt;br /&gt;
have produced a same result.&lt;br /&gt;
&lt;br /&gt;
Thanx,&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/340766/340766/fwrite--fprintf----binary-or-text-/</guid>
      <pubDate>Sat, 08 Jul 2006 02:57:59 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: fwrite() &amp; fprintf() -- binary or text ???</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/340766/340783/re-fwrite--fprintf----binary-or-text-/#340783</link>
      <description>: hi,&lt;br /&gt;
: &lt;br /&gt;
: I've noticed that fwrite() writes data in binary format and fprintf()&lt;br /&gt;
: writes data in text format regardless to the mode I use in fopen() to&lt;br /&gt;
: open the file.&lt;br /&gt;
: In this situation, what the mode argument of fopen() readlly does????&lt;br /&gt;
: I've examined this, with DEV-C++ and Digital Mars compilers and both&lt;br /&gt;
: have produced a same result.&lt;br /&gt;
: &lt;br /&gt;
: Thanx,&lt;br /&gt;
: &lt;br /&gt;
&lt;span style="color: Purple;"&gt;&lt;span style="color: Blue;"&gt;fprintf()&lt;/span&gt; writes formatted output. probably this is the reason that &lt;span style="color: Blue;"&gt;fprintf()&lt;/span&gt; overrides how the file is opened.&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;hr /&gt;&lt;span style="color: Purple;"&gt;~Donotalo()&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/340766/340783/re-fwrite--fprintf----binary-or-text-/#340783</guid>
      <pubDate>Sat, 08 Jul 2006 09:40:03 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: fwrite() &amp; fprintf() -- binary or text ???</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/340766/342085/re-fwrite--fprintf----binary-or-text-/#342085</link>
      <description>: : hi,&lt;br /&gt;
: : &lt;br /&gt;
: : I've noticed that fwrite() writes data in binary format and fprintf()&lt;br /&gt;
: : writes data in text format regardless to the mode I use in fopen() to&lt;br /&gt;
: : open the file.&lt;br /&gt;
: : In this situation, what the mode argument of fopen() readlly does????&lt;br /&gt;
: : I've examined this, with DEV-C++ and Digital Mars compilers and both&lt;br /&gt;
: : have produced a same result.&lt;br /&gt;
: : &lt;br /&gt;
: : Thanx,&lt;br /&gt;
: : &lt;br /&gt;
: &lt;span style="color: Purple;"&gt;&lt;span style="color: Blue;"&gt;fprintf()&lt;/span&gt; writes formatted output. probably this is the reason that &lt;span style="color: Blue;"&gt;fprintf()&lt;/span&gt; overrides how the file is opened.&lt;br /&gt;
: &lt;/span&gt;&lt;br /&gt;
: &lt;hr /&gt;&lt;span style="color: Purple;"&gt;~Donotalo()&lt;/span&gt;&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
Binary/Text file support (windows/dos):&lt;br /&gt;
When you use fopen, you only say what mode you want, how to treat the file if it already exists and sometimes you give some additional flags.&lt;br /&gt;
&lt;br /&gt;
So, when you want to open a file for writing, the operating system always gets 3 info:&lt;br /&gt;
1. file descriptor - what file to write,&lt;br /&gt;
2. data - a buffer filled with data you want to write,&lt;br /&gt;
3. length - the length of the data you want to write.&lt;br /&gt;
&lt;br /&gt;
The fact is, that the operating system don't know what type of data is in the buffer (plain text, binary), it only knows it's location in memory and it's length.&lt;br /&gt;
&lt;br /&gt;
The "b" flag in fopen (I think only used by DOS and windows) is only a flag which tells the operating system you are opening a binary file. When the operating system finds such flag, it assume you will randomly change the position in the file (with fseek) and will use diferant caching policy (to speedup your access to that file).&lt;br /&gt;
&lt;br /&gt;
Writing binary/text data:&lt;br /&gt;
 &lt;br /&gt;
&lt;pre class="sourcecode"&gt;
FILE* f1 = fopen("file1.txt", "w");
FILE* f2 = fopen("file2.txt", "w");

char* data = "HELLO\0WORLD";
int len = 11;

fwrite(data, 1, len, f1);
fprintf(f2, data);
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
fprintf will write only "HELLO" to the file, because to check your data length, it will use strlen, will will stop at character 0 ("\0"), while to fwrite, you will pass the true length of the data, so it will write "HELLO\0WORLD" to the file.&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/340766/342085/re-fwrite--fprintf----binary-or-text-/#342085</guid>
      <pubDate>Fri, 28 Jul 2006 11:44:13 -0700</pubDate>
      <category>C and C++</category>
    </item>
  </channel>
</rss>
