Announcement

Collapse
No announcement yet.

How to fill an array from a file

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

  • How to fill an array from a file

    Hello,
    I try to fill an array with strings read from a file on the SD card.
    Each field is a fixed length of 19 characters.

    Here is my code :

    HTML Code:
    func LdDataFromFile()
    
    var cpt, F, tmp ;
    
    file_Mount() ;
    F := file_Open("LDDATA.TXT", 'r');
    file_Rewind(F);
    
    cpt := 0 ;
    repeat
    file_GetS(LdData[cpt],20, F);
    cpt++ ;
    until (cpt >= (LdNbItems-1)) ;
    
    file_Close(F);
    file_Unmount() ;
    
    endfunc
    
    I also tried like this:
    
    
    file_GetS(tmp,20, F);
    LdData[cpt] := tmp ;
    An idea of the reason why it is not working ?
    Attached files LDDATA.TXT (228 B)

  • #2


    Vincent,

    I presume you've got LdData declared as an array of at least as many elements as you need? Sorry, it wasn't in your code snippet. Assuming this is correct, what gets put into LdData on each iteration?
    Hope this helps - sometimes we all need another brain to suss it out!

    Comment


    • #3


      Hi,

      HTML Code:
      var LdData[13], LdNbItems ;
      LdNbItems   := 13 ;

      Comment


      • #4


        Note a var only holds 16 bits - i.e. 2 bytes - so you'll need an array to hold a 20 character string. A single var won't cut it.

        4DGL doesn't support 2D arrays according to the documentation but you could use an array to point to a set of string arrays.

        e.g.

        var LDdata0[10],LDdata2[10], ... LDdata12[10];

        If you initialise LDdata[] to point to these arrays

        LDdata[0]=&LDdata1[0];
        ...
        LDdata[12]=&LDdata[12];

        before you run your code, you might even find the arrays LDdata0...12 populated with your strings (unless there are other bugs!)

        Note file_GetS needs the address of the variable to accept the string, not the variable itself (already taken care of in my example above)

        Simon

        Comment


        • #5


          Hello,

          In fact I am inspired by the example provided with the compiler.
          Picaso - String Functions : 0002.4dg

          HTML Code:
          var days[7];
          
          days[0] := "Monday";
          days[1] := "Tuesday";
          days[2] := "Wednesday";
          days[3] := "Thursday";
          days[4] := "Friday";
          days[5] := "Saturday";
          days[6] := "Sunday";
          
          .../...
          Attached files 0002.4dg (7.9 KB)

          Comment


          • #6
            I've never tried out the file handling stuff yet, so your problem was a good incentive to try it out.
            I think I've found the problem - you need to read from the file into a character array and then copy the character array into your string array:

            #platform "uOLED-32028-PMD3"

            var LdData[13];
            var LdNbItems;

            var str[30];


            func main()

            var cpt, F, tmp ;

            gfx_Cls();

            LdNbItems := 13 ;
            txt_Set(FONT_SIZE, FONT1);

            file_Mount() ;
            print("\t\t\tMounted\n");

            F := file_Open("LDDATA.TXT", 'r');
            print("\t\t\tOpened=",file_Error()," \n");

            file_Rewind(F);
            print("\t\t\tRewound\n");

            cpt := 0 ;
            repeat
            file_GetS(str,20, F);
            LdData[cpt] := str;
            print("\t\t\t str=", [STR]LdData[cpt], "\n");
            cpt++ ;
            until (cpt >= (LdNbItems-1)) ;

            file_Close(F);
            file_Unmount() ;

            while (1)
            wend

            endfunc


            Thanks for the challenge!

            Comment


            • #7
              Thank you for your help.

              Have you tried to access the value contained in LdData outside the loop"repeat" ?

              print("\t\t\t str=", [STR]LdData[0], "\n");
              print("\t\t\t str=", [STR]LdData[4], "\n");
              print("\t\t\t str=", [STR]LdData[9], "\n");

              always give me the last value LdData[12] :/

              Comment


              • #8
                Yea, it doesn't work

                But the following code does work:

                #CONST strSize 20
                #CONST numStr 13
                #CONST totalSize 260 // WORDS

                var LdData[totalSize];
                var LdNbItems;

                var str[strSize];

                func main()

                var cpt, F, tmp ;
                var strPtr;
                var p;

                gfx_Cls();

                LdNbItems := 12 ;
                txt_Set(FONT_SIZE, FONT1);

                file_Mount() ;
                print("\t\t\tMounted, ");

                F := file_Open("LDDATA.TXT", 'r');
                print("\Opened=",file_Error(),",");

                file_Rewind(F);
                print("\tRewound\n");

                p := &LdData[0];
                cpt := 0 ;
                repeat
                file_GetS(str, strSize, F);
                to(p); print([STR]str);
                print("\t\t\t\t str=", [STR]str, ",", str_Length(str),",",p,"\n");
                p := p + strSize;
                cpt++;
                until (cpt >= LdNbItems) ;

                file_Close(F);
                file_Unmount() ;

                print("\n");

                cpt := 0 ;
                repeat
                print("\t\t\t\t arr=", [STR]&LdData[cpt*strSize], ",", &LdData[cpt*strSize], "\n");
                cpt++ ;
                until (cpt >= LdNbItems) ;

                while (1)
                wend

                endfunc



                Comment


                • #9


                  A big thank you! This works perfectly.

                  Comment


                  • #10
                    Originally posted by kevingill
                    Thanks for the challenge!
                    Do you need a new challenge ?

                    Can we extract from a file, values of different size and not set at 20 characters ?
                    The different "fields" are separated by a EoL for example.

                    Our new LDDATA.TXT :
                    Code:
                    I.LD\nI.Set\nLD State\nI.Max\netc...
                    file_GetS("*dest", "size", "handle");
                    This function reads a line of text to a buffer (specified by "*dest") from a file at the position indicated by the associated file-position pointer and advances the pointer appropriately. Characters are read until either a newline or an EOF is received or until the specified maximum "size
                    " is reached.

                    How can I do this ?

                    Comment


                    • #11


                      Vincent,

                      I guess you could pad out the fields to 20 characters.
                      Alternatively, you could keep another array that keeps track of the length of each field in the first array. May be best to wrap this all up into a set of functions that give/control access to the storage you require.
                      You could start off a "Useful functions" post!

                      I presume the file_Gets works OK with CR?

                      Comment


                      • #12


                        In fact your function works well with fields < 20 chars and

                        But if I want to print something like this : LD State: OFF
                        with this code :

                        print ([STR] &LdData[2*strSize],": ","OFF");

                        I get this:

                        LD State
                        : OFF

                        I'll try your idea of an array with length of each field.

                        Thank you.

                        Comment


                        • #13
                          Hmm, I'd try a str_Find to find "\n" and then replace it with a space or "\0". This will then give you a string that hasn't got a CR and your print example should work fine.

                          Comment

                          Working...
                          X