Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

fwrite() & fprintf() -- binary or text ???

fh791fh791 Posts: 35Member
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,

Comments

  • DonotaloDonotalo Posts: 715Member
    : 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,
    :
    [purple][blue]fprintf()[/blue] writes formatted output. probably this is the reason that [blue]fprintf()[/blue] overrides how the file is opened.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • PrzemekG_PrzemekG_ Posts: 595Member
    : : 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,
    : :
    : [purple][blue]fprintf()[/blue] writes formatted output. probably this is the reason that [blue]fprintf()[/blue] overrides how the file is opened.
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    :
    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:

    [code]
    FILE* f1 = fopen("file1.txt", "w");
    FILE* f2 = fopen("file2.txt", "w");

    char* data = "HELLOWORLD";
    int len = 11;

    fwrite(data, 1, len, f1);
    fprintf(f2, data);
    [/code]

    fprintf will write only "HELLO" to the file, because to check your data length, it will use strlen, will will stop at character 0 (""), while to fwrite, you will pass the true length of the data, so it will write "HELLOWORLD" to the file.

Sign In or Register to comment.