missing last bytes when reading binary file with C++ iostreams

Hello All,

Could you please help me on below issue!!!!

I was writing and reading binary file as below, when i write array to file it was writing good but when i read same array i am unable to read last array index or value.

<Writing code snip>

std::ifstream inFile(abs9.txt1);
if (!inFile) {
std::cout<<"INFO : CAN NOT BE OPEN...."<<"\n";
std::cout<<"PLEASE CHECK GIVEN INPUT FILE."<<"\n";
exit(1);
}
std::string tempLine;
std::string delimiter="\n";
std::cout<<"Record Count is :"<<recordCount<<"\n";
std::cout<<"Line count is :"<<lineCount<<"\n";
// Allocate memory to hold address of record for Index table.
pAddress=new long int [recordCount];
std::ofstream outFile("index.bin");
if (!outFile) {
std::cout<<"ERROR: COULD NOT CREATE"<<temptargetFile<<"FILE.."<<"\n";
exit(1);
}
// Read input file and write data to RVR database file line by line.
std::cout<<"INFO :STAGE1 !! "<<"\n";
std::cout<<":DATA IS BEING LOADED TO DATABASE, PLEASE BE WAIT..."<<"\n";
recordCount=0;
while ( getline(inFile, tempLine)) {
long int tempLineLength=0;
//tempLineCount=tempLineCount + 1;
tempLineLength= tempLine.length() + delimiter.length();
if (tempLine[0] == '>' || (tempLine[0] == ' ' && tempLine[1] =='>') || (tempLine[0] == ' ' && tempLine[1] == ' ' && tempLine[2] =='>')
|| (tempLine[0] == ' ' && tempLine[1] == ' ' && tempLine[2] ==' ' && tempLine[3] == '>')
|| (tempLine[0] == ' ' && tempLine[1] ==' '&& tempLine[2] ==' ' && tempLine[3] == ' ' && tempLine[4]== '>')
|| (tempLine[0] == '\t' && tempLine[8] =='>')) {

outFile<<tempLine<<'\n';
numberOfBytes=inFile.tellg()-tempLineLength;
pAddress[recordCount]=numberOfBytes;
recordCount=recordCount + 1;
} else {
outFile<<tempLine<<'\n';
if (tempLineCount == lineCount-1) {
numberOfBytes=inFile.tellg();
pAddress[recordCount]=numberOfBytes;
}
}
tempLineCount=tempLineCount + 1;
}
inFile.close();
outFile.close();
delete [] pAddress;
delete [] pAddressStatus;
</snip>


<Reading code snip>

long int *pAddress=0, *pAddressStatus;
long int recordCount=6626;
std::ifstream readIndex("index.bini", std::ios::in | std::ios::out | std::ios::binary);
readIndex.seekg(0);
pAddress=new long int[recordCount];
if (!pAddress) {
std::cout<<"ERROR :MEMORY CAN NOT BE ALLOCATED TO ADDRESS ........"<<"\n";
exit(1);
}
pAddressStatus=new long int[recordCount];
if (!pAddressStatus) {
std::cout<<"ERROR :MEMORY CAN NOT BE ALLOCATED TO ADDRESS ........"<<"\n";
exit(1);
}

//readIndex.seekg(0);
readIndex.read((char*)pAddress, sizeof(long int)*recordCount);
readIndex.read((char*)pAddressStatus, sizeof(long int)*recordCount);
readIndex.close();
//std::cout<<pAddress<<"\n";
for(int k=1; k<=recordCount;k++) {
std::cout<<"Read Address is:"<<k<<":"<<pAddress[k]<<"\n";
}
delete [] pAddress;
delete [] pAddressStatus;
</ reading code snip>

Writing array content:

Array Index is :6624 & Value is: 12680567
Array Index is :6625 & Value is::12682599
Array Index is :6626 & Value is:12682682 // Missing while reading

Reading array content:

Read Address is:6625 & Value is: 12682599
Read Address is:6626 & Value is: 0


Note :Assume that all variable declared and defined .

So , please help.......................................


Your code is completely unreadable. Please use code tags and indent.
Your code REALLY needs to be in code tags and indented. Also, your code is without any context. How are the snips related? What are the varaibles recordCount and lineCount and how did you initialize them? What array are you actually reading to? What is RVR?

Also, punctuation marks do not live in herds.
Topic archived. No new replies allowed.