Announcement

Collapse
No announcement yet.

Working with strings

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Working with strings

    Hi

    I'm working with strings and I have different troubles to understand. The way that works 4d system language is a bit confusing.
    I try to read a text file which has different strings. These strings are float numbers. I can not convert this strings to a float numbers.
    I send you part of the code

    #constant MAX_LINE 35
    var GAIN_OFFSET[MAX_LINE];

    //This function read the file
    func read_file(var Filename)
    ////////////////////////////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////////
    var Handle; // File handle
    var Cadena[50];
    var n := 0;

    Handle := file_Open(Filename, 'r'); //open file

    file_GetS(Cadena,50,Handle);
    //putstr(Cadena);

    n:=1;

    while (n < MAX_LINE)
    file_GetS(Cadena,50,Handle); //read line -- [Caption variable]

    file_GetS(Cadena,50,Handle); //read line -- [Value of variable]

    str_Copy(GAIN_OFFSET[n],Cadena);

    n := n+1;
    wend
    n :=0;

    file_Close(Handle); //Close file
    endfunc


    ////Function which translate strings to floats and then operate them
    func Calculate_Level(var position, var setvalue)

    var G, O;
    var Gain[2], Offset[2];
    var value1[2];
    var Const[2];
    var temp[2];

    flt_VAL(Gain,GAIN_OFFSET[position]);
    //flt_VAL(Gain,"222.2");

    flt_VAL(Offset,O);
    flt_ITOF(value1,setvalue);
    flt_ITOF(Const,100);
    flt_DIV(temp,Gain,Const);
    flt_MUL(temp,temp, value1);
    flt_ADD(temp,temp,Offset);

    flt_PRINT(Gain, " %g \n");
    flt_PRINT(Offset, " %g \n");
    flt_PRINT(value1, " %g \n");
    flt_PRINT(temp, " %g \n");
    return flt_FTOI(temp);
    endfunc


    // Calling function

    read_file("File_F.TXT");

    result := Calculate_Level2(34,-30);


    Could somebody help me to understant how to work with strings and arrays of strings?

    Thanks a lot
    Jordi

  • #2
    Exactly what do these strings look like in the file?

    Your code is a bit weird, I would think you you should be using flt_Val() to convert the input string to a float, but as above, I'm not sure what the contents of the file looks like
    Mark

    Comment


    • #3
      After lot of hours of debugging i figure out that 4d systems dont write the memory. When I work with array of strings or array of Floats (doesnt matter) the for 4d systems doesnt like at all. Let me put example

      I have a function where I get some strings (in this case, with floats happens the same) and I want to try to save inside of array of Strings. In my case I get strings from a text file and I save inside of the Array. If I try to save using function from Diablo Library doesn't work, however if I save using the direct equal = looks like works (looks like). Inside of the funtion works because I print the values and the array of the strings seems correctly filled. However when I print the array in the main program that has default values (The array is defined global variable).

      In the case that I work in low level programing. I mean, I save Word by Word the array of strings then the program works perfectly fine.

      Could you help me to know how to work properly with Strings?

      P.s, It is a shame that we can not have a debugger. In this cases it will be very usefull.

      Jordi

      Comment


      • #4
        Hi Again

        I would like to add some simple code that shows a bit how it difficult to understand strings in 4d systems.

        var Handle; // File handle
        var Cadena[50];
        var po[50];
        var n := 0;


        Handle := file_Open(Filename, 'r'); //open file

        file_GetS(Cadena,50,Handle); //read caption "VESI"

        n:=1;

        while (n < 50)
        file_GetS(Cadena,50,Handle); //read line -- [Caption variable]

        file_GetS(Cadena,50,Handle); //read line -- [Value of variable]

        x := 0;
        str_Copy(po,Cadena);


        print (" NN String-", [STR]Cadena, " Po_result-- ", [STR]po, "--\n");

        n := n+1;

        wend


        -------------- Display result

        NN String-3756.56 Po_result--
        NN String-5000 Po_result--
        -----------------------------------------
        File text is:

        Values
        [Speed]
        3756.56
        [Speed2]
        5000
        ------------------------------------------

        P.S. In theory the simple function str_Copy should work.
        Jordi

        Comment


        • #5
          You seem to be demonstrating a miss-understanding of how to use strings. Have a read of app note P2006 from here http://www.4dsystems.com.au/appnotes

          So
          str_Copy(po,Cadena);
          should be
          str_Copy(str_Ptr(po),str_Ptr(Cadena));

          But you should read the app note to understand why, as there are probably other places in your code that will need fixing as well.
          Mark

          Comment

          Working...
          X