🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.os.msdos.programmer
2 messages
2 total messages Started by Utz-Uwe Haus Wed, 20 Jan 1993 18:53
Random access files under TC++???
#7646
Author: Utz-Uwe Haus
Date: Wed, 20 Jan 1993 18:53
22 lines
1574 bytes
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.

If there is some general error in my concept - (or some fine c++-specific I
missed ) *please* let me know. 
If there is some code available for automatic RandomAccessFiles - that would be
even better (sorry - I can't ftp, but mail is okay)!!
P.s. My last idea would be to copy all preceding records to a new file, append
the new one and copy the rest of the original file and then rename to the 
original, but thats pretty disk-intensive, right :->>>?

Utz-Uwe Haus
root%pse@chamber.in-berlin.de
Re: Random access files under TC++???
#7714
Author: Michael Spencer
Date: Sun, 24 Jan 1993 18:24
160 lines
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
===========================================================================
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads