Read CSV file as txt, char by char

Hi guys,
I have a CSV file that looks something like this
23;tst;;;12;TEST;2;......
So, my delimiter is ;

Using excel buffer is not an option so I need to read each line of this txt file char by char and write data to some tables because each line has cca 2500 characters so I cannot simply read whole line at once, as one string.
This is in NAV 2009 R2.

Does anyone have any suggestions how to accomplish this?

THanks!

Answers

  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    edited 2018-02-05
    1. You can use a dataport, setting the field delimiter to ; (that will work if you have some sensible record delimiter, new line or smth)
    2. You could load the file into bigText using streams, and parse it from there
    3. you can read it character by characted using streams too

    The dataport will do for you column parsing, while reading the file in code will require you to build some parser to extract columns.

    If you decide not to use ther dataport, then loading the file all in one go into BigText will be much much quicker that reading it char by char, but parsing it might be a bit more tricky.

    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • TiwazTiwaz Member Posts: 98
    Thank you for your aswer @Slawek_Guzek
    I think I'd go with char by char reading.
    How can I do it?
    I guess, something like
    file.READ(CharVar);
    but I'm kinda stuck on what to do next...
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    edited 2018-02-05
    This is from NAV 2009 Help:
    FileTest.OPEN('c:\XMLDocs\NewTest.txt');
    FileTest.CREATEINSTREAM(StreamInTest);
    // Starting a loop
    WHILE NOT (StreamInTest.EOS) DO BEGIN
      Int := StreamInTest.READTEXT(Txt,100);
      MESSAGE(Txt + '\Size: ' + FORMAT(Int));
    END;
    FileTest.CLOSE();
    
    The above reads the file into Txt var in 100 character long chunks. Use it as your starting point.
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
Sign In or Register to comment.