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