binary files

This is a discussion on binary files within the C++ Programming forums, part of the General Programming Boards category; does anybody know how to create binary files instead of plain text files? Thanks -Chris...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up binary files

    does anybody know how to create binary files instead of plain text files?

    Thanks
    -Chris

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    just do file io operations in binary mode...
    hasafraggin shizigishin oppashigger...

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    please explain further...

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    when you open a file [with fopen, or whatever...] they give you a parameter in which you can specify the access [read/write/append/create/etc...] and you have a binary/text flag you can set too... depending on what functions you use to do file io... which do you use?
    hasafraggin shizigishin oppashigger...

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    you could write a function that would convert each character to a binary string, but I'm sure it's been done before. I'm not extremely familiar with all the printf() family of functions' conversions for printing integers in various different bases, but there might be somthing in there for printing them as binary strings...you'd have to look at the printf() man pages or help files in whatever compiler you use. You would just make convert the character to it's ASCII value (an integer) and then print that if printf() has anything like that. If not you would need something like the following to do it manually:

    void convertToBinary(char oldString[], char newString[][8]) {

    double *****eck;
    int bitPos = 0;
    char tmpString[strlen(oldString)];
    strcpy(tmpString, oldString);

    for(int i=0; i<strlen(oldString); i++) {

    while(tmpString[i]<pow(2, 8-bitPos)-1) {
    newString[i][bitPos] = 0;
    bitPos++;
    }

    while(bitPos < 8) {
    tmpString = *****eck = double(tmpString[i])/2.0;
    // I've never tried this before but I think oldString will get an integer value of
    // the divide, and *****eck will get the double value of it.

    if(*****eck - int(*****eck) > 0) newString[i][bitPos++] = 1;
    else newString[i][bitPos++] = 0;
    }
    }
    }

    NOTE: I haven't compiled this, it's probably got errors
    So, once you've worked out all the bugs and errors from the above function, it should convert the ASCII string that you pass as oldString into a binary string, that you pass as newString. The form that the binary string takes is given below. This assumes that the first dimension of the two dimensional array, newString is strlen(oldString). I use 8 as the size of the second dimension because a character has 8 bits (0 - 255 in decimal)

    binary string form:

    example:
    oldString = "cat"
    ------------------------
    newString:
    [0][0] = 0
    [0][1] = 1
    [0][2] = 1
    [0][3] = 0
    [0][4] = 0
    [0][5] = 0
    [0][6] = 1
    [0][7] = 1
    end of first character
    [1][0] = 0
    [1][1] = 1
    [1][2] = 1
    [1][3] = 0
    [1][4] = 0
    [1][5] = 0
    [1][6] = 0
    [1][7] = 1
    end of second character
    [2][0] = 0
    [2][1] = 1
    [2][2] = 1
    [2][3] = 1
    [2][4] = 0
    [2][5] = 1
    [2][6] = 0
    [2][7] = 0
    end of third character

    all together:
    cat = 01100011 01100001 01110100 (no spaces though)

    An easy way to print this then would be to overlaod the << operator for two dimensional arrays, if you need help with that ask...not to tough to look up though. Then simply outFile << newString;I just remembered, chars. are only 4 bits, so everywhere I use 8 use 4 instead, unless you are using an unsigned char, which would be 0 - 255, otherwise it's 0-127 = 4 bits. With unsigned chars you could convert a into bitmap to binary for example...anyways it's getting late, early class, not a good combo.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    >just do file io operations in binary mode...

    why didn't you post 5 mins. earlier?

  7. #7
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    i did! ::note to ender for a note to self: take speed typing classes... and summary classes...::
    hasafraggin shizigishin oppashigger...

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    this is yet another waste of time to read from yours truly:

    yeah, so where it has the astericks in my code above, that's not a pointer to a pointer to a pointer to a pointer...etc, the variable name was bit Check, the filter thought I was attempting to swear

  9. #9
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    LOL>.>>>>>> THAT'S FUNNY!!!
    hasafraggin shizigishin oppashigger...

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    Originally posted by doubleanti
    i did! ::note to ender for a note to self: take speed typing classes... and summary classes...::
    so I'm long winded, sue me I was converting b10 to b2...

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    To create binary files, you can either use the c++ type streams or the c functions fopen(), fwrite(), fputc().
    Here's an example of writing one integer to a file, then reading it back, using c++ streams.
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
       int i = 65535;
       int j;
       char *filename = "b.dat";
    
       ofstream out(filename,ios::binary);
       out.write((char *) &i,sizeof(int));
       out.close();
    
       ifstream in(filename,ios::binary);
       in.read((char *) &j,sizeof(int));
       cout << "j:" << j << endl;
       in.close();
       return 0;
    }
    Last edited by swoopy; 11-12-2001 at 12:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  2. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  3. MFC: CStrings & binary files question(s)
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-24-2004, 05:41 PM
  4. Binary files
    By Brian in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 12:13 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:33 PM

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