Get Tab character in a string read from a txt file

GabryGabry Member Posts: 48
edited 2013-06-18 in NAV Three Tier
I have to read from a text file some field in order to import data. The row is separated from tab character. I tryied to use this code:

[char] TabChar := 9;
[string] Tab := Format(TabChar);
[integer] position := StrPos(NomeFile, NomeFile);
[integer] TabPosition := STRPOS(ImportTextSubtring, Tab); // here TabPosition gets always zero (it does not detect the tab in the string)

Then I get the string until the tab as field and I replace the current long string with one without the last one.

ImportTextSubtring := COPYSTR(ImportTextSubtring, ImportTextStartPos);

The issue is that "STRPOS(ImportTextSubtring, Tab)" does not detect "Tab" character in the text. How can I detect the tab field? Or how can I get the separation of the string using a Tab character? The issue is that I use a function which gets the string using an initial fixed postion and a final fixed position.

But if I use a concatenated string using a Tab character, the "StrPos" functions do work:

Tab := 9;
vtab := FORMAT(Tab);
vtest := 'pippoxx' + vtab + 'pippo';
TabPosition := STRPOS(vtest, vtab); // TabPosition gets 8: right!
MESSAGE('The position is: %1', TabPosition);

Comments

  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    I would check the import file if the tab character is really character no. 9. Maybe it looks like a tab, but in fact it isn't.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • okioki Member Posts: 46
    Hi,

    try the following:

    Tab := ' '; // important to initialize string length to 1
    Tab[1] := 9; // store tab into this 1-char-string

    Oliver
  • GabryGabry Member Posts: 48
    I would check the import file if the tab character is really character no. 9. Maybe it looks like a tab, but in fact it isn't.

    Attached a text piece in image jpg format showing invisible characters file to be imported. If I write tab character using ALT+[ASII code number], I will get a different character (that is '○'). But showing all characters (also not visible ones) on text file it appears like an arrow, that is a horizontal tab character.

    Can you check the attached file please?
  • GabryGabry Member Posts: 48
    oki wrote:
    Hi,

    try the following:

    Tab := ' '; // important to initialize string length to 1
    Tab[1] := 9; // store tab into this 1-char-string

    Oliver

    In my code 'Tab' variable is a char. I tried using your code, but it does not change anything: STRPOS returs zero.
    Anyway thank you. Do you have any further ideas or insights?
  • Me_OntourMe_Ontour Member Posts: 11
    Hi,

    You should Try it with textvariable Length =1 and read the file via a Stream
    Then you can Check if your Tab is really ASCII 9


    Chris
  • thegunzothegunzo Member Posts: 274
    You can try a different method for reading the file.

    Here is an example that works for me,
    FileName := 'C:\temp\tabtest.txt';
    FieldSeparator := '';
    FieldDelimiter := '';
    FieldSeparator[1] := 9;
    CodePage := 'iso-8859-1';
    DelimitedClientFileToExcelBuf(FileName,ExcelBuffer,FieldSeparator,FieldDelimiter,CodePage)
    
    OBJECT Codeunit 50000 Import File to Excel Buffer
    {
      OBJECT-PROPERTIES
      {
        Date=18.06.13;
        Time=10:15:50;
        Modified=Yes;
        Version List=Dynamics.is;
      }
      PROPERTIES
      {
        OnRun=BEGIN
              END;
    
      }
      CODE
      {
        VAR
          Text101@1000000002 : TextConst 'ENU=Please select a file name;ISL=Tilgreina verÐur skr arheiti';
          Text102@1000000001 : TextConst 'ENU=File not found;ISL=Skr  er ekki fyrir hendi';
          Text103@1000000000 : TextConst 'ENU=Importing Text File...\\@1@@@@@@@@@@@@@@@@;ISL=Les inn textaskr ...\\@1@@@@@@@@@@@@@@@@';
    
        PROCEDURE DelimitedClientFileToExcelBuf@1100408006(FileName@1100408000 : Text;VAR ExcelBuffer@1100408002 : Record 370;FieldSeparator@10000200 : Text[1];FieldDelimiter@1000000005 : Text[1];CodePage@1000000000 : Text[10]);
        VAR
          ClientFile@1000000003 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.File" RUNONCLIENT;
          ClientFileLines@1000000002 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array" RUNONCLIENT;
          Encoding@1000000001 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding" RUNONCLIENT;
          DialogMgt@1000000004 : Codeunit 10000200;
          TextLine@1100408009 : Text[1024];
          ColumnValue@1100408010 : Text[1024];
          Loop@1100408005 : Integer;
          RowNo@1100408007 : Integer;
          ColumnNo@1100408008 : Integer;
        BEGIN
          IF FileName = '' THEN
            ERROR(Text101);
    
          IF NOT ClientFile.Exists(FileName) THEN
            ERROR(Text102);
    
          ClientFileLines := ClientFile.ReadAllLines(FileName,Encoding.GetEncoding(CodePage));
          ExcelBuffer.RESET;
          ExcelBuffer.DELETEALL;
          DialogMgt.WindowOpen(Text103);
          DialogMgt.WindowSetTotal(1,ClientFileLines.Length);
    
          FOR Loop := 0 TO (ClientFileLines.Length - 1) DO BEGIN
            DialogMgt.WindowProcess(1);
            TextLine := ClientFileLines.GetValue(Loop);
            RowNo := RowNo + 1;
            ColumnNo := 1;
            WHILE STRPOS(TextLine,FieldSeparator) > 0 DO BEGIN
              ColumnValue := ExtractFirstPartFromString(TextLine,FieldSeparator,FieldDelimiter);
              ExcelBuffer.INIT;
              ExcelBuffer.VALIDATE("Row No.",RowNo);
              ExcelBuffer.VALIDATE("Column No.",ColumnNo);
              ExcelBuffer."Cell Value as Text" := ColumnValue;
              ExcelBuffer.INSERT;
              ColumnNo := ColumnNo + 1;
            END;
          END;
    
          DialogMgt.WindowClose;
        END;
    
        PROCEDURE ExtractFirstPartFromString@1000000001(VAR Text@1000000000 : Text;Separator@1000000001 : Text[1];Delimiter@1000000003 : Text[1]) Token : Text;
        VAR
          Pos@1000000002 : Integer;
        BEGIN
          Pos := STRPOS(Text,Separator);
          IF Pos > 0 THEN BEGIN
            Token := COPYSTR(Text,1,Pos-1);
            IF Pos+1 <= STRLEN(Text) THEN
              Text := COPYSTR(Text,Pos+1)
            ELSE
              Text := '';
          END ELSE BEGIN
            Token := Text;
            Text := '';
          END;
          IF Delimiter <> '' THEN
            IF (COPYSTR(Token,1,1) = Delimiter) AND (COPYSTR(Token,STRLEN(Token),1) = Delimiter) THEN
              Token := COPYSTR(Token,2,STRLEN(Token) - 2);
        END;
    
        BEGIN
        END.
      }
    }
    
    

    More about this method for reading and writing text files on my blog http://www.dynamics.is/?p=1405
    ________________________________
    Gunnar Gestsson
    Microsoft Certified IT Professional
    Dynamics NAV MVP
    http://www.dynamics.is
    http://Objects4NAV.com
Sign In or Register to comment.