Difference Between Binary and ASCII writing?

This is a discussion on Difference Between Binary and ASCII writing? within the C Programming forums, part of the General Programming Boards category; I really don't know the difference between writing to a files using binary mode, and doing so using ASCII mode....so ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Difference Between Binary and ASCII writing?

    I really don't know the difference between writing to a files using binary mode, and doing so using ASCII mode....so what's the difference? I once had a problem with writing a blog feature, and it only worked properly when I wrote to files with fwrite() and not fputs(), which is binary-mode writing.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Some strange operating systems distinguish between binary and text mode. If you write a newline (\n) in C in text mode, it may translate that to something else. Also, if you read that in text mode, you will see a \n. Binary mode will ensure this doesn't happen. Another side effect of text mode is that a certain character can cause the end-of-file status to be set.

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Quote Originally Posted by cwr
    Another side effect of text mode is that a certain character can cause the end-of-file status to be set.
    Could you please give me some examples. Thanks.

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Antigloss
    Could you please give me some examples. Thanks.
    No, since I don't have access to any platforms that exhibit this behaviour. Try putting a Windows EOF character (0x1A) into the middle of a file, then opening it in text mode and reading from it. You may get an EOF at that point. It's been more than ten years since I programmed under Windows, so this may have changed, or my memory is hazy.

  5. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    So it's better to use binary mode rather than text mode?

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Use binary mode for binary files, text mode for text files

  7. #7
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Still don't quite get it...

    But apparently, if you write "65" to a file in text mode, it will produce "65" as expected...in binary mode, does that translate to 'A'? I still don't see the difference in HOW it actually writes to the file.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    If you write "Hello\n" to a file in binary mode, the actual bytes written are:
    54 65 73 74 0A

    If you write "Hello\n" to a file in ASCII mode, the bytes written are:

    54 65 73 74 0D 0A - On a Windows system
    54 65 73 74 0A - On a Unix/Linux system
    54 65 73 74 0D - On a Mac system

    In text mode, the newline character is translated to the byte (or bytes) which are used to represent newlines on that system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with c program: binary to ascii program
    By bigmac(rexdale) in forum C Programming
    Replies: 26
    Last Post: 02-03-2008, 02:26 PM
  2. Binary to Ascii Conversion
    By codemaster12 in forum C Programming
    Replies: 2
    Last Post: 10-24-2007, 11:57 AM
  3. Wininet Binary and ASCII
    By maxorator in forum Windows Programming
    Replies: 5
    Last Post: 11-26-2005, 03:16 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Replies: 4
    Last Post: 10-16-2001, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21