Recent

Author Topic: How to read/write text files using Lazarus?  (Read 32305 times)

Tiago

  • Guest
How to read/write text files using Lazarus?
« on: November 21, 2004, 09:39:13 pm »
Hi there!

I'm having some problems to read and write textfiles inlazarus.
My code's like this:

Code: [Select]

var tf : textFile;
     parameter : string;
begin
(...)
              assignFile(tf, parameter);
             
              {$I-)
                  reset(tf);
              ($I+}
             
              if IORESULT <> 0
              then return := COMNOFILEERROR //error constant
              else begin
                     textMemo.append('file found');
                     
                     closeFile(tf);
                     return := COMNOERROR;
                   end;

I actually write the message "file found" in my textMemo, but I receive the dialogbox message "file not open".

What am I doing wrong?
How would I do it right?

I'm desperate!!! I need to deliver this project next week!!!!

HELP

Thanks

Tiago

Tiago

  • Guest
actually, it doesn't obbey the {I-} {$I+} directives
« Reply #1 on: November 21, 2004, 10:18:23 pm »
I found out that it works when the file exists, but when it doesn't, the program does not obbey the directives...

how would I treat the IO errors without the $I directives?????

Tiago

Tiago

  • Guest
what the....
« Reply #2 on: November 22, 2004, 12:35:15 am »
Ok...
when I change the former code to
Code: [Select]

var tf : textFile;
     parameter : string;
begin
(...)
              assignFile(tf, parameter);
             
              {$I-)
                  reset(tf);
              ($I+}
             
              if IORESULT <> 0
              then return := COMNOFILEERROR //error constant
              else begin
                     while not eof(tf) do
                      begin
                        readln(tf,lineFromFile); //linefromfile is a string
                         textMemo.append(lineFromFile);
                      end;


and I got an invalid handler error...

I'm beginnind to dislike lazarus.....

I BEG FOR HELP

Tiago[/b]

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
How to read/write text files using Lazarus?
« Reply #3 on: November 22, 2004, 10:29:26 am »
I don't know if it is a typo, but reset(tf) is not compiled, because {$I-) should end with a }. Also ($I+} should be {$I+}

Syntax highlighting helped me. ;-)

Lightning

  • Sr. Member
  • ****
  • Posts: 422
How to read/write text files using Lazarus?
« Reply #4 on: November 22, 2004, 11:02:53 am »
Why don't you use string lists or some other higher level component ?
The future must be... Fast and OpenSource so...
Think Open and Lightning Fast!

Tiago

  • Guest
what do you mean?
« Reply #5 on: November 22, 2004, 03:50:18 pm »
" don't know if it is a typo, but reset(tf) is not compiled, because {$I-) should end with a }. Also ($I+} should be {$I+} "

it was my miss: in my code I typed it right, but in the forum  I typed wrong...

"Why don't you use string lists or some other higher level component ?"
What did you mean with this, Lightning?

All I need is to read some strings from file and save them in another file...
Which high-level component should I use?

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2696
How to read/write text files using Lazarus?
« Reply #6 on: November 22, 2004, 06:02:40 pm »
TStringList

or do you want to have a 1 to 1 copy, then I would suggest something like FileCopy (if it exists) or TFileStream.

(gloabal filestream idea)

instream := TFileStream.Create(infilename);
outstream := TFileStream.Create(outfilename);
outstream.readfromstream(instream);
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: what do you mean?
« Reply #7 on: November 22, 2004, 07:13:49 pm »
Quote from: "Tiago"
" don't know if it is a typo, but reset(tf) is not compiled, because {$I-) should end with a }. Also ($I+} should be {$I+} "

it was my miss: in my code I typed it right, but in the forum  I typed wrong...


After I fixed this, I could load an existing file in a memo, so I would suggets you create a simpler example and post it completely.

Tiago

  • Guest
what about ... TStringList?
« Reply #8 on: November 23, 2004, 07:37:23 pm »
Hello guys.

Thanks a LOT for trying to help me.

I looked for the TStringList documentation, which is easier to use comparing to the pascal methods.

I haven't used it before because I didn't know it existed.

Lazarus could be more well-documented...

Anyway, can someone say how do I save a bunch of separated strings in a file using TStringList? I have this data structure composed of strings, and I'd like to save them in a sequencial text file.

Can you keep on helping me?

Thanks a LOT.

Tiago

Lightning

  • Sr. Member
  • ****
  • Posts: 422
How to read/write text files using Lazarus?
« Reply #9 on: November 23, 2004, 11:25:00 pm »
Unfortunately Lazarus doesn't have much documentation, you should check the Lazarus-CCR site, the included docs and get some Delphi books (i would suggest "Mastering Delphi 6/7" by Marco Cantu).
If you want to save only some items then use 2 Lists and transfer required items to the second list and save it, "explore" objects using [CTRL]+[SPACE] after the dot of each class/component (identifier completion), use [CTRL]+[CLICK] on a property/method to find it's implementation.
The future must be... Fast and OpenSource so...
Think Open and Lightning Fast!

RudieD

  • Full Member
  • ***
  • Posts: 234
How to read/write text files using Lazarus?
« Reply #10 on: November 25, 2004, 07:53:20 pm »
Hi Tiago, don't know if this will help :

if you replace

  reset(tf);

with

  if FileExists(Parameter) then // If the file exists
    Reset(tF)          // open it
  else
    ReWrite(tF);     // else create it
The FRED Trainer. (Training FRED with Lazarus/FPC)

Anonymous

  • Guest
How to read/write text files using Lazarus?
« Reply #11 on: December 02, 2004, 01:20:24 pm »
Here's how I would read a file into a stringlist :

Procedure domystuff(parameter:string);

var aStrinlist:Tstringlist;

begin
    aStringlist:=TStringlist.create;
    If fileexists(parameter) then aStringlist.loadfromfile(parameter);

     // Do what ever you do with the strings
     // ...
     // ...
     // ... and in the end

     aStringlist.destroy
end;

Does that make sense?

Lightning

  • Sr. Member
  • ****
  • Posts: 422
How to read/write text files using Lazarus?
« Reply #12 on: December 02, 2004, 10:34:07 pm »
Use aStringList.Free instead of Destroy since it's safer :)
The future must be... Fast and OpenSource so...
Think Open and Lightning Fast!

Tiago

  • Guest
Thanks
« Reply #13 on: December 05, 2004, 12:38:22 am »
Thanks
that worked pretty good

 

TinyPortal © 2005-2018