mardi 4 décembre 2018

How it works ??? Strange behavior of fstream

Why "fin >> baff" changes "res" ???

char res[10000], buff[9];
int end, pos = 0;
ifstream fin( "input.txt");
fin.seekg( 0, ios::end);
end = (fin.tellg() / 8) - ( (fin.tellg() % 8 == 0) ? 0 : 1);
refresh( fin );
while( pos < end ){
    fin.seekg( pos*8, ios::beg);
    // this strange fin change my array res
    fin >> buff;
    refresh( fin );
    if ( buff[3] == '1') {
        if( buff[7] == '1') {
            res[pos++] = '1';
        } else if ( buff[7] == '0') {
            res[pos++] = '0';
        } else
            cout << "Error1" << endl;
    } else if ( buff[3] == '0') {
        res[pos++] = ' ';
    } else
        cout << "Error2";
    buff[8] = '\0';
    cout << buff << " " << pos << endl;
}
cout << "End of game" << endl;
ofstream fout("result.txt");
for ( int i = 0; i < pos; ++i)
    fout << res[i];
for ( int i = 0; i < pos; ++i)
    cout << res[i];

When i debug this code, on 3 iterations "fin >> buf" change my array "res", it's wery weird. Sorry for my English.

Aucun commentaire:

Enregistrer un commentaire