I've gone through that faq, but well changing the code to what you said doe nothing. It has the same effect as this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
cop=1;//DEV
while(database.good())//FIXME
{
database.read(buf, 21);
for(int i=0; i<7; i++)
{
if(cop==3)//DEV *2
buf[0]='k';
cout<<&buf[i*3];database.tellg();
}
cop++;//DEV
cout<<endl;
}
|
The problem here is that the eof is only set after reading file_size+1 bytes. And the last time database.read(buf, 21); it doesn't change the value of buf because it's already at file_size bytes and there's nothing else to read.
This is not my main problem as I can solve this with an if statement. The problem is that once it tries to read past eof the bad flag is raised.
Unenabling me to write to file after that happens, which I need to.
Here's what I need to do:
1.-input some info.
2.-read file to check if that info is already in it.
3.-if info is not on file, then write it to file.
Everything is running smothly except for the 3rd step, which fails because of the erro flag not letting me write to file.
Hope that helps..
btw: I was thinking I could first check file size and read only that number of bytes. But what's upsetting me is that this seems like a C++ implementation problem and not my fault...
EDIT: Heres the code if it helps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
void Scale::write_scale()
{
if(!open_file())
{
cout<<"Cannot write scale to disk.\n";
return;
}
char buf[22];
int cop;
while(database.good())//FIXME
{
cop=1;
database.read(buf, 21);
if(database.eof())
break;
for(int i=0; i<7; i++)
{
if(strcmp(&buf[i*3],&scl[i*3])!=0)
{
cop=0;
break;
}
}
if(cop==1)
{
cout<<"That scale is already in the database.\n";
return;
}
}
database.seekp(0, ios::end);
cout<<"tellp: "<<database.tellp()<<endl;
for(int i=0; i< scl_notes_n; i++)
{
database.write(&scl[i*3],3);
}
database.close();
}
|
cout<<"tellp: "<<database.tellp()<<endl; -> this outputs -1