Recent

Author Topic: How do i read from a file then display it to a user?  (Read 12907 times)

captian jaster

  • Guest
How do i read from a file then display it to a user?
« on: February 18, 2010, 06:21:44 pm »
Code: [Select]
BEGIN
     writeln('Getting Data....');
     assign(file1, 'C:\hydraks\information\info.txt');
     reset(file1);
     delay(2000);
     ClrScr;
     writeln(file1);
     writeln('Press enter to go back');
     readln;
     Goto 1;
   END;

When the program starts it just closes
what am i doing wrong? Im trying to get it to read from the file then display the file contents

mmab

  • Jr. Member
  • **
  • Posts: 89
Re: How do i read from a file then display it to a user?
« Reply #1 on: February 18, 2010, 06:47:10 pm »
Code: [Select]
BEGIN
     writeln('Getting Data....');
     assign(file1, 'C:\hydraks\information\info.txt');
     reset(file1);
     delay(2000);
     ClrScr;
     writeln(file1);
     writeln('Press enter to go back');
     readln;
     Goto 1;
   END;

When the program starts it just closes
what am i doing wrong? Im trying to get it to read from the file then display the file contents

Code: [Select]
var F:textfile;
     s:string;
begin
if fileexists('C:\hydraks\information\info.txt') then begin
assignfile(F,'C:\hydraks\information\info.txt');
reset(F);
repeat
  s:=readln(F);
   writeln(s);
until eof(F);
closefile(F);
end
else writeln('File not found');
end;

captian jaster

  • Guest
Re: How do i read from a file then display it to a user?
« Reply #2 on: February 18, 2010, 07:16:00 pm »
I ran your code and got some errors that confused me.
I edited it and got some anistring type error code
Code: [Select]
  BEGIN
   if fileexists('C:\hydraks\information\info.txt') then
   BEGIN
     assign(file1, 'C:\hydraks\information/info.txt');
     reset(file1);
     repeat
     s := readln(file1);
     writeln(s)
     until EoF(file1);
     close(file1);
     writeln('Press enter to go back');
     readln;
     Goto 1;
   END
    Else
     BEGIN
       writeln('ERROR');
       writeln(ERROR2);
       writeln('Press enter to go back');
       readln;
       Goto 1;
     END;

   END;

Zoran

  • Hero Member
  • *****
  • Posts: 1980
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: How do i read from a file then display it to a user?
« Reply #3 on: February 19, 2010, 09:07:27 am »
   assign(file1, 'C:\hydraks\information/info.txt');

I don't know if it is the only thing, but I can see that you put one slash instead of backslash (.../info.txt - ... \info.txt).
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Bart

  • Hero Member
  • *****
  • Posts: 5667
    • Bart en Mariska's Webstek
Re: How do i read from a file then display it to a user?
« Reply #4 on: February 19, 2010, 12:05:49 pm »
This looked odd to me:
Code: [Select]
  s := readln(file1);

Try
Code: [Select]
  readln(File1,s);

(The forwardslash in the filename should not be a problem (at least in the old DOS days all BIOS and DOS functions accepted both \ and / internally as separator))

And please, pretty please, do not use Goto ;-)

Bart

captian jaster

  • Guest
Re: How do i read from a file then display it to a user?
« Reply #5 on: February 19, 2010, 05:07:21 pm »
Code: [Select]
if fileexists('C:\hydraks\information\info.txt') then
       BEGIN
       writeln('Getting Data.....');
       assign(file1, 'C:\hydraks\information\info.txt');
       reset(file1);
       repeat
       readln(file1,s);
       writeln(s);
       until EoF(file1);
       close(file1);
       writeln('Press enter to go back');
       Readln;
       Goto 1;
same problem
the window close everytime..
Why shouldnt i use goto

LazaruX

  • Hero Member
  • *****
  • Posts: 597
  • Lazarus original cheetah.The cheetah doesn't cheat
Re: How do i read from a file then display it to a user?
« Reply #6 on: February 19, 2010, 05:24:14 pm »
GOTO is an old style of programming, and should be avoided if possible, instead use otherconsturctions like procedures or functions.
Remember in FreePascal the old Turbo Pascal way of handling with files have been replaced so:
Assign now is AssignFile
Close is CloseFile

What for type is file1? Try by using file1:text;

Try this:
Code: [Select]
var
f:text;
ch:char;

begin
 assignfile(f,'test.txt');
 reset(f);
 while not eof(f) do
  begin
   read(f,ch);
   write(ch);
  end;
 closefile(f);
 readln;
end.
I leave it to you to write the exceptions, and so on....
« Last Edit: February 19, 2010, 08:28:10 pm by BPsoftware »

LazaruX

  • Hero Member
  • *****
  • Posts: 597
  • Lazarus original cheetah.The cheetah doesn't cheat

captian jaster

  • Guest
Re: How do i read from a file then display it to a user?
« Reply #8 on: February 19, 2010, 05:42:46 pm »
identifirier not found 'Closefile'  :-\

LazaruX

  • Hero Member
  • *****
  • Posts: 597
  • Lazarus original cheetah.The cheetah doesn't cheat
Re: How do i read from a file then display it to a user?
« Reply #9 on: February 19, 2010, 08:27:31 pm »
This is my code I cannot help you more, for me it works:

Code: [Select]
program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

var
f:text;
ch:char;

begin
 assignfile(f,'test.txt');
 reset(f);
 while not eof(f) do
  begin
   read(f,ch);
   write(ch);
  end;
 closefile(f);
 readln;
end.

captian jaster

  • Guest
Re: How do i read from a file then display it to a user?
« Reply #10 on: February 20, 2010, 02:05:40 pm »
Ill try that later and thnx
but why doesnt the closefile code work for me?

Bart

  • Hero Member
  • *****
  • Posts: 5667
    • Bart en Mariska's Webstek
Re: How do i read from a file then display it to a user?
« Reply #11 on: February 20, 2010, 03:03:26 pm »
but why doesnt the closefile code work for me?

You need fpc to be in ObjFpc (or Delphi) mode.

Code: [Select]
program test;

{$mode objfpc} // <-- this puts the compiler into ObjFpc mode

var
  f: TextFile;
  S: String;

begin
  assignfile(f,'test.txt');
  reset(f);
  while not eof(f) do
  begin
    readln(f,S);
    writeln(S);
   end;
  closefile(f);
  write('Press <Enter> to close ... ');
  readln;
end.

Bart
« Last Edit: February 20, 2010, 03:21:22 pm by Bart »

captian jaster

  • Guest
Re: How do i read from a file then display it to a user?
« Reply #12 on: February 20, 2010, 03:41:43 pm »
Your code worked but i still didnt have to use closefile
just close.
and i didnt have to use cthreads either

Bart

  • Hero Member
  • *****
  • Posts: 5667
    • Bart en Mariska's Webstek
Re: How do i read from a file then display it to a user?
« Reply #13 on: February 21, 2010, 12:24:16 am »
It is perfectly OK to use Assign() and Close() when performing file operations.

AssignFile() and CloseFile() were introduced in Delphi as replacements for Assign() and Close(), because they (the Borland guys) introduced new procedures with the same name and very different meaning.

So when you start programming in Lazarus or Delphi, it is common to use AssignFile() and CloseFile() instead.

(Alternatively one could uses system.assign() and system.close() too)

And no, you do not need cthreads  :)

Bart

 

TinyPortal © 2005-2018