Saturday, 28 September 2013

How to split the ByteArray by reading from the file in C++?

How to split the ByteArray by reading from the file in C++?

I have written a Java program to write the ByteArray in to a file. And
that resulting ByteArray is a resulting of these three ByteArrays-
First `2 Bytes` is my `schemaId` which I have represented it using short
data type..
Then next `8 Bytes` is my `Last Modified Date` which I have represented it
using long data type.
And remaining bytes can be of variable size which is my actual value for
my attributes..
So I have a file now in which first line contains resulting ByteArray
which will have all the above bytes as I mentioned above.. Now I need to
read that file from C++ program and read the first line which wiil contain
the ByteArray and then split that resulting ByteArray accordingly as I
mentioned above such that I am ablt to extract my schemaId, Last Modified
Date and my actual attribute value from it..
I have done all my coding always in Java and I am new to C++... I am able
to write a program in c++ to read the file but not sure how should I read
that ByteArray in such a way such that I am able to split it as I
mentioned above..
Below is my C++ program which is reading the file and printing it out on
the console..
int main () {
string line;
//the variable of type ifstream:
ifstream myfile ("text.txt");
//check to see if the file is opened:
if (myfile.is_open())
{
//while there are still lines in the
//file, keep reading:
while (! myfile.eof() )
{
//place the line from myfile into the
//line variable:
getline (myfile,line);
//display the line we gathered:
// and here split the byte array accordingly..
cout << line << endl;
}
//close the stream:
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Can anyone help me with that? Thanks

No comments:

Post a Comment