Reading/Writing in Binary

Status
Not open for further replies.
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::eek: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::eek: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
 
code:<HR>
Code:
inData.seekg(0, ios::end);
int size = inData.tellg();<P>char *temp, charA, charB;
temp = new char[size];<P>inData.read(reinterpret_cast<char *>(temp), size);
/me thinks you forgot to reset the position before that reading operation. Your file pointer is still at the end of the file from seekg(0, ios::end). That would mean no data will be read.

As for your other question... it's not quite that simple. You can read from / write to just any part of a file in binary mode. You can decide to take a 10 byte array, and write it from bytes 200 to 209. You can also decide to read in only part of a file.

For instance, if you want to read the last 2 characters of a file:

char buffer[2];
inFile.seekg(-2, ios::end); // Not sure if that's the way
inFile.read(buffer, 2);

Just don't think of a file as a big array of characters that you load in memory and write back later. Read only the chunks you need, and write only what you have to.
 
Status
Not open for further replies.