TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: STEPHEN GAGE
from: KURT KUZBA
date: 1997-04-19 11:09:00
subject: Random access files

SG> char* FromName[36];
SG> char* ToName[36];
SG> char* Subject[72];
SG> int origzone;
SG> int destzone;
SG> etc...
SG>   How do I read the n'th chunk from the disk? I'm learning c++ at
SG>   Uni, but I'm only a first year, and we've only used the file
SG>   streams so far... I think I will probably need to use structs
SG>   as well, but I have no experience whatsoever
sg>......
   You can use fread() and fwrite() with structs.
Assume you have a struct, MSG_DAT;
   typedef struct {
      char* FromName[36];
      char* ToName[36];
      char* Subject[72];
      int origzone;
      int destzone;
   } MSG_DAT;
   MSG_DAT Msg;
   FILE *FILEpointer;
   if(access("message.dat", 0))
   {
      FILEpointer = fopen("message.dat","wb+");
   }
   else
      FILEpointer = fopen("message.dat", "rb+");
For writing to a fixed length record:
   long record_index = record_number * sizeof(MSG_DAT);
   fseek(FILEpointer, record_index, SEEK_SET);
   fwrite(&Msg, sizeof(MSG_DAT), 1, FILEpointer);
For reading from a fixed length record:
   long record_index = record_number * sizeof(MSG_DAT);
   fseek(FILEpointer, record_index, SEEK_SET);
   fread(&Msg, sizeof(MSG_DAT), 1, FILEpointer);
You should get a copy of SNIPPETS, really. :)
> ] Wow! This new machine has 64K. Now I can do ANYTHING........
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.