Advertising

Read all lines from .DT file with Karel

  • I am trying to write a Karel program that will read all existing lines in a .dt file.

    Currently my .dt file has 3 lines, simply:


    Code
    Line 1
    Line 2
    Line 3

    And I want to read all 3 lines with KAREL, this is my KAREL code:

    It is almost working, but it is reading only the first line, when it attempts to read the second line I get the error:

    Code
    INTP-311 (READINGFROMFILE, 19) Uninitialized data is used

    Line 19 is the line with the WRITE statement, so on the second iteration of the loop, no data was read and tempString is empty, (im assuming), why is that?


    What is the correct way to read all lines from a file?

  • AD
  • You have "filename" defined twice, once as a constant and once as a variable. I can't even compile this, I'm getting an "Id is already defined" compiler error. That said, remove the ", CR" from your read statement. You should also check the IO status before trying to write. See below for an example of how I read dt files.


  • You have "filename" defined twice, once as a constant and once as a variable. I can't even compile this, I'm getting an "Id is already defined" compiler error. That said, remove the ", CR" from your read statement. You should also check the IO status before trying to write. See below for an example of how I read dt files.


    Hey, thanks for the help. First my FILENAME constant was just a name for the file to be opened, I havent defined it twice. Anyway, I just tried your code, and it is reading just the first 2 lines, but not the 3rd. I tried adding another 4th Line, and now it is showing Line 1; Line 2; Line 3, but again skips the last line.

  • I've always gotten around that by adding a dummy line on the end of my .dt files. I'm sure there's probably a correct way to fix that problem, but I haven't figured it out yet. Maybe someone smarter than me will know how to make it read the last line.

  • I've always gotten around that by adding a dummy line on the end of my .dt files. I'm sure there's probably a correct way to fix that problem, but I haven't figured it out yet. Maybe someone smarter than me will know how to make it read the last line.

    Thanks for the tip, I will use that workaround for now :)

  • hello guys,

    have you tried to change the definition of Var from curline: string[40] to curline: string[80]?

    it shows error ???-598 when i built the KAREL file.

    do you happen to know what is the reason for that?

    regards,

    Chen

  • thanks for the reply, it works properly now.

    btw, it is good to know that we have similar target, i am trying to read a .dt file into an array of real.

    do you have experince about how to read .dt file into an array?

  • I Might have a part of solution for reading data of the last line. An extra empty line can be added in the end of .dt file, then if you use for loop with same iterations, the last line become readable without showing error.

    However, do you guys know how to append an extra line in the .dt file?

    Seems like, Append myfile(cr)does not work:/

  • Just read it as strings and do string manipulation to extract the data that you need from each row and then assign it to elements in your array

    for instance, my data string is given as below:

    1 100 .200 8 -179.897 7.189 -24.204 26.705 -69.770 75.901

    as far as i know, sub_str command can extract sub_string, however, the start and sub_string length has to be specified. in my case, both are random value. do you know if there is any better way to extract sub_string?

  • for instance, my data string is given as below:

    1 100 .200 8 -179.897 7.189 -24.204 26.705 -69.770 75.901

    as far as i know, sub_str command can extract sub_string, however, the start and sub_string length has to be specified. in my case, both are random value. do you know if there is any better way to extract sub_string?

    Well unify it, instead of sending it like this, send it like:

    10179.8970 00007.1890 10024.2040 00026.7050 10069.7700 00075.9010

    That way you will always have the same length, the first digit will show you weather its + or -, if its 0 its + if its 1 its -

    For example the 10024.2040, starts with 1 so its -, and then u convert the rest to a number CNV_STR_REAL('0024.2040', result) and then just multiply by -1 and woalla. 0000.0000 u have 4 digits on either side, add more if u need more.

  • This will parse a single line from your text file and store your real values to an array.

  • I Might have a part of solution for reading data of the last line. An extra empty line can be added in the end of .dt file, then if you use for loop with same iterations, the last line become readable without showing error.

    However, do you guys know how to append an extra line in the .dt file?

    Seems like, Append myfile(cr)does not work:/

    I just wrote this test program and it successfully adds a blank line:

  • Well unify it, instead of sending it like this, send it like:

    10179.8970 00007.1890 10024.2040 00026.7050 10069.7700 00075.9010

    That way you will always have the same length, the first digit will show you weather its + or -, if its 0 its + if its 1 its -

    For example the 10024.2040, starts with 1 so its -, and then u convert the rest to a number CNV_STR_REAL('0024.2040', result) and then just multiply by -1 and woalla. 0000.0000 u have 4 digits on either side, add more if u need more.

    my input .dt file is generated by Data Monitor option, which i cannot unify it beforehand. is it possible to unify it within Karel code? and it still need to read out the value, which is become a probelm again.

  • I just wrote this test program and it successfully adds a blank line:

    it seems that the AP only workable for MC: drive instead of UD1:

  • my input .dt file is generated by Data Monitor option, which i cannot unify it beforehand. is it possible to unify it within Karel code? and it still need to read out the value, which is become a probelm again.

    See post #14 in this thread. That code uses the spaces in your .dt file as delimiters to split the string into separate reals and store them to a real array.

  • See post #14 in this thread. That code uses the spaces in your .dt file as delimiters to split the string into separate reals and store them to a real array.

    there exists both space and tab between values in my .dt file, your code is applicable for searching index of space, but it is not workable with tab.

    do you have a clue about searching the index of tab?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account
Sign up for a new account in our community. It's easy!
Register a new account
Sign in
Already have an account? Sign in here.
Sign in Now