Mohsin
Mohsin

Reputation: 533

A strange c++ code phenomenon

Look at these two programs:

#1:

int main(){
    ulong num = 100;
    float *pFArr = new float[num];
    if(!pFArr)return 0;

    for(ulong i=0; i<num; i++)
        pFArr[i] = 22.4f*(float)i - 11.4f;

    ofstream file("fArr.dat");
    if(!file){delete []pFArr; return 0;}
    if(!file.write((char*)&num,sizeof(num))){file.close(); delete []pFArr; return 0;}
    if(!file.write((char*)pFArr,num*sizeof(float))){file.close(); delete []pFArr; return 0;}
    file.close();

    delete []pFArr;
}


#2:

int main(){
    ulong num = 100;
    float *pFArr = new float[num];
    if(!pFArr)return 0;

    for(ulong i=0; i<num; i++)
        pFArr[i] = 53.2f*(float)i - 22.5f;


    ofstream file("fArr.dat");
    if(!file){delete []pFArr; return 0;}
    if(!file.write((char*)&num,sizeof(num))){file.close(); delete []pFArr; return 0;}
    if(!file.write((char*)pFArr,num*sizeof(float))){file.close(); delete []pFArr; return 0;}
    file.close();

    delete []pFArr;
}


The single code difference between the two is the assignment of the array elements. However, the first program saves a 404 bytes file (4 bytes for "num" and 100*4 bytes for the elements), while the second one saves a 405 bytes file!!

When I read the files using this code:

int main(){
    ulong num = 0;
    float *pFArr = null;

    ifstream file("fArr.dat");
    if(!file)return 0;

    if(!file.read((char*)&num,sizeof(num))){file.close(); return 0;}

    pFArr = new float[num];
    if(!pFArr){file.close(); return 0;}
    for(ulong i=0; i<num; i++)
        pFArr[i] = 0;
    if(!file.read((char*)pFArr,num*sizeof(float))){delete []pFArr; file.close(); return 0;}

    for(ulong i=0; i<num; i++)
        cout<<pFArr[i]<<"\n";

    delete []pFArr;
    file.close();
}


the 404 file is read successfully.
For the 405 file, the second "file.read" call fails. When I ignore the failure and continue displaying the array elements, I see that elements from 0 to 46 were assigned the right values, the "pFArr[47]" element has a strange value (x.xxxxxe-0xx), and the rest of the array were not assigned.

Can someone explain why this is happening??
Another issue: Avast! is always blocking the reading program, what's wrong with the code?

Thanks for any help

Upvotes: 0

Views: 170

Answers (2)

paxdiablo
paxdiablo

Reputation: 882806

There's a good chance that, because you're opening it in the default text mode, there are some translations going on under the covers. That gels with the fact that this appears to be a data-related problem.

For example, Windows may convert a \n byte to a \r\n sequence. A good idea would be to do a hex dump of the file to see if it has this sequence in it.

If it does, ensure you open it in binary mode instead:

ofstream file("fArr.dat", ios_base::out | ios_base::binary);

In fact, you probably want to do that anyway just to avoid potential problems in future (to all code that reads/writes binary information, such as your reader program as well).

Upvotes: 2

Praind
Praind

Reputation: 1581

I think when you read out the number, it isn't converted correctly into an unsigned long. Im pretty sure this is the problem because you have an issue at the array index 47 and 48 is in the ASCII table a 0. You should convert the chars into ulong.

Upvotes: 0

Related Questions