The diference between binary and text

This is a discussion on The diference between binary and text within the C++ Programming forums, part of the General Programming Boards category; I have asked this before but i didn't understand, so if it could be explained again that would be appreciated, ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    142

    The diference between binary and text

    I have asked this before but i didn't understand, so if it could be explained again that would be appreciated, i need to know what the difference between binary and text files are, i know that with text files you can use the << and >> operators as well as things like cin.getline() but with binary you use write() and read(), but what is the difference can somebody explain it to me without the jargon please? eventually i will know how to use both efficiently but untill then i need to understand how they are used so i know which way to go in my project.

    Thanks in advance
    Last edited by wart101; 12-03-2006 at 10:35 PM.
    WhAtHA hell Is GoInG ON

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Binary files give you the data byte for byte as it is in the file.

    Text files don't, exactly. In Windows, for example, a newline is stored as hexadecimal 0A 0D, but when read in "text" mode, you only get a "\n". (Why this is, I don't know... probably dates back to some issue long ago... I think dealing with consoles, but don't quote me. Too young. )
    If you were to try to read a binary file as text, you wouldn't get the data you needed, as it probably wouldn't be text. Text files are things like *.txt, *.html, *.php, anything composed of text characters. Binary files are things not meant to be read by humans: *.zip, *.exe, *.gif, *.bmp, *.mp3 (both lists go on an on)

    Here's a link. Also, try searching Google.
    Last edited by Cactus_Hugger; 12-03-2006 at 11:20 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    To elaborate:

    In text mode whitespace characters are processed and altered; in binary mode you read byte-for-byte.

    As to why 0D 0A, it's a "carriage return-linefeed" pair, which DOS used but UNIX did not.

    The term carriage return, which comes from the typewriter, means to move the cursor to the beginning of the current line. Linefeed, also from typewriter terminology, means to advance to the next line. Special ASCII characters were reserved for these functions because that is how the data was sent to a printer, which did have a print head that needed to return and did need to linefeed, so the control characters would make the printer behave just like the typewriter.

    When it came to files, these two characters have been used in different ways on different systems to signify newlines in text files:

    DOS/Windows used CR+LF to signify a newline.
    Apple up through OS9 used just CR for a newline.
    UNIX/Linux/OSX use just LF for a newline.

    In C++ '\n' is some character (often chosen to be LF but it doesn't have to be) that is used to mark newlines. In text mode \n will be expanded to the newline character sequence appropriate for that platform.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by Cactus_Hugger
    Binary files give you the data byte for byte as it is in the file.

    Text files don't, exactly. In Windows, for example, a newline is stored as hexadecimal 0A 0D, but when read in "text" mode, you only get a "\n". (Why this is, I don't know... probably dates back to some issue long ago... I think dealing with consoles, but don't quote me. Too young. )
    If you were to try to read a binary file as text, you wouldn't get the data you needed, as it probably wouldn't be text. Text files are things like *.txt, *.html, *.php, anything composed of text characters. Binary files are things not meant to be read by humans: *.zip, *.exe, *.gif, *.bmp, *.mp3 (both lists go on an on)

    Here's a link. Also, try searching Google.
    Ok, i think i'm begining to understand, so if you have a text file that consists of 30 bytes which would equate to 30 characters 1 per byte using binary file type would then empower you to monipulate the these characters on a more one on one basis?

    but if you have a text file the functions that are available to monipulate the file work more on a line to line basis or as a whole?

    is it that there is a destinction between the way the two type read and write?

    also for my project i need to store information passed to a structure then sent to a file ie (stores.ddf), then retreived later, for example below is the information has been sent to the file.

    Item 22222 Description Cheese Quantity 230

    now i have managed to get this into a text file pretty easy, i have set the programme to loop and every new entry goes to the next line, i have had a lot of trouble retrieving the information (which i will work out myself eventually) but what i need to know is if i where using a binary file type would it be easier (after learning) to get the information, like you said this information isn't intended to be stored as text but only for the programms eyes (if you get me).

    please let me know if i am on the right track as i will set my train of thought around this.

    Check this out.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        
        int length;
        char string[20], q;
        
        do{
        
        cout<<"Please type some text > ";
        cin.getline(string,20);
        cout<<"\nIs this what you typed > "<< string <<"\n";
        cout<<"(Y/N) > ";
        cin>> q;
        cin.get();
        }
       
        
        while (q=='n');
        
        length=strlen(string);
        cout<<"The string length is "<< length <<" characters long "<<endl;
        cin.get();
        
        cin.get();
        ofstream afile;
        afile.open("binary.ddf",ios::binary|ios::out);
        afile.write(string,length);
        afile.close();
    
    }

    (i wrote this after the original post) but if i am correct what it is saying is from the first space in memory pointed to by (string) get the next 5 bytes and output them to file stores.ddf, is that right?

    if it is, i'm wrapped because i think i just got through a massive learning curve
    Last edited by wart101; 12-04-2006 at 11:18 PM.
    WhAtHA hell Is GoInG ON

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, it's not easier. Some things are easier to do in binary files, some are easier in text files. A CSV-like format like you have here is a text format. You could perhaps store these things record-wise in binary, but the advantage would be minor and it would have its own share of problems.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by CornedBee
    No, it's not easier. Some things are easier to do in binary files, some are easier in text files. A CSV-like format like you have here is a text format. You could perhaps store these things record-wise in binary, but the advantage would be minor and it would have its own share of problems.
    one question, how do you read from a text file, with binary it's read() but what do you do with text?
    WhAtHA hell Is GoInG ON

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    how do you read from a text file
    >> for example
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by vart
    >> for example
    done that but it only gave me one word, how do you get past the spaces?

    do i need a file buffer to do this?

    FORGET THIS QUESTION I WORKED IT OUT
    Last edited by wart101; 12-05-2006 at 04:43 AM.
    WhAtHA hell Is GoInG ON

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    getline()
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by CornedBee
    getline()
    Ok i can get a line from the file, i can even get the whole file into memory, but what i need to do is sort the data, find keywords stuff like that, can you direct me to where these functions might be i can do the rest myself.


    thankyou.
    WhAtHA hell Is GoInG ON

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That's got nothing to do with I/O. That's algorithms and knowing where to find them in the standard library, if they exist.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by CornedBee
    That's got nothing to do with I/O. That's algorithms and knowing where to find them in the standard library, if they exist.
    algorithims? thats the most broard answer i have had on this board, ofcourse ther algorithims but where are they? if anyone know please tell me.
    WhAtHA hell Is GoInG ON

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by CornedBee
    No, it's not easier. Some things are easier to do in binary files, some are easier in text files. A CSV-like format like you have here is a text format. You could perhaps store these things record-wise in binary, but the advantage would be minor and it would have its own share of problems.
    what different kinds of formats are there exactly?
    WhAtHA hell Is GoInG ON

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Quote Originally Posted by wart101
    what different kinds of formats are there exactly?
    http://en.wikipedia.org/wiki/File_format
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by wart101
    algorithims? thats the most broard answer i have had on this board, ofcourse ther algorithims but where are they?
    What do you mean, where are they? They're algorithms. Their domain is computer science. They ought to be in your head. If they're not, get a book about algorithms.

    Once you know which algorithms you need, you can then look for a C++ implementation. The best way to do that is search in your reference.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary text file
    By spanker in forum C Programming
    Replies: 7
    Last Post: 12-28-2007, 01:10 AM
  2. Text file versus Binary file
    By sherwi in forum C Programming
    Replies: 6
    Last Post: 04-15-2006, 02:41 PM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Determining if a file is binary or text
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 08-05-2003, 01:33 PM

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