I've never learn about pascal language and my teacher had some homework about cache working and he told me write it in Pascal.
This is sample running.

Main menu

1.  Open fake disk
2.  Read block to file
3.  Write block from file
4.  Close fake disk
5.  Exit
Enter your choice:1
Enter fake disk name:myfakedisk.dat
Disk has 100 blocks of 512 bytes each

1.  Open fake disk
2.  Read block to file
3.  Write block from file
4.  Close fake disk
5.  Exit
Enter your choice:1
Enter fake disk name:/etc/shadow
Permission denied: cannot open '/etc/shadow'
Main menu

and i can code some code but i don't know "how to open file and count block" of it
( block is from how to create the fake disk on linux. For 13 blocks, try this:

N=13
dd if=/dev/zero of=fakedisk.dat bs=512 count=${N}  )

Here is my code

program testcache(output);
uses crt;
var oldest,fs,i,n,z,bnum : integer;
    map : array[1..z] of integer;
    fname :string;
    f : file of dat;
    mode : char;{'r','w','a' --- read write append}

  begin 
   clrscr;
   fs :=-1;
   writeln('Main menu');
   writeln;
   writeln('1. Open fake disk.');
   writeln('2. Read block to file.');
   writeln('3. Write block from file');
   writeln('4. Close fake disk');
   writeln('5. Exit');
   writeln('Enter your choice: ');
   readln(n);
       case n of
       1:writeln('Enter fake disk name: ');
             readln(fname);
             openf(f,fname,'r');
             RESET (f); 
            if IORESULT <> 0 THEN 
            WRITELN('No such file: cannot open ',fname) ;
            closef(f);
            else
            begin
    read(f);
    WRITELN('Disk has ',' blocks of ',' bytes each');
            end;
   2:writeln('Enter block number: ');
        readln(bnum);
   3:writeln('Enter block number: ');
        read(bnum);

   4:if 
     writeln('Flushing cache to fake disk...');
     writeln('Fake disk closed');
   5:writeln;
   else
      writeln('Invalid Choice');
end.

anyone please help me to complete my code T^T
thank you so much

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please use [code] [/code] tags.

It looks like you are off to a fairly good start, but you are missing a couple things.

Remember, in Pascal multiple statements must be enclosed in a begin..end block.

Also, make sure you get a good reference. It looks to me like you are using an old Borland Turbo Pascal, which doesn't have the openf procedure.

You must define the dat type before you can specify the file of dat type.

type
  dat: record
       block_count: longint;
       ... { however you have your file defined }
       end;
  DatFile: file of dat;
var
  f: DatFile;
  d: dat;
...

case n of
  1: begin
     writeln('Enter fake disk name: ');
     readln(fname);
     assign(f,fname);
     reset(f,sizeof(dat));
     if IOResult <> 0
       then writeln('No such file: cannot open ',fname)
       else begin
            read(f,d);
            writeln('Disk has ', d.block_count, ' blocks of ' ... );
            close(f)
            end;
  2: ...
  end;

I would also suggest you split your code up into procedures and functions.

type
  TUserChoice: (ucOpenDisk, ucReadBlock, ucWriteBlock, ucCloseDisk, ucExit);
var
  user_choice: TUserChoice;

procedure show_menu;
  begin
  writeln( 'Main Menu' );
  writeln;
  writeln( ucOpenDisk:2, '. Open fake disk' );
  writeln( ucReadBlock:2, '. Read block to file' );
  ...
  end;

procedure open_fake_disk;
  begin
  ...
  end;

...

begin
repeat
  show_menu;
  readln( user_choice );

  case user_choice of
    ucOpenDisk:   open_fake_disk;
    ucReadBlock:  read_block_to_file;
    ucWriteBlock: write_block_from_file;
    ucCloseDisk:  close_fake_disk;
    ucExit:
    else writeln( 'Invalid choice!' )
    end

until user_choice = ucExit;

writeln( 'Good-bye.' )
end.

Hope this helps get you going.

thank you for help me so much
but i'm still have problem
what is constant and selector - -*

35  case n of
 36      1 : writeln('Enter fake disk name: ');
 37           readln(fname);
 38           assign(f,fname);
 39           reset (f,sizeof(dat));

OS.PAS(36,26) Error: Constant and CASE types do not match
OS.PAS(36,26) Error: Constant Expression expected
OS.PAS(36,26) Fatal: Syntax error, ":" expected but ";" found
Fatal: Compilation aborted

thx a lot

It is complaining because readln is not a constant expression that matches the case type (n is an integer), and then it is complaining that it found a semicolon after the readln() instead of a colon, as case expressions should.

Remember, multiple statements must be enclosed in a begin..end block:

case n of
  1: begin
     writeln( 'Enter fake disk name: ' );
     readln( fname );
     assign( f, fname );
     ...
     end;
  2: ...
  end;

Fatal:Syntax error,;but CONST found

Is this meant as a reply to this code, or is it a new question you want answered?

This article has been dead for over six months. Start a new discussion instead.