EOF when reading from a text file

atankersleyatankersley Member Posts: 32
In this code InputFile and OutputFile are both text files in notepad. I added the word "QUIT" to the end of the InputFile to stop execution when there were no more records(Item Numbers) and it works. Is there a better way to do this? I normally use EOF as a Boolean with other languages but could not find it referenced anywhere.

IF InputFile.OPEN(FileNameIn) THEN
  BEGIN
    InputFile.READ(TNumber);
    WHILE TNumber <> 'QUIT' DO
      BEGIN
        ProgWin.UPDATE;
        MyItem.SETFILTER("No.",TNumber);
        IF (MyItem.FIND('-') = TRUE) THEN
          BEGIN
            OutputLine := MyItem."No." + '~' +
            FORMAT(MyItem."Unit Price");
            OutputFile.WRITE(OutputLine);
            Number := Number + 1;
            InputFile.READ(TNumber);
          END
        ELSE
          ERROR(NoItemNumber, TNumber);
     END;
  END
ELSE
  ERROR(NoFile, FileNameIn);
tank

Comments

  • ara3nara3n Member Posts: 9,258
    You need to create an instream
      MyFile.OPEN(FileName);
      MyFile.CREATEINSTREAM(IStream);
      REPEAT
        IStream.READTEXT(textLine);   
      UNTIL IStream.EOS;
    
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    I think
    File.pos = file.len
    

    Also works

    But I like the streams.
  • krikikriki Member, Moderator Posts: 9,118
    I think
    File.pos = file.len
    

    Also works

    But I like the streams.
    But don't use this in a loop.
    The File.POS and File.LEN are very slow!
    So it is better to calculate the POS manually.
    But of course: streams are an good alternative.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • kinekine Member Posts: 12,562
    From on-line help:
    Read] := File.READ(Variable)
    Read

    Data type: integer

    This optional return value tells you the number of bytes read.

    File

    Data type: file

    Use this variable to refer to the file.

    Variable

    Data type: any

    The destination variable.

    It means, if you test the result of the reading and the result is =0 it means, there are no more data to read (you are on the end of file).

    Common construction:
      ...
      While File.Read(Data) <> 0 do begin
         ...
      end;
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • ara3nara3n Member Posts: 9,258
    That not totally true. If you have file.textmode (true) read will return zero if the line is empty.

    Also the way he has written the code he is not settng this property. So
    InputFile.READ(TNumber) will read the whole file. the first time and will have an overflow error.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • kinekine Member Posts: 12,562
    Ah, sorry, you are right... :-#
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.