read and write in binary

This is a discussion on read and write in binary within the C Programming forums, part of the General Programming Boards category; Hi, How can I read or write in binary data. I wish to read one byte (8 bits) in binary ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    read and write in binary

    Hi,

    How can I read or write in binary data. I wish to read one byte (8 bits) in binary and also wish to write the same in binary ie 8 bit byte.

    I have seen that there are two standards 1 is ascii and the other is ansi. I have seen in c its taking ascii code characters and in php its taking ansi, i havent tested others. ofcourse php is different. I am on winxp and using tcc. Is it that if i use turboc or vc or anything other c versions, uses different ascii or ansi chars.

    for example ascii(131) is different to ansi(131).

    Will, i get the same binary values? even if i use different languages?

    Thank you.

  2. #2
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    949
    When you go to open the file, you'll want to use "rb" or "wb" instead of "r" or "w", respectively. To read/write binary values, check out the functions fread() and fwrite(), respectively.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by comedychiru View Post
    Hi,

    How can I read or write in binary data. I wish to read one byte (8 bits) in binary and also wish to write the same in binary ie 8 bit byte.

    I have seen that there are two standards 1 is ascii and the other is ansi. I have seen in c its taking ascii code characters and in php its taking ansi, i havent tested others. ofcourse php is different. I am on winxp and using tcc. Is it that if i use turboc or vc or anything other c versions, uses different ascii or ansi chars.

    for example ascii(131) is different to ansi(131).

    Will, i get the same binary values? even if i use different languages?

    Thank you.
    It would probably help if you gave us more explicit details of what you are trying to do...

    You are apparently confusing two things together here... ascii and ansi are character sets used with text files. If you write an integer like 12345 you will be able to open the file and see 12345. Binary files do not translate values to human readable form... if you write 12345 you will see a bunch of squiggles when you open the file because what you will be looking at is the raw binary data for that value. Thus binary files are not humanly readable.

    The ansi and ascii characters from 0 to 127 are standardized. You can view a chart of their values HERE Characters from 128 to 255 are defined regionally and will be different in North America than they are in Greece and different again from France or Germany.

    If you are looking for a language agnostic character set look into using Unicode which is the programming equivalent of stepping on a landmine since it exposes issues such as Endianness and Line Ending. Windows supports the Unicode standard of UTF16LE and uses cr/lf pairs to end lines. This will allow for almost any character in any language to be represented, but again the character sets vary from region to region and the endian format and line endings also vary from OS to OS creating a veritable rats nest of problems for programmers.

    This is now a hot topic in that the "English Only" days of computing are slowly drawing to a close and Internationalization is becoming the new best thing with programmers being called upon to make applications available in multiple languages through use of resource-only DLLS.

    Binary files are a whole different ball of wax. They are called "binary" because the actual raw data from the computer's memory is written directly to disk and read back directly into memory without any encoding or translation. The disk file itself is usually a series of records, most often created by reading and writing structs directly into and out of memory. This is a highly efficient form of disk storage that is widely used for database, inventory and financial applications. But it is not intended to be humanly readable.

    Hope that helps...

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Quote Originally Posted by CommonTater View Post
    It would probably help if you gave us more explicit details of what you are trying to do...

    You are apparently confusing two things together here... ascii and ansi are character sets used with text files. If you write an integer like 12345 you will be able to open the file and see 12345. Binary files do not translate values to human readable form... if you write 12345 you will see a bunch of squiggles when you open the file because what you will be looking at is the raw binary data for that value. Thus binary files are not humanly readable.

    The ansi and ascii characters from 0 to 127 are standardized. You can view a chart of their values HERE Characters from 128 to 255 are defined regionally and will be different in North America than they are in Greece and different again from France or Germany.

    If you are looking for a language agnostic character set look into using Unicode which is the programming equivalent of stepping on a landmine since it exposes issues such as Endianness and Line Ending. Windows supports the Unicode standard of UTF16LE and uses cr/lf pairs to end lines. This will allow for almost any character in any language to be represented, but again the character sets vary from region to region and the endian format and line endings also vary from OS to OS creating a veritable rats nest of problems for programmers.

    This is now a hot topic in that the "English Only" days of computing are slowly drawing to a close and Internationalization is becoming the new best thing with programmers being called upon to make applications available in multiple languages through use of resource-only DLLS.

    Binary files are a whole different ball of wax. They are called "binary" because the actual raw data from the computer's memory is written directly to disk and read back directly into memory without any encoding or translation. The disk file itself is usually a series of records, most often created by reading and writing structs directly into and out of memory. This is a highly efficient form of disk storage that is widely used for database, inventory and financial applications. But it is not intended to be humanly readable.

    Hope that helps...

    You are absolutely correct. Okay if i write in binary mode to the file, does the C language change the binary data not the chars. I would not care about the chars since its a 8 bit byte (not a unicode) or any other special standard. And I only suppose that the chars or symbols shown on the screen are only an interpretation of the binary data. So if we change language or system os it will differ.

    I, only have a doubt that if we use c code and write the data in binary mode, then will the data be secure in binary format? and this is true for system independent os.?

    Thank you

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm sorry you're going to have to explain that again... I don't know what you're asking...

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    for example ascii(131) is different to ansi(131). Will, i get the same binary values? even if i use different languages?
    On the same processor, if f(x) encodes to 131 in binary and g(x) encodes to 131 in binary then you will get the same binary value for f(x) and g(x).

    Okay if i write in binary mode to the file, does the C language change the binary data not the chars.
    Okay, let's say you open the file with a text editor. In a text editor, the binary data of the file is used to represents characters. If you change the binary data of your file, you change the characters your text editor displays.

    Let's say you open the file with a program that decodes each byte into a musical note and plays the note. If you change the binary data of your file, you change the musical notes that your program plays.

    Let's say you open the file with a program that decodes each byte into a pixel and shows the pixel in a window. If you change the binary of the file, then your program will show a different picture.

    Notice in each example there are two main objects, the file with the data you are modifying and a program that uses the file in a (hopefully) meaningful way.

    I, only have a doubt that if we use c code and write the data in binary mode, then will the data be secure in binary format? and this is true for system independent os.?
    What do you mean by secure? Data security can mean a lot of things. To the best of my knowledge, most C compilers (or compilers/interpreters of ANY language) do not encrypt data by default.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read binary, change a value, write to binary
    By mammoth in forum C Programming
    Replies: 16
    Last Post: 05-12-2011, 10:56 AM
  2. read / write binary files
    By Dark_Phoenix in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2009, 07:56 AM
  3. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 05:31 PM
  4. Read/Write Binary Files
    By Ideswa in forum C++ Programming
    Replies: 11
    Last Post: 08-29-2006, 07:23 AM
  5. binary read/write of objects
    By arjunajay in forum C++ Programming
    Replies: 25
    Last Post: 06-06-2005, 05:35 AM

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