Jump to content

code loading - Please help!

- - - - -

  • Please log in to reply
4 replies to this topic

#1
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hi guys...Im a newbie in pascal..Im wondering how to load a code from an external file... I using this one wich is not working:

f: text;

s: string;



Assign(f,'level1.lvl');

 Reset(f);

    while not eof(f) do

      Readln(f,s);

      writeln(s);

      close(f);

My problem is that its not recognising the code as commands...Its showing the commands just like a text on the screen...I also tried with a byte type,but had a bad result too...

Please help me to make this to be recognised as a code,not just as a text!

THANK YOU!!!!

#2
Zorfox

Zorfox

    Learning Programmer

  • Members
  • PipPipPip
  • 36 posts

alex1 said:

My problem is that its not recognising the code as commands...Its showing the commands just like a text on the screen...I also tried with a byte type,but had a bad result too...

Please help me to make this to be recognised as a code,not just as a text!

THANK YOU!!!!

"Code" has to be compiled by a pascal compiler. What your code is doing is assigning lines from the file to a variable of type string. What you want to do is sort of like asking the application to load the string "Show a message saying hello" from a file and expect the "computer" to do it. You need an interpreter, the compiler, to explain that statement.

Example, if you have a file with two lines,
writeln('Line One');
writeln('Line Two');
You cant load that file and expect to see two lines written to the screen. You can however do something like,

Readln(f,s)
if S := writeln('Line Two') then
writeln('Line Two') else
writeln('Line One');

What is it your trying to do exactly and maybe someone can help?

#3
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Actually the thing i would like to do is to execute a code wich is written on a text file.
For example:
I have the following code on a text file and I would like to execute it:

gotoxy(10,25);

write('hello');

After the file loading it should go to 10,25 and to write hello... But I don't know why the compiler is not recognising it as commands... Its seeing it just like a string line,so after the file is loaded its writing on the screen:
gotoxy(10,25);write('hello');

How can I make it to be recognised as a command and to do whatever the code is saying?

THANKS A LOT FOR ANY HELP!!!! :)

#4
Zorfox

Zorfox

    Learning Programmer

  • Members
  • PipPipPip
  • 36 posts
Apparently you misunderstood my response. The lines of code in a file cannot be executed without being compiled.

There are command line compilers to compile code, which are actually projects, but that would not allow for code blocks to be used from within your application.

alex1 said:

... Its seeing it just like a string line,so after the file is loaded its writing on the screen:
gotoxy(10,25);write('hello');

That's because it IS a string! gotoxy and write are commands that direct a COMPILER what to do NOT the operating system. These commands are not like .bat file commands back in the day.

alex1 said:

How can I make it to be recognised as a command and to do whatever the code is saying?

This is why I asked exactly what you are doing. You cannot use the string from a file as commands. However, you can "simulate" a compiler from your application. Basically look to see if a command is in the string from the file. If there is a command, get the information from it and execute the command.

An example of the two lines you wanted to execute...

var

f: text;

s, SubStr1: string;

i,x,y: Integer;


begin

Assign(f,'level1.lvl');

 Reset(f);

    while not eof(f) do

      begin

        Readln(f,s);

        {Check if this line is a gotoxy command. If it is then execute the command}

          if Pos('gotoxy',S) <> 0 then

            begin

              i := Pos('(', S);

              SubStr1 := Copy(S, i+1 , (Length(S) - i-2)); // Returns X,Y

              X := StrToInt(Copy(SubStr1,1,2));

              Y := StrToInt(Copy(SubStr1,4,2));

              gotoxy(X,Y);

            end else


         {Check this line to see if it is a write command. If it is execute the command}

          if Pos('write',S) <> 0 then

            begin

              i := Pos('(', S);

              SubStr1 := Copy(S, i+2 , (Length(S) - i) -4);

              write(SubStr1);

            end else

        write(s);

      end;

      close(f);

end.


#5
alex1

alex1

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Thanks Zorfox!!!

You are the best!!!! :cool:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users