Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with writing number 10 in binary file

Status
Not open for further replies.

zgoran

Programmer
Mar 23, 2003
4
HR
Hello everyone!

Well, I have a starnge problem.

Here is the code, very simple:


#include <iostream.h>
#include <fstream.h>

typedef short int type;

int main () {

fstream file;

file.open(&quot;file.bpd&quot;, ios::in | ios::eek:ut);

type wr, rd;

cout << &quot;writing...&quot; << endl;

for (int i = 0; i < 3; i++) {

cout << &quot;wr: &quot;;
cin >> wr;

file.seekp(0, ios::end);
file.write((char *)&wr, sizeof(type));

}

cout << &quot;reading....&quot; << endl;

for (int i = 0; i < 3; i++) {

int poz = i*sizeof(type);
file.seekg(poz, ios::beg);
file.read((char *)&rd, sizeof(type));

cout << &quot;rd: &quot; << rd << endl;
}

file.close();
}


Problem occures when I try to write number 10 in file. After that everything in &quot;file.bpd&quot; gets corrupted.

Has anyone have some idea what is causing the problem?

Regards, Goran!
 
Try the following. I get strange results on the offsets. They read 0, 3, 5. Would have expected 0, 2, 4
Code:
#include <iostream>
#include <fstream>
using namespace std;

typedef short int type;

int main () {
    
    ofstream ofile;
    int i;
    ofile.open(&quot;file.bpd&quot;);

    type wr, rd;

    cout << &quot;writing...&quot; << endl;

    for (i = 0; i < 3; i++) {    

        cout << ofile.tellp() << &quot;wr: &quot;;
        cin >> wr;
    
        //int poz = i*sizeof(type);
        //ofile.seekp(poz, ios::beg);
        ofile.write((char *)&wr, sizeof(type));

    }
    ofile.close ();

    ifstream ifile;
    ifile.open (&quot;file.bpd&quot;);

    cout << &quot;reading....&quot; << endl;

    for (i = 0; i < 3; i++) {
        
        //int poz = i*sizeof(type);
        //ifile.seekg(poz, ios::beg);
        cout << ifile.tellg();
        ifile.read((char *)&rd, sizeof(type));
    
        cout << &quot;rd: &quot; << rd << endl;
    }    

    ifile.close();

    return 0;
}
 
Well I tried the code under three compilers.
Borland C++ 5.5.1 for win32 gives offsets 0,3,5 and wrong reading of number 10 from file. The similar behaviour I noticed for Gnu gcc 3.2. under Cygwin wiht offsets 0,2,5
BUT gcc 2.95.4 (debian prerelease) is workin fine, normal, like you expect it to work.
I don't know if it is bug in compiler or in library if we are talking about bug at all? What do I qnow. I am just a begginer in all this!

Goran!




 
I don't normally use C++ for binary I/O - I normally stick to C. With C you know you're safe and it doesn't exhibit weird behaviour. Use fseek in place of seekg and seekp.
Code:
#include <iostream>
#include <cstdio>
using namespace std;

typedef short int type;

int main () {
    
    FILE* ofile;
    int i;
    ofile = fopen(&quot;file.bpd&quot;, &quot;wb&quot;);

    type wr, rd;

    cout << &quot;writing...&quot; << endl;

    for (i = 0; i < 3; i++) {    

        cout << ftell(ofile) << &quot;wr: &quot;;
        cin >> wr;
    
        //int poz = i*sizeof(type);
        //ofile.seekp(poz, ios::beg);
        fwrite((char *)&wr, sizeof(type), 1, ofile);

    }
    fclose (ofile);

    FILE* ifile;
    ifile = fopen (&quot;file.bpd&quot;, &quot;rb&quot;);

    cout << &quot;reading....&quot; << endl;

    for (i = 0; i < 3; i++) {
        
        //int poz = i*sizeof(type);
        //ifile.seekg(poz, ios::beg);
        cout << ftell(ifile);
        fread((char *)&rd, sizeof(type), 1, ifile);
    
        cout << &quot;rd: &quot; << rd << endl;
    }    

    fclose(ifile);

    return 0;
}
 
>> With C you know you're safe and it doesn't
>> exhibit weird behaviour.

I don't want to be offensive but i just can't let that go without saying that it's a load of hogwash. There is no such thing as &quot;weird behaviour&quot; nor even &quot;weird behavour&quot; [lol]

fstream.write() does exactly what you tell it to do, period.

>> everything in &quot;file.bpd&quot; gets corrupted.

There is no useful information in that statement. You need to post specific information to obtain any meaningful help.


-pete
I just can't seem to get back my IntelliSense
 
The weird behaviour was because it was opened as a text file, which is the default. Still not sure why it dumped binary 10 as 5130. Make the following changes and it will work as expected.

Code:
   ofile.open(&quot;file.bpd&quot;, ios::binary);
   ifile.open (&quot;file.bpd&quot;, ios::binary);
 
5130 = 140A Hex Looks like it grouped the 10 and 20 together.

The second tellp() prints 3 because 10 is a line feed. Windows automatically adds a carriage return before a linefeed.
 
Hello!

Well, this is what I ment with &quot; everything in file.bpd gets corrupted&quot;.

I gave above (my first post) the code that I was using to write numbers in binary file.
So everything works fine untill I get to number 10. After number 10 readings gets wrong.
Example 1. I wrote 10,20,30 and read 10,10,5120.
Example 2. I wrote 20,10,30 and read 20,2560,10.
Example 3. I wrote 20,30,10 and read 20,7680,30.
Example 4. I extend the code for 5 numbers and wrote 20,30,40,50,10 and the read nubers were 20,7680,30,10240,40.
(I wonted to know that, if the nuber 10 is at the end of a file, does it affect the reading of numbers before)
There was also observation of readings of fstream.teelp() that comes whit number 10.
I used compiler gcc 3.2 under Cygwin.

Trully there is no such things as &quot;weird behaviour&quot; there is only correct code or code with error. So there is explanation for this.

One another thing.
If I wrote just 1 number in binary file (examp. number 20), would it be that the binary file looks like binary notation of number 20, without no other bits in front of or after the bits of 20?

Thanks for everything!

Goran
 
It works now!

Above code (form my firs post) is incomplete.
Fstream in definition should have aditional token &quot;ios::binary&quot; in order to work as expected.
Thanks xwb for suggestion!

Thank you all for help!

Goran!



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top