Announcement

Collapse
No announcement yet.

Acces to file on SD card

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

  • Acces to file on SD card

    Hello,

    Is it posible to read data from the SD-card.
    Like this pseudo code:

    Open file
    While(not(eof))
    read(value1,value2)
    <processing the data>
    Wend
    Close File


    my file contain the next format as an example

    0 1
    2 -4
    4 6

    etc

    Please can you when possible with an example.

    Thanks

    Jan



  • #2
    I don't normally use text, as a way of saving information if it needs to be reread, as saving and loading it as binary is much faster, easier and 'foolproof', but the following should work

    Code:
    func main()
        var hndl, strx[10], i, j, p ;
        putstr("Mounting...\n");
        if (!(file_Mount()))
            while(!(file_Mount()))
                putstr("Drive not mounted...");
                pause(200);
                gfx_Cls();
                pause(200);
            wend
        endif
    
        hndl := file_Open("test.txt", 'r') ;
        if (!hndl)
            print("Open Failed") ;
            repeat
            forever
        endif
        repeat
            file_GetS(strx, sizeof(strx)*2, hndl) ;     // replace with your code
            if (strlen(strx))                           // if last line is 'null' we need to ignore it
                print([STR] strx, "\n") ;
                p := str_Ptr(strx) ;
                str_GetW(&p, &i) ;
                str_GetW(&p, &j) ;
                print("i=", i, " j=", j, "\n");
            endif
            if (file_Error()) break ;                   // if last line was terminated by EOF file_Error will be set, and we processed that line above
        forever
    
        repeat
        forever
    endfunc
    Mark

    Comment


    • #3
      Thanks,

      This is helpful.
      I am triggered about your remark of using binary format.
      Please can you explain me how to use that with the example

      0 1
      2 -4
      4 6

      Jan


      Comment


      • #4
        Code:
        var inidat[6] ;     // ini file data
        
        func main()
            var hndl, i, j, p ;
        print("\n\n\n") ;
            putstr("Mounting...\n");
            if (!(file_Mount()))
                while(!(file_Mount()))
                    putstr("Drive not mounted...");
                    pause(200);
                    gfx_Cls();
                    pause(200);
                wend
            endif
        
            file_Erase("test.bin") ;                // open for write will fail if file exists
            hndl := file_Open("test.bin", 'w') ;
            if (!hndl)
                print("Open for Write Failed") ;
                repeat
                forever
            endif
            inidat[0] := 0 ;                        // setup dummy ini file
            inidat[1] := 1 ;                        // we could use enumerated constants to give each variable a name
            inidat[2] := 2 ;                        // rather than trying to remember what each number is for
            inidat[3] := -4 ;
            inidat[4] := 4 ;
            inidat[5] := 6 ;
            file_Write(str_Ptr(inidat), sizeof(inidat)*2, hndl) ;    // write to inidat for size of inidat, note we use a string pointer to write from
            file_Close(hndl) ;                                       // that's it
        
            for (i := 0; i < sizeof(inidat); i++)                    // clear the data, to prove the file_read works
                inidat[i] := i ;
            next
        
            hndl := file_Open("test.bin", 'r') ;                     // open for read
            if (!hndl)
                print("Open for read Failed") ;
                repeat
                forever
            endif
            file_Read(inidat, sizeof(inidat)*2, hndl) ;              // read from inidat for size of inidat
            file_Close(hndl) ;                                       // close
            for (i := 0; i < sizeof(inidat); i++)                    // print what we read
                print(inidat[i]," ") ;
            next
        
            repeat
            forever
        endfunc
        Mark

        Comment

        Working...
        X