Forum > General

Text file to a Array

(1/2) > >>

MuteClown:
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: ---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.

--- End code ---


cdbc:
Hi MuteClown
I haven't tested it, but this revised code should compile and run...


--- Code: ---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.
--- End code ---

Another approach would be:


--- Code: ---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.
--- End code ---

... a sligthly more modern approach :o)

HTH
Regards Benny

MuteClown:
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: --- [0] = Hello, [1] = World
--- End code ---
Say i want it like
--- Code: --- [h],[e],[l],[l],[o],[w],[o],[r],[l],[d]
--- End code ---
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:

--- Quote from: MuteClown on February 27, 2010, 08:10:40 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:
--- End quote ---

Tell us what you want to achieve; that makes giving hints easier  :)

MuteClown:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version