Solved

Readln Problem

Posted on 1997-05-14
7
471 Views
Last Modified: 2010-04-04
When I use the Readln procedure to read a line from a text file, everything goes ok.  But when I use it within a loop to read the body of the text it gives me everything but when there's a empty line (or simply a couple of carraige returns, whatever) it doesn't give back a result.  So when I'm writing to an array of strings, it acts like it skips all carraige returns.  Does anyone know how to resolve this or a better procedure to use.  Any tips would be helpful.
0
Comment
Question by:peteyj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
CFantin earned 200 total points
ID: 1336269
try this code

procedure TForm1.Button1Click(Sender: TObject);
var F: TextFile;
    I: Integer;
    A: Array [0 .. 100] of String;
begin
  AssignFile(F, 'C:\MyFileName.Txt');
  Reset(F);
  I := 0;
  while not EOF(F) do
  begin
    ReadLn(F, A[I]);
    Inc(I);
  end;
  CloseFile(F);
end;

This code works for me.

Why don't you use a StringList instead of an array like this

procedure TForm1.Button1Click(Sender: TObject);
var F: TextFile;
    I: Integer;
    S: String;
    A: TStringList;
begin
  AssignFile(F, 'C:\MyFileName.Txt');
  A := TStringList.Create;
  Reset(F);
  while not EOF(F) do
  begin
    ReadLn(F, S);
    A.Add(S);
  end;
  CloseFile(F);
end;

Hope this helps
Good Luck
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1336270
Try "LoadFromFile" method of TStringList.

/// John
0
 

Author Comment

by:peteyj
ID: 1336271
Using Writeln, I am also getting errors.  Any suggestions for these?  And how many strings can I have in a stringlist?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:erajoj
ID: 1336272
Submit the code so we can have a look!

/// John

0
 
LVL 1

Expert Comment

by:CFantin
ID: 1336273
A StringList Should be able to hold 32767,
but I have never tried to hold that many.

Does the ReadLn code work?
0
 

Author Comment

by:peteyj
ID: 1336274
The readln procedures are working ok, but now the writeln still aren't.  Here's the code if anyone can take a crack at it.

function TMainForm.OverWriteTextFile(SatFileName: String;
                                     SatRTextFile: RTextFile): Boolean;
var
  SalTextFile         : TextFile;
  SalFileName         : String;
  SalRTextFile        : RTextFile;
  SalWriteString      : String;
  i                   : integer;
begin
  Result := False;
  SalFileName  := SatFileName;
  SalRTextFile := SatRTextFile;
  AssignFile(SalTextFile, SalFileName);
  try
    Rewrite(SalTextFile);
  except on EInOutError do
    begin
      CloseFile(SalTextFile);
      Result := False;
      exit;
    end;
  end;
  for i := 1 to 8192 do
  begin
    SalWriteString := SalRTextFile.OutputBuffer[i];
    if (SalWriteString <> '') then
    begin
      try
        Writeln(SalTextFile, SalWriteString);
      except on EInOutError do
        begin
          CloseFile(SalTextFile);
          exit;
        end;
      end;

    end;
  end;
  CloseFile(SalTextFile);
  Result := True;
end;


All variables of type RTextFile are records, and InputBuffer and OutputBuffer are arrays of strings, which I plan to change to SringLists.  If you've got any questions on anything else let me know.
0
 
LVL 1

Expert Comment

by:CFantin
ID: 1336275
Sorry I have not responded for a while but I was on site in another City.

What error are you getting when you use the WriteLn?

If the problem is that it is not writing the blank lines then try
taking out the check "if the SalWriteString <> '' then".

Other than that it looks like your code sould work.

Here is a bit of code that will write a Memo to a text file

unit MyTextFile.Pas;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function WriteTextFile(SatFileName: String; StringListToWrite: TStringList): Boolean;
var FileToWrite: TextFile;
    I: Integer;
begin
  AssignFile(FileToWrite, SatFileName);
  //Add try finally to close the file
  try
    try
      Rewrite(FileToWrite);
    except
      on EInOutError do
      begin
        Result := False;
        Exit;
      end;
    end;
    // for item in the StringList
    for I := 0 to StringListToWrite.Count - 1 do
    begin
      try
        //Write the line
        Writeln(FileToWrite, StringListToWrite[I]);
      except
        on EInOutError do
        begin
          Result := False;
          Exit;
        end;
      end;
    end;
  finally
    CloseFile(FileToWrite);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  WriteTextFile('C:\MyTextFile.txt', TStringList(Memo1.Lines));
end;

end.

Try this out.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reconfigure Delphi Install? 2 65
Dev express lookupcombo 3 45
update joined tables 2 62
Why  'Host Connection unavailable/Socket Error #113 No route to host' 6 75
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question