Hello All,

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

I am write and read binary file as below, when i write array to file it was writing good but when i read same array i am always loosing last values and below is the code for both operations.

Writing Code:

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;

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;

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 // Here the value should be 12682682


Note :Assume that all variable declared and defined properly.

So , please advise.

Firstly, please find an alternative to that monster of an if statement that's just...what o.O

If you don't want to change it, please explain what it does. My brain works slowly on a morning ^^

First Use a program like BinViewer to actually see if the content is written to the file.

I am doing in Unix and tried to map with BinViewer, but not useful. I could not see the contents as expect from Binviewer.

Thanks & Regards,
Balu

I could not see the contents as expect from Binviewer.

You didn't know how or you didn't see the contents. The latter states that the file is not written.

yes, It's missing while writing to file and i tried with more memory allocation but no useful. When i increase allocation size, it's dispalaying "0" and some garbage values instead of expected values, so could you please advise how to resolve this.

Thanks & Regards,
Balu

getline(inFile, tempLine)

In a binary file there is no such thing as a line. Are you sure the reading part is correct?
Try using functions fread() instead.

char buff[50];
fread(buff,1,50,inFile);

I am sorry, I did not mentioned what i am doing with above code.

1. reading text file and calculating block size sepated with delimiters(">") from text file.
2.Writing all block sizes(values) to another file in binary mode to access a particular block for some purpose at later stage.

I hope you understand what i am doing and please advise.

Thanks & Regards,
Balu

getline(inFile, tempLine)

In a binary file there is no such thing as a line. Are you sure the reading part is correct?
Try using functions fread() instead.

char buff[50];
fread(buff,1,50,inFile);

fread() is the C++ function???, It's C standard library function.


Regards,
Balu

reading text file

No you are not, you are reading a binary file now, as you opened it in binary mode.

fread() is the C++ function???, It's C standard library function.

Yes it is a cstio/stdio function but C++ has inherited many things from C, fread just being one of them. If you still want to use fstream objects, there is fstream.read() option available too.

yes, first writing to the file and then reading the same file in binary mode, which is written previously.

Thanks & Regards,
Balu

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.