Reading/Writing in Binary

This is a discussion on Reading/Writing in Binary within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a complicated (to me) problem reading/Writing data in binary. Here is what I am doing. For example ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Reading/Writing in Binary

    Hi,
    I have a complicated (to me) problem reading/Writing data in binary. Here is what I am doing.

    For example let say a text file has the following:
    File: test.txt
    -----------------
    1234a5678b9
    -----------------

    Here is what I am doing:

    istream inData;
    ostream outData;
    inData.open("test.txt", ios::in | ios::binary);
    outData.open("modified.txt", ios:ut | ios::binary);
    inData.seekg(0, ios::end);
    int size = inData.tellg();

    char *temp;
    temp = new char[size + 2];

    inData.read(reinterpret_cast<char *>(temp), size);

    temp[size + 1] = 'c';
    temp[size + 2] = 'd';

    outData.write(reinterpret_cast<char *>(temp), (size + 2);

    -----------------------------------------------------

    The code above should work okay. As you can see, all I am doing is reading data from a file, add two characters "c" and "d" and then writing it back to the file. Everything is fine.

    Now, let say I read the modified file back.

    Here is what I am doing:

    istream modData;
    ostream weird;

    modData.open("modified.txt", ios::in | ios::binary);
    weird.open("hungry", ios:ut | ios::binary);

    inData.seekg(0, ios::end);
    int size = inData.tellg();

    char *temp, charA, charB;
    temp = new char[size];

    inData.read(reinterpret_cast<char *>(temp), size);

    charA = temp[size - 2];
    charB = temp[size - 1];

    -----------------------------------------------------

    Do you see what I am trying to doing?

    1)read fileA in binary
    2)save data from fileA to tempX
    3)add characters to tempY
    4)write temp to fileB

    -----------------------
    1)read fileB in binary
    2)save data from fileB to tempZ
    3)extract ONLY those characters I added to tempX from tempZ
    -----------------------

    The method I use to extract the added characters from the modified file after I read it back is subscript. I assume let say temp[5] is 'a'. When I read the data back, I look at temp[5].

    My question is when you read a file in binary, add characters to it in specific spaces and write it to modified file, is it possible to read the data from the modified file and extract those characters you added via assumption subscript? How are data saved when you read and write in binary?

    Thanks,
    Kuphryn

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I was able to run your code and read back 'c' and 'd' with the following change:

    temp[size] = 'c';
    temp[size + 1] = 'd';

    This change is because in c++, an array starts with subscript 0.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    Also, does temp need the last space for NULL since it is a character array?

    Kuphryn

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Also, does temp need the last space for NULL since it is a character array?

    No. read() will read 'size' characters, if the file contains that many, leaving no room for a NULL anywary. The last space temp[size-1] will contain whatever character it reads last. In this case we're reading binary data, so in most cases, the whole character array would not be printable anyway. Some individual characters may be printable. If all the file consisted of was a string, then it might be useful, but then you'd have to do this:

    temp = new char[size + 1];
    inData.read(reinterpret_cast<char *>(temp), size);
    temp[size] = NULL;

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Cool.

    Thanks.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  2. Reading/Writing Binary
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2006, 10:15 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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