Article View: comp.os.msdos.programmer
Article #7714Re: Random access files under TC++???
From: Michael Spencer
Date: Sun, 24 Jan 1993 18:24
Date: Sun, 24 Jan 1993 18:24
160 lines
4925 bytes
4925 bytes
hausutzu@chamber.in-berlin.de (Utz-Uwe Haus) writes:
>Hello,
>I'm currently writing a small database for a friend's company and encountered
>the following problem:
>1) I save the records in a fstream file, build 2 indexes for easier search
>(everything okay so far)
>2) When I try to change the content of a record (read it, change the
>appropriate data and try to write it back, it gets appended to the end of the
>file, although I opened the file ios::out (I also tried ios::out|ios::app and a
>seekg to the required position and ios::out|ios::ate and seekg()). In addition
>to the wrong position for the rewrite the file sometimes is corrupted
>afterwards, making read-access for my build_index() - function impossible.
>Utz-Uwe Haus
>root%pse@chamber.in-berlin.de
Can't help you if you insist on using C++ io streams.
Generally I try to stay away from them, because they have proven
conceptually unstable in the past (Stroustrup keeps changing his
mind).
If you don't mind using old-fashioned C i/o, the following has been
used successfully in just the kind of application you are
describing.
===========================================================================
Michael Spencer | 2640 Borregas
spencer@cats.ucsc.edu | Aptos, CA 95003
===========================================================================
/* 04-16-1989 */
#define FILES_C
#include <stdio.h>
#include <fcntl.h>
#include <dir.h>
#include <cc_std.h> /* my basic #defines */
bool DataMake (int *fd, char *fname); /* pass fd as DM (&fd,..) */
bool DataOpen (int *fd, char *fname);
bool DataAppend (int *fd, char *fname);
void DataClose (int fd);
long DataFileSize (int fd, int recSize); /* num records in file */
bool DataGet (int fd, long recNum, void *buf, int recSize);
bool DataPut (int fd, long recNum, void *buf, int recSize);
/* ----------------------------------- DATA */
#define S_IREAD 256 /* read permission, owner */
#define S_IWRITE 128 /* write permission, owner */
#define S_RDWR (S_IREAD | S_IWRITE)
#define S_MAKE (O_CREAT | O_TRUNC | O_RDWR | O_BINARY)
#define S_OPEN (O_RDWR | O_BINARY)
#define S_APPEND (O_APPEND | O_WRONLY)
bool DataMake (int *fd, char *fname)
{
bool bad;
errno = 0;
*fd = open (fname, S_MAKE, S_RDWR);
bad = (*fd == -1);
if (bad) Errio (fname);
errno = 0;
return (! bad);
}
bool DataOpen (int *fd, char *fname)
{
bool bad;
errno = 0;
*fd = open (fname, S_OPEN);
bad = (*fd == -1);
if (bad) Errio (fname);
errno = 0;
return (! bad);
}
bool DataAppend (int *fd, char *fname)
{
bool bad;
errno = 0;
*fd = open (fname, S_APPEND);
bad = (*fd == -1);
if (bad) Errio (fname);
errno = 0;
return (! bad);
}
void DataClose (int fd)
{
close (fd);
errno = 0;
}
long DataFileSize (int fd, int recSize)
{
long size;
size = filelength (fd);
if (Errio (""))
size = 0L;
else size /= recSize;
return size;
}
bool DataGet (int fd, long recNum, void *buf, int recSize)
{
long kk;
kk = DataFileSize (fd, recSize) - 1;
if (Errata (! in (recNum, 0, kk), badRec)) return no;
lseek (fd, recNum * recSize, 0);
return (read (fd, buf, recSize) == recSize);
}
bool DataPut (int fd, long recNum, void *buf, int recSize)
{
long kk;
kk = DataFileSize (fd, recSize);
if (Errata (! in (recNum, 0, kk), badRec)) return no;
lseek (fd, recNum * recSize, 0);
return (write (fd, buf, recSize) == recSize);
}
--
===========================================================================
Michael Spencer | 2640 Borregas
spencer@cats.ucsc.edu | Aptos, CA 95003
===========================================================================
Technical Details
Message-ID:
<1jumsbINNft5@darkstar.UCSC.EDU>Path:
arc1-ca.pugleaf.net!sparky!uunet!ukma!usenet.ins.cwru.edu!agate!darkstar.UCSC.EDU!cats.ucsc.edu!spencerReferences:
<1993Jan20.185339.19456@chamber.in-berlin.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!ukma!usenet.ins.cwru.edu!agate!darkstar.UCSC.EDU!cats.ucsc.edu!spencer From: spencer@cats.ucsc.edu (Michael Spencer) Newsgroups: comp.os.msdos.programmer Subject: Re: Random access files under TC++??? Date: 24 Jan 1993 18:24:11 GMT Organization: University of California, Santa Cruz Lines: 161 Message-ID: <1jumsbINNft5@darkstar.UCSC.EDU> References: <1993Jan20.185339.19456@chamber.in-berlin.de> NNTP-Posting-Host: am.ucsc.edu