473,320 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Overwriting a portion of a binary file

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

Jan 26 '06 #1
3 7961
mo**************@gmail.com wrote:
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 :
We don't want a peek, we want a long, lingering look at the whole thing.

void add_record(record* rec)
{
// m_db is a FILE*
Where is the code that created this 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
There is no _write() function in the C language.
any help greatly appreciated


Post a complete, minimal, compilable program that demonstrates the
problem.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Jan 26 '06 #2
On 26 Jan 2006 15:17:13 -0800, in comp.lang.c ,
mo**************@gmail.com wrote:
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',
by default, fopen() opens a file for writing at the end. You'd need to
move elsewhere in the file if you want to overwrite.

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.


This is a FAQ - 19.14. The answer is that generally you can't do this
efficiently.

Assuming you only want to overwrite a record however, you MAY be able
to open the file for update, move to the start of the record you want
to change, then write some data. How you'd find that, is an exercise
for the reader, given that fseek() isn't guaranteed to work.

By the way, your code (reworked into C) works for me. I suspect one of
the parts you didn't post is causing a problem.

#include <stdio.h>

typedef struct record
{ int a; char b[4]; int d;
}record;

FILE* m_db;

int record_size = sizeof(struct record);

void add_record(record* rec)
{
// m_db is a FILE*
//fseek(m_db,0,SEEK_END);
fwrite(rec,record_size,1,m_db);
}
void reset_to_file_start()
{
rewind(m_db);
}
int main ()
{
record records1[] = { {0,"foo",10} , {1,"bob",4} };
record records2[] = { {0,"bar",7} , {1,"bob",3} };

m_db = fopen("d:/temp/test.txt","wb");
add_record(records1);
add_record(records1+1);

reset_to_file_start();

add_record(records2);
add_record(records2+1);
fclose(m_db);
}

bar
bob

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 27 '06 #3
thanks very very much for ur help,
actually my program was opening the file as "ab+" not as "wb", looke
like ab+ just "appends" and doesn't allow modifications. the replace
worked fine after opening it as "wb+", but wb+ erases the file every
time. so opening it as rb+ solved the problem except that it does not
create a new file if it doesn't exist... so i guess the `hybrid'
solution is to use rb+ and if the file doesn't exits use wb+.

here the minimal code after a lot of reductions to demonstrate the
problem as `default user' requested:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <io.h>

FILE* m_db;

typedef struct record
{
int pk;
char CustomerName[10];
int Age;
}record;

void assign_file(char* dbname);
void add_record(record* rec);

void main(void)
{
int i;
int record_count;

record records1[] = { {0,"foo",10} , {1,"bob",4} };
record records2[] = { {0,"bar",10} , {1,"bob",4} }; // bar shud
replace foo

assign_file("db1");

add_record(records1);
add_record(records1+1);

//rewind(m_db); // uncomment to test replacemnt

add_record(records2);
add_record(records2+1);

fflush(m_db);
fclose(m_db);
}

void assign_file(char* dbname)
{
char dbfilename[40] = { 0 };
strcat(dbfilename,dbname);
strcat(dbfilename,".txt");
m_db = fopen(dbfilename,"rb+");
fseek(m_db,0,SEEK_END);
rewind(m_db); // comment to test appending
}

void add_record(record* rec)
{
//fwrite(rec,sizeof(record),1,m_db);
_write(fileno(m_db),rec,sizeof(record));
}

Jan 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ellixis | last post by:
Hello, How can I use fwrite() and fseek() in order to write data in the middle (or anywhere else) of a file without overwriting existing data ? People told me that I should load the file into...
8
by: Stewart | last post by:
is there any way this can be done? I've looked at the help files and checked out as many tutorials as i could find on the net (as always) but no joy. thanks
15
by: Anand Ganesh | last post by:
HI All, I have an Image. I want to clip a portion of it and copy to another image. How to do this? I know the bounding rectangle to clip. Any suggestions please. Thanks for your time and...
9
by: SStory | last post by:
I use a bitmap class new bitmap(filepath) this should and does load my jpg into memory. I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save it; overwriting the...
5
by: Reddy | last post by:
System.Web.UI.HtmlControls.HtmlInputFile.SaveAs(FileName) is not overwriting the file. It used to work fine on IIS5.1 Recently we migrated to IIS6.0. Since then it's not working. If it's new file...
3
by: Kat | last post by:
Ok my problem with this program is that currently it isn't overwriting a dummy TXT file I have set up to test the program. At one point it did however so I know it should work but now, for some...
7
by: Rico | last post by:
Hello, I have a VB6 ActiveX DLL project that I use to connect to an SQL Server database. The front end that uses this DLL is written in Access XP / 2002. When I modify the ActiveX code, compile...
27
by: Jason | last post by:
Hi, I need to open an existing file, seek to a position at X number of bytes, and write out Y number of bytes overwriting any existing bytes, but no erasing any other data. Is this possible? ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.