C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28273
Number of posts: 94120

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
fwrite() & fprintf() -- binary or text ??? Posted by fh791 on 8 Jul 2006 at 2:57 AM
hi,

I've noticed that fwrite() writes data in binary format and fprintf()
writes data in text format regardless to the mode I use in fopen() to
open the file.
In this situation, what the mode argument of fopen() readlly does????
I've examined this, with DEV-C++ and Digital Mars compilers and both
have produced a same result.

Thanx,
Report
Re: fwrite() & fprintf() -- binary or text ??? Posted by Donotalo on 8 Jul 2006 at 9:40 AM
: hi,
:
: I've noticed that fwrite() writes data in binary format and fprintf()
: writes data in text format regardless to the mode I use in fopen() to
: open the file.
: In this situation, what the mode argument of fopen() readlly does????
: I've examined this, with DEV-C++ and Digital Mars compilers and both
: have produced a same result.
:
: Thanx,
:
fprintf() writes formatted output. probably this is the reason that fprintf() overrides how the file is opened.


~Donotalo()

Report
Re: fwrite() & fprintf() -- binary or text ??? Posted by PrzemekG_ on 28 Jul 2006 at 11:44 AM
: : hi,
: :
: : I've noticed that fwrite() writes data in binary format and fprintf()
: : writes data in text format regardless to the mode I use in fopen() to
: : open the file.
: : In this situation, what the mode argument of fopen() readlly does????
: : I've examined this, with DEV-C++ and Digital Mars compilers and both
: : have produced a same result.
: :
: : Thanx,
: :
: fprintf() writes formatted output. probably this is the reason that fprintf() overrides how the file is opened.
:

:
~Donotalo()
:
:
Binary/Text file support (windows/dos):
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.

So, when you want to open a file for writing, the operating system always gets 3 info:
1. file descriptor - what file to write,
2. data - a buffer filled with data you want to write,
3. length - the length of the data you want to write.

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.

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).

Writing binary/text data:

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);


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.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.