M
mohammad.nabil.h
I've been working on a database management system, and i have met a
problem when i update a row, i found that fwrite() and _write, writes
to the end of the file, even if i hade rewind() the file before it. As
far as i can guess, they `append', i am looking for something that
overwrites. of course i can't load the 'whole' file in memory, change
it, then resave it again. the whole memory might not be enough.
my code always insists on storing 'bar' after 'foo'. i want to save
'bar' instead of 'foo'
here is a peek of my code :
main.cpp
//...
record records1[] = { {0,"foo",10} , {1,"bob",4} };
add_record(records1);
add_record(records1+1);
reset_to_file_start();
record records2[] = { {0,"bar",10} , {1,"bob",4} }; // 'bar' shud
instead of 'foo'
add_record(records2);
add_record(records2+1);
//..
void add_record(record* rec)
{
// m_db is a FILE*
//fseek(m_db,0,SEEK_END);
//fwrite(rec,record_size(),1,m_db);
_write(fileno(m_db),rec,record_size()); // both function result in the
same output
}
void reset_to_file_start()
{
rewind(m_db);
}
any help greatly appreciated
problem when i update a row, i found that fwrite() and _write, writes
to the end of the file, even if i hade rewind() the file before it. As
far as i can guess, they `append', i am looking for something that
overwrites. of course i can't load the 'whole' file in memory, change
it, then resave it again. the whole memory might not be enough.
my code always insists on storing 'bar' after 'foo'. i want to save
'bar' instead of 'foo'
here is a peek of my code :
main.cpp
//...
record records1[] = { {0,"foo",10} , {1,"bob",4} };
add_record(records1);
add_record(records1+1);
reset_to_file_start();
record records2[] = { {0,"bar",10} , {1,"bob",4} }; // 'bar' shud
instead of 'foo'
add_record(records2);
add_record(records2+1);
//..
void add_record(record* rec)
{
// m_db is a FILE*
//fseek(m_db,0,SEEK_END);
//fwrite(rec,record_size(),1,m_db);
_write(fileno(m_db),rec,record_size()); // both function result in the
same output
}
void reset_to_file_start()
{
rewind(m_db);
}
any help greatly appreciated