Importing Text from a TAB separated file

MrinmayMrinmay Member Posts: 74
Hi Exparts,

I want to import some data from a file Which is TAB separated. I have written a code like below:

File1.TEXTMODE := True;
File1.Open(path);
File1.Read(String);
Cha := 9;

Pos := STRPOS(String1,FORMAT(Cha));
String2 := STRSUBSTNO(String1,1,Pos-1);
MESSAGE(String2);

But here value before first TAB is not coming.
If TextMode is False then all the line in the text coming as a single string .

Please help me how to separate a string with TAB separator if the file TEXTMODE is true.


Regards,
Mrinmay.

Comments

  • SavatageSavatage Member Posts: 7,142
    any particular reason you're not using a dataport?
  • MrinmayMrinmay Member Posts: 74
    I am changing those Dataport into report so that we can run it from RTC. We can use XMLport also. Anyway got the solution :

    TestFile1.TEXTMODE := TRUE;
    TestFile1.OPEN(Path);

    TestFile1.CREATEINSTREAM(InStream1);
    WHILE NOT InStream1.EOS DO BEGIN
    InStream1.READTEXT(String1,250);
    Cha := 9;
    String2 := CONVERTSTR(String1 ,FORMAT(Cha),',');
    END;
    Now we can break the string depending on ','. Or we can separate there itself.

    Thank you everybody.
  • vaprogvaprog Member Posts: 1,139
    There were three problems in your initial code snippet:
    1. You read String instead of String1
    2. You need to use COPYSTR to extract the field, not STRSUBSTNO
    3. READ from a TEXTMODE file converts tabs to spaces. You need to use CREATEINSTREAM and then READTEXT from the stream.

    You do not need the CONVETRSTRING in your second code snippet. In fact this causes problems if there are commas in the data. STRPOS with the tab character formated as you did works well.
    Mrinmay wrote:
    Now we can break the string depending on ','. Or we can separate there itself.
    This leads me to think you attempt to use SELECTSTR to split the line into fields. However, you should not use SELECTSTR for unknown data as SELECTSTR requires all fieled values to be distinct after they were capitalised, and requires the last field to be non-empty. (See C/SIDE help on SELECTSTR).
Sign In or Register to comment.