Recent

Author Topic: Text file to a Array  (Read 14463 times)

MuteClown

  • New Member
  • *
  • Posts: 33
Text file to a Array
« on: February 26, 2010, 02:53:44 am »
Sorry to be a bother, asking another noob thing  :-[
I am trying to pass the text file into an array then i want to do various things to it, thought i had it but got a EAccessViolation.

here is my code so far:
Code: [Select]
program pjct9;

{$mode objfpc}{$H+}

uses
    SysUtils;


var
   FileU : TextFile;
   FileAdr, FileCon : String;
   FileAr : Array of Char Absolute FileCon;
   i : Integer;



begin
Writeln('Please neter your text files adress(please right the full adress)');
readln(FileAdr);
IF NOT fileexists(FileAdr) then BEGIN Writeln('Error: FILE NOT FOUND') end;

AssignFile(FileU, FileAdr);
Reset(FileU);
i := 0;
While not EOF(FileU) do
      BEGIN
      Readln(FileU, FileCon);
      Writeln('Array ', i, ': ', FileAr[i]);
      inc(i);
      end;
CloseFile(FileU);
end.



cdbc

  • Hero Member
  • *****
  • Posts: 2671
    • http://www.cdbc.dk
Re: Text file to a Array
« Reply #1 on: February 26, 2010, 05:47:11 am »
Hi MuteClown
I haven't tested it, but this revised code should compile and run...

Code: [Select]
program pjct9;

{$mode objfpc}{$H+}

uses
    SysUtils;
var
   FileU : TextFile;
   FileAdr, FileCon : String;
// this array holds strings...
   FileAr: array of string
// this array holds ONE string... ->  FileAr : Array of Char Absolute FileCon;
   i : Integer;

begin
  Writeln('Please enter your text files adress(please write the full adress)');
  readln(FileAdr);
  if not fileexists(FileAdr) then BEGIN
    Writeln('Error: FILE NOT FOUND');
    Halt; // will stop the program on error
  END;

  AssignFile(FileU, FileAdr);
  Reset(FileU);
  i:= 0;
  While not EOF(FileU) do BEGIN
    Readln(FileU, FileCon);
    setlength(FileAr,i+1); // dynamically allocate space for strings
    FileAr[i]:= FileCon;    // assign string to the new slot
    Writeln('Array ', i, ': ', FileAr[i]); // show on screen
      inc(i);
  END;
  CloseFile(FileU);
  { remember to free memory when you're done }
  for i:= low(FileAr) to high(FileAr) do FileAr[i]:= ''; // free strings first
  Finalize(FileAr); // or setlength(FileAr,0); frees the array itself
end.

Another approach would be:

Code: [Select]
program pjct9;
{$mode objfpc}{$H+}
uses SysUtils;
var
   FileAdr, FileCon: string;
   FileLst: TStringList; { use an object of strings }
   I: integer; { for oldstyle use... }
begin
  { sanity check }
  writeln('Please enter your text-file adress(please write the full adress)');
  readln(FileAdr);
  if not fileexists(FileAdr) then begin
    writeln('Error: FILE NOT FOUND');
    halt; { will stop the program on error }
  end;
  { now allocate strings object }
  FileLst:= TStringList.Create;
  try
    { read the file in one fell swoop }
    FileLst.LoadFromFile(FileAdr);
    { then show on screen, using new "for-in-do" construct }
    for FileCon in FileLst do writeln(FileCon);
    { or show on screen, using oldstyle construct }
    for i:= 0 to FileLst.Count-1 do writeln(FileLst[i]);
  finally
    { remember to free memory when you're done }
    FileLst.Free;
  end;
end.

... a sligthly more modern approach :o)

HTH
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

MuteClown

  • New Member
  • *
  • Posts: 33
Re: Text file to a Array
« Reply #2 on: February 27, 2010, 08:10:40 pm »
Ah ty, sorry for the delay in my response.

I see now that when using a dynamic array i need to make sure i setlength, but don't array's go 0,1,2,3 etc... ?
Or does pascal just work with 1,2,3,4 etc? Because you set the array's length starting at 1, but seem that when calling the array it starts at 0, is that right?


Say instead of putting each line into a string i want to store them as characters.

At the moment say i have a file which says:

Hello
World

The array will look like this  
Code: [Select]
[0] = Hello, [1] = World Say i want it like
Code: [Select]
[h],[e],[l],[l],[o],[w],[o],[r],[l],[d]Can you pass a string into a char array? Essentially aren't they the same?


Thank you again for reply, it helps a lot  :D

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: Text file to a Array
« Reply #3 on: February 27, 2010, 08:27:34 pm »
Say instead of putting each line into a string i want to store them as characters.

At the moment say i have a file which says:

Tell us what you want to achieve; that makes giving hints easier  :)
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

MuteClown

  • New Member
  • *
  • Posts: 33
Re: Text file to a Array
« Reply #4 on: February 27, 2010, 10:13:04 pm »
go through the file and find how many of each letters and number are in the file, so say 5 A's ,2 B's etc.. are in the file, also looking to see how many of each are on each line. I'm not doing it for any specific reason just as a learning excise.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: Text file to a Array
« Reply #5 on: February 27, 2010, 10:23:24 pm »
It's easy using a TStringList.
You can access individual ASCII characters like this:

  sl:=TStringList.Create;
  sl.LoadFromFile('filepath');
  AChar:=sl[LineIndex][CharIndex];
  sl.Free;

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: Text file to a Array
« Reply #6 on: February 27, 2010, 11:18:11 pm »
go through the file and find how many of each letters and number are in the file, so say 5 A's ,2 B's etc.. are in the file, also looking to see how many of each are on each line. I'm not doing it for any specific reason just as a learning excise.

So you want access to the individual characters of each string. You do not need a seperate array for that, you can access the string as an array.
Say S is of type String. Now S[1] is the first character in the string.

You want to count how much of each character is in the file?
You'll have to define the characters you want to count: only lower ASCII, or even all UTF-8 characters that exist?
How will you keep scores?

An example:

Code: [Select]
Type
  LowAsciiRange = #32..#127;

const
  //a set of chars containing all LowerAscii writable chars, starting with #32 = space.
  LowAsciiChars = [Low(LowAsciiRange)..High(LowAsciiRange)];
 
var
  score: array[LowAsciiRange] of Integer; //array to hold scores in
  S: String;
  i: Integer;
  c: Char;

begin
  //set all scores to 0
  for c := Low(LowerAsciiRange) to High(LowerAsciiRange) do score[c] := 0;
  //can also be done by: FillChar(Score, SizeOf(Score), 0);
  ...
  ...
  //Assume you have read string S form the file:
  for i := 1 to length(S) do
  begin
    //if S[i] is a Lower Ascii character
    if (upcase(S[i]) in LowAsciiChars) then
      //this is the same as: Score[Upcase(S[i])] := Score[Upcase(S[i])] + 1;
      Inc(Score[Upcase(S[i])]);
  end;
  ...
  ...
end.

Bart

 

TinyPortal © 2005-2018