phat code I hate when they fix a bug that I use.
Main

Projects

Downloads

Articles

Links

Forum

 

View Message

Back to Messages
Peter Swinkels Thu Oct 24 2024 at 1:09 pm
Okay I figured it out.
 
 
Code with Clock as a local variable that works:


/*Does not work in DOSBox without booting into actual MS-DOS.*/

#define TRUE 1
#include <conio.h>
#include <dos.h>
#include <stdio.h>

struct ClockStr {
 unsigned short Days;
 unsigned char Minutes;
 unsigned char Hours;
 unsigned char Seconds100ths;
 unsigned char Seconds;
};

void HideCursor ();
void ReadClock ();

int main() {
 struct ClockStr Clock;

 clrscr();
 HideCursor();

 while (TRUE) {
  if (kbhit()) {
   if (getch() == 27) break;
  }

  ReadClock(&Clock);
  gotoxy(1, 1);
  printf(" Days since 01-jan-1980: %hun", Clock.Days);
  printf("                  Hours: %hhun", Clock.Hours);
  printf("                Minutes: %hhun", Clock.Minutes);
  printf("                Seconds: %hhun", Clock.Seconds);
  printf("   Hundreths of seconds: %hhun", Clock.Seconds100ths);
 }
}

void HideCursor() {
 union REGS regs;
 regs.h.ah = 0x01;
 regs.h.ch = 0x20;
 regs.h.cl = 0x00;
 int86(0x10, &regs, &regs);
}

void ReadClock (struct ClockStr *Clock) {
 FILE *file = fopen("CLOCK$", "rb");
 if (file != NULL) {
  fread(Clock, 1, 6, file);
  fclose(file);
 }
}

The issue was with the parameters indicating the amount of data to be read. SizeOf(Clock) or SizeOf(&Clock) would be interpreted as the size of the pointer, not the variable itself. Trying to use SizeOf(ClockStr) which ought to be valid imho doesn't work. I rarely do much with C so its subtleties can easily get to me. :-)
 
 
 
 

Reply to this Message

Name
Subject
Message

No HTML is allowed, except for <code> <b> <i> <u> in the message only.
All URLs and email addresses will automatically be converted to hyperlinks.