FILE.READ method only read first line of ascii file

EdZEdZ Member Posts: 10
Hi all,
I hope that someone can help me 'coz th problem described below is drivig me nuts:

I have a function Import_Order in a Code Unit to import ascii files.
The ascii files contain sales order header lines (labeled TI ) as well sales order lines (labeled LI )
A typical file would look like this:

TI;70;14012008;16012008;"PRI";"CF-0000001";"TILL";"Urgent svp"
LI;70;0;"CF-0000001";1;"F003-105";10;188,2
LI;70;0;"CF-0000001";2;"F003-106";8;150,56
LI;70;0;"CF-0000001";3;"F003-110";15;79,35
TI;70;19102010;19102010;"PRI";"CFA-000001";"TILL";""
LI;70;0;"CFA-000001";1;"F001-41";10;1000
LI;70;0;"CFA-000001";2;"F001-41";10;900
TI;70;20102010;20102010;"PRI";"CFA-000002";"TILL";""
LI;70;0;"CFA-000002";1;"F001-40";4;42,34

The basic code of the import function is below:

FUNCION Import_Order

L_Tag_Ok := TRUE;

IF ( EXISTS ( P_Full_File_Name ) ) THEN
BEGIN

IF ( L_File.OPEN( P_Full_File_Name) ) THEN
BEGIN
L_File.WRITEMODE( FALSE );
L_File.TEXTMODE( TRUE );
WHILE ( L_File.READ( L_StrLine ) <> 0 ) AND ( L_Tag_Ok ) DO
BEGIN
L_StrField := Get_Field( L_StrLine, ';' );
CASE ( UPPERCASE( L_StrField ) ) OF
'TI' : L_Tag_Ok := Import_Order_Header( LR_Order_Header,
L_StrLine,
L_Customer_No,
L_File_Name ) ;
'LI' : L_Tag_Ok := Import_Order_Line( LR_Order_Line,
L_StrLine,
LR_Order_Header ) ;
END;
END;
L_File.CLOSE ();
END;
END;

EXIT( L_Tag_Ok );

The problem:

When the function gets called from within an OnPush trigger of a button on a form all lines are read.
When the function gets called from the OnRun trigger of the same code unit, only the first line of a text file gets imported.
Actually the File.Read method only reads a line the first time the cycle gets executed.
In both situations I'm trying to import the same file.

Anybody has a suggestion ?

Answers

  • krikikriki Member, Moderator Posts: 9,110
    I don't see how that could influence the reading of the file.
    I would use the debugger to see what's happening in the case of the OnRun-trigger.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • kinekine Member Posts: 12,562
    It seems like the L_Tag_Ok is false for some reason... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • EdZEdZ Member Posts: 10
    Hi,

    thanks for your suggestions.

    I tried limiting the fuctionality of the code as below.
    Sadly the same behaviour still occurs.

    FUNCION Import_Order ( P_Full_File_Name : Text[250]) L_Tag_Ok : Boolean

    L_Tag_Ok := TRUE;

    IF ( EXISTS ( P_Full_File_Name ) ) THEN
    BEGIN

    IF ( L_File.OPEN( P_Full_File_Name) ) THEN
    BEGIN
    L_File.WRITEMODE( FALSE );
    L_File.TEXTMODE( TRUE );
    WHILE ( L_File.READ( L_StrLine ) <> 0 ) AND ( L_Tag_Ok ) DO
    BEGIN
    MESSAGE( L_StrLine );
    END;
    L_File.CLOSE ();
    END;
    END;

    EXIT( L_Tag_Ok );

    One thing that has changed between is the size of L_StrLine and P_Full_File_Name. Changed (and rechanged ) between size 1024 and 250.
    Another thing I can mention is that it has worked before, but i made some changes I can't recall, but sadly we don't work with a versionning system...
    I hoped somebody actually had expierenced the same issue.

    cheers.
  • kinekine Member Posts: 12,562
    L_File.READ( L_StrLine ) <> 0

    Is not good condition to test end of file. If you read empty string, the result could be 0, but not EOF hitted... Try to compare position and file length, it is much better.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • EdZEdZ Member Posts: 10
    edited 2011-09-15
    Thanks Kamil,

    Allthough we're not supposed to receive files with empty lines, I'll change that anyway just to practice Best practices.

    Furthur to my problem I found out the following:

    A difference between the two calls to the import_order function is the loation of the file to import.
    In the case where the file is imported from the user's temp directory on the C: drive, the file gets read correctly
    If the file is in a directory on my D: drive (2nd partition on my disk) only the first line gets read.

    I confirmed this by hardcoding the filename in both function calls.

    Anybody ?
  • kinekine Member Posts: 12,562
    Are those disc local disc with NTFS/FAT system? Are the files on both identical? I mean, aren't there different end-of-line characters? (DOS vs UNIX EOL).
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • krikikriki Member, Moderator Posts: 9,110
    One remark: I noticed that EOF is slow.

    So best test on length of the string and if it is 0 then also test EOF. The code is something as this:

    REPEAT
      blnEOF := TRUE;
      fil.READ(txtString);
      blnEOF := (txtString = "");
      IF blnEOF THEN
        blnEOF := fil.EOF;
    UNTIL blnEOF;
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • EdZEdZ Member Posts: 10
    C: and D: are windows NTFS partitions on the same physical disk

    But your suggestion pointed me into a direction. The file gets downloaded with different functions, depeding on from which form the import_order funtion gets called...
    I'll have to do some research there..
  • EdZEdZ Member Posts: 10
    I've solved the problem:

    In the case where the file was downloaded in FTP-mode Binary all lines where correctly read by FILE.READ,
    In the case where the file was downloaded in FTP-mode ASCII FILE, only the first line was read by FILE.READ.

    Silly me to think that if you would use Mode ASCII for File.Read, you'ld have to use ftp-mode ASCII as well.... :roll:

    Thanks for your time guys...

    \:D/ :lol: \:D/
  • vaprogvaprog Member Posts: 1,139
    EdZ wrote:
    Silly me to think that if you would use Mode ASCII for File.Read, you'ld have to use ftp-mode ASCII as well.... :roll:
    I disagree. It was exactly the right thing to do. But either the ftp server is wrong or the file has not the proper line endings for the platform you download it from.
    Luckily a binary transfer works for you.
Sign In or Register to comment.