Recent

Author Topic: How to call from a .txt file into an array record?  (Read 19063 times)

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #30 on: March 04, 2013, 02:13:35 pm »
I have fixed it, i had some arrays that were [1..14] and i was using a count for 1 to 20. i set all arrayS to [1..20] AND NO MORE SIGSEGV!

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How to call from a .txt file into an array record?
« Reply #31 on: March 04, 2013, 03:45:29 pm »
Seems this goes to show the usefulness of posting an entire test project (see link in signature).
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Peter_Vadasz

  • New Member
  • *
  • Posts: 35
Re: How to call from a .txt file into an array record?
« Reply #32 on: March 04, 2013, 09:33:09 pm »
This works
__________
Code: [Select]
program pro;
uses crt,sysutils;
type
rec=record
Surname,name:string;
Date:string;
az:integer;
end;
Var
  A:array[1..14] of rec;
  B:Array[1..16] of rec ;
  C:array[1..17] of rec  ;
  D:array[1..19] of rec   ;
  E:array[1..20] of rec    ;
  F:array[1..20] of rec     ;
  G:array[1..19] of rec      ;
  H:array[1..19] of rec       ;
  I:array[1..19] of rec        ;
  J:array[1..19] of rec         ;
  K:array[1..15] of rec          ;


  Tem,can:char;
  Tem1,Tem3,tem4,tem5,tem6,tem7:string;
  Tem2,Rem:integer;
  Bookin : Text;
 begin;

   Assign(bookin,'C:\Users\davenport\My Documents\bookin.TXT');
   try
   reset(bookin);

   while not Eof(bookin) do
   begin
   Readln(Bookin, Tem);



   If (tem='A') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   A[Tem2].surname:=tem4;
   A[Tem2].name:=tem5;
   A[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   A[Tem2].az:=1;
   end;

   end;


   If (tem='B') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   B[Tem2].surname:=tem4;
   B[Tem2].name:=tem5;
   B[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   B[Tem2].az:=1;
   end;

   end;

   If (tem='C') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   C[Tem2].surname:=tem4;
   C[Tem2].name:=tem5;
   C[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   C[Tem2].az:=1;
   end;

   end;
  If (tem='C') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   C[Tem2].surname:=tem4;
   C[Tem2].name:=tem5;
   C[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   C[Tem2].az:=1;
   end;

   end;
  If (tem='D') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   D[Tem2].surname:=tem4;
   D [Tem2].name:=tem5;
   D[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   D[Tem2].az:=1;
   end;

   end;
  If (tem='E') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   E[Tem2].surname:=tem4;
   E[Tem2].name:=tem5;
   E[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   E[Tem2].az:=1;
   end;

   end;
  If (tem='F') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   F[Tem2].surname:=tem4;
   F[Tem2].name:=tem5;
   F[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   F[Tem2].az:=1;
   end;

   end;
  If (tem='G') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   G[Tem2].surname:=tem4;
   G[Tem2].name:=tem5;
   G[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   G[Tem2].az:=1;
   end;

   end;
  If (tem='G') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   G[Tem2].surname:=tem4;
   G[Tem2].name:=tem5;
   G[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   G[Tem2].az:=1;
   end;

   end;
  If (tem='H') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   H[Tem2].surname:=tem4;
   H[Tem2].name:=tem5;
   H[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   H[Tem2].az:=1;
   end;

   end;
   If (tem='I') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   I[Tem2].surname:=tem4;
   I[Tem2].name:=tem5;
   I[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   I[Tem2].az:=1;
   end;

   end;
   If (tem='J') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   J[Tem2].surname:=tem4;
   J[Tem2].name:=tem5;
   J[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   J[Tem2].az:=1;
   end;

   end;
   If (tem='K') then begin
   Readln(bookin, tem2);
   readln(bookin, tem3);
   Readln(bookin, tem4);
   Readln(bookin, tem5);
   Readln(bookin, tem6);
   K[Tem2].surname:=tem4;
   K[Tem2].name:=tem5;
   K[Tem2].Date:=tem6;
   If (tem3='BOOKED') then begin
   K[Tem2].az:=1;
   end;

   end;

   end;

   Writeln(A[2].surname);
   Writeln(B[2].surname);
   Writeln(C[2].surname);
   Readln();

except;
end;

end.

And this works too, but it is a little bit shorter.

Code: [Select]
program pro;

{$mode objfpc}{$H+}

uses
  crt, sysutils;

type
  rec=record
    Surname,name:string;
    Date:string;
    az:integer;
  end;
 
Var
  A:array[1..14] of rec;
  B:Array[1..16] of rec;
  C:array[1..17] of rec;
  D:array[1..19] of rec;
  E:array[1..20] of rec;
  F:array[1..20] of rec;
  G:array[1..19] of rec;
  H:array[1..19] of rec;
  I:array[1..19] of rec;
  J:array[1..19] of rec;
  K:array[1..15] of rec;

  Tem : char;
  Tem2 : integer;
  Bookin : Text;

function GetData(var aFVar:Text):rec;
var
  tmp: string;
begin
   Readln(aFVar, tmp);
   if tmp='BOOKED' then Result.az:=1;
   Readln(aFVar, Result.Surname);
   Readln(aFVar, Result.Name);
   Readln(aFVar, Result.Date);
end;
 
begin
   Assign(Bookin,'bookin.txt');
   Reset(Bookin);
   while not EOF(Bookin) do
   begin
     Readln(Bookin, Tem);
     Readln(Bookin, Tem2);
     case Tem of
       'A': A[Tem2]:=GetData(Bookin);
       'B': B[Tem2]:=GetData(Bookin);
       'C': C[Tem2]:=GetData(Bookin);
       'D': D[Tem2]:=GetData(Bookin);
       'E': E[Tem2]:=GetData(Bookin);
       'F': F[Tem2]:=GetData(Bookin);
       'G': G[Tem2]:=GetData(Bookin);
       'H': H[Tem2]:=GetData(Bookin);
       'I': I[Tem2]:=GetData(Bookin);
       'J': J[Tem2]:=GetData(Bookin);
       'K': K[Tem2]:=GetData(Bookin);
     end;
  end;
  Writeln(A[2].surname);
  Writeln(B[2].surname);
  Writeln(C[2].surname);
  Readln();
end.
OS: Ubuntu 12.04.2 32 bit
Lazarus: 1.0.8
FPC: 2.6.2

lainz

  • Guest
Re: How to call from a .txt file into an array record?
« Reply #33 on: March 04, 2013, 10:54:41 pm »
You can change the input or that file is created with another program?
If you can change it I recommend you to use something like CSV, INI or XML...

CSV:
Code: [Select]
var
  myfile: TStringList;
  temp: TStringList;
  i: integer;
begin
  myfile := TStringList.Create;
  myfile.LoadFromFile('file.txt');

  for i:= 0 to myfile.Count - 1 do
  begin
    temp := TStringList.Create;
    temp.CommaText := myfile[i];
    //temp[n]
    //0,1,2,     3,     4,   5
    //A,2,BOOKED,Parrot,Ryan,21/07/1995

    temp.Free;
  end;

  myfile.Free;

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #34 on: March 06, 2013, 03:54:33 pm »
I have no idea how to use filepos within this code. here is what i have.
Code: [Select]
Readln(CH); {Character}
count:=0;
while not Eof(bookin) do begin
     
     Readln(Bookin, Tem);
     If (tem=CH) then begin
     Readln(bookin, tem2);
     readln(bookin, tem3);
     Readln(bookin, tem4);
     Readln(bookin, tem5);
     Readln(bookin, tem6);
     Writeln(filepos(bookin));
I just want to find the file position, -6 from it and then repeat this code but with writeln('') instead of readlns. This will leave blank lines.

ps is there anyway to delete lines e.g if there are 9 lines of data 3 for each record, then the 3 in the middle become blank there are 9 lines but only 2 records.
333
000
333
if i wanted to get rid of the blank lines (0's) how would i do this.
222
222
.

Peter_Vadasz

  • New Member
  • *
  • Posts: 35
Re: How to call from a .txt file into an array record?
« Reply #35 on: March 06, 2013, 05:35:35 pm »
What kind of file do you use? Text or typed (binary) file? If you use text file, the FilePos not working. I suggest you use filestream or stringlist.
OS: Ubuntu 12.04.2 32 bit
Lazarus: 1.0.8
FPC: 2.6.2

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #36 on: March 06, 2013, 10:30:26 pm »
I'm using a txt file. could you give me some example code.

Cheers bud

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to call from a .txt file into an array record?
« Reply #37 on: March 06, 2013, 11:17:09 pm »
just to make things clear using writeln in a text file on a position other than the end of file it will not insert an empty line it will replace two existing characters with the character pair CR/LF. In order to insert data you need to move all the file data at that position and to the end two characters down and then insert.

There is no magic bullet to clean up a file like this you either read the data and discard them as invalid or open two files and copy line by line the data to the new file discarding the invalid lines then you simple delete the old and renaming the new to take its place.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How to call from a .txt file into an array record?
« Reply #38 on: March 07, 2013, 05:22:30 am »
Simplest thing you can do, is not trying to insert. When you save anything to file, you would write the whole file. I'm sure you have all its data loaded in memory anyway, or you could have. If you would go and use TStringList, TIniFile etc, they would do the same, write the whole file.

If you want to truly insert data in between, you need to use binary file. That's done with normal AssignFile(), or TFileStream class. This TFileStream is newer and much better if you want to dig into it, but since we started with AssignFile(), i'll show example. Firstly there cannot be dynamic strings in records, but you have to give them maximum length.
Code: [Select]
rec=record
  Surname, name: string[32];
  Date: string[10]; // 10.10.2010 contains 10 characters...
  az: integer;
end;

A: array of rec;

procedure SaveFile(filename: string);
var i: integer;
    F: file of rec;
begin
  assignfile(F, filename);
  rewrite(F);
  for i:=low(A) to high(A) do
    write(F, A[i]);
  closefile(F);
end;

procedure LoadFile(filename: string);
var index: integer;
    F: file of rec;
begin
  assignfile(F, filename);
  reset(F);
  index:=0;
  while not EOF(F) do begin
    setlength(A, index+1);
    read(F, A[index]);
    inc(index);
  end;
  closefile(F);
end;
Some guide for TFileStream too is here: http://www.angelfire.com/hi5/delphizeus/customfiles.html
With it you could write also the array length as integer to beginning of file, so you don't need to do the slow setlength() per each record. But under 1000 indices that "slow" is hardly noticable.

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #39 on: March 07, 2013, 11:21:23 am »
What is love?

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #40 on: March 07, 2013, 11:23:13 am »
Baby, don't hurt me.

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #41 on: March 07, 2013, 11:23:46 am »
Don't hurt me, no more.

Afrooman108

  • New Member
  • *
  • Posts: 26
Re: How to call from a .txt file into an array record?
« Reply #42 on: March 14, 2013, 11:21:09 am »
I HAVE HAD ENOUGH< FIGHT ME IRL FGT!

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How to call from a .txt file into an array record?
« Reply #43 on: March 14, 2013, 11:44:17 am »
I HAVE HAD ENOUGH< FIGHT ME IRL FGT!
What is your problem now? If you wouldn't understand help that people have given you, i assume you would say so. Or is that simply trolling?

 

TinyPortal © 2005-2018