Announcement

Collapse
No announcement yet.

file_GetS and str_GetByte

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

  • file_GetS and str_GetByte

    Hi

    I'm baking my noodle trying to get this code working within a larger Visi program

    I've tried to test it in a smaller 4dg program that should be easier to follow.

    the prog reads a line from a file on the sd card (just some test text) using the file_GetS function and works fine.

    I need to parse the string using the tabs as break points .

    the for next loop should print a byte for byte copy of the original string on the next line ? but only prints 40 ish of the 56 of the origonal chars.

    it seems so straightforward I can't figure out whats going wrong ?

    Any insight would be appreciated

    Cheers

    Garry



    #platform "Gen4-uLCD-43DCT-CLB"


    #inherit "4DGL_16bitColours.fnc"

    func main()

    gfx_ScreenMode(LANDSCAPE) ; // change manually if orientation change


    var RECIPERECORD[50]; // THE FULL RECIPELINE FROM THE ORIGINAL TYPED FILE
    var RECIPERECORDPTR;
    var myfilehandle;
    var charsinline;
    var disk;
    var mychar;
    var x;
    var lenofstr;

    touch_Set(TOUCH_ENABLE);
    gfx_ScreenMode(LANDSCAPE);
    putstr("Mounting...\n");
    if (!(disk:=file_Mount()))
    while(!(disk :=file_Mount()))
    putstr("Drive not mounted...");
    pause(200);
    gfx_Cls();
    pause(200);
    wend
    endif



    gfx_Cls();



    RECIPERECORDPTR := str_Ptr (RECIPERECORD); //Define Pointer

    myfilehandle := file_Open ("tBook1.txt",'r') ; //read

    charsinline := file_GetS(RECIPERECORDPTR, 70, myfilehandle);//RETURNS NUMBER OF CHARS RETRIEVED UP TO NEWLINE

    print("Chars Read=",charsinline,"\n");

    lenofstr :=strlen(RECIPERECORDPTR);

    print("length=",lenofstr,"\n");

    print([STR]RECIPERECORDPTR,"\n");//FINE UP To HERE


    for (x :=0 ; x != strlen(RECIPERECORDPTR) ; x ++)

    mychar := str_GetByte(RECIPERECORDPTR + x );
    //pause(20);
    print([CHR]mychar);

    next

    print("\n");
    print("x=",x);

    file_Close ("tBook1.txt");


    //print("Hello World") ; // replace with your code

    repeat // maybe replace
    forever // this as well

    endfunc
    Attached Files

  • #2
    Hi Garry, welcome to the forum!

    Kindly refer to the code below. Read the comments to be guided.
    Code:
    var fileName[10];        // filename string buffer that can handle 10*2 chars
    var readBuff[50];
    var file;
    var ptr;
    
    func main()
    
        putstr("Mounting...\n");
        while(!(file_Mount()))                    // use this routine in projects with no gci file ...
            putstr("Drive not mounted...");        // ... to ensure FAT16 file functions to run properly
            pause(200);
            gfx_Cls();
            pause(200);
        wend
        gfx_Cls();
        gfx_ScreenMode(LANDSCAPE);
        touch_Set(TOUCH_ENABLE);
    
        to(fileName); print("tBook1.txt");            // sends filename string to fileName string buffer
        ptr := str_Ptr(fileName);                     // byte pointer for fileName string
    
        createFile() ;                                // used to create and write to txt file, comment-out if file already created
        //readFile();                                 // uncomment to read the data in txt file
    
        repeat
    
        forever
    endfunc
    
    
    func createFile()
        file_Erase(ptr);                                     // deleting previously created file
        file := file_Open(ptr, 'a');                        // opens the file in FILE_APPEND mode
        file_PutS("HELLO WORLD\nand\nHELLO TO YOU!", file);                // appends HELLO WORLD string
        file_Close(file);                                   // close file to save file changes
        txt_MoveCursor(0,0);
        print("New file created");
    endfunc
    
    func readFile()
        txt_MoveCursor(0,0);
        if(file_Exists(ptr))                        //checking if file exists using the pointer
            print("File exists          ");
            file := file_Open(ptr, 'r');             // opening file to read
            file_Read(readBuff, 100, file);          // read 100 chars and store in readBuff
            txt_MoveCursor(2,0);
            print([STR]readBuff);                    // print read txt data
        else
               print("File does not exists");
        endif
    
    endfunc
    Also, note that using file_GetS(...) will stop reading when any of the following conditions are true:
    A) It has read n-1 bytes (one character is reserved for the null-terminator)
    B) It encounters a newline character (a line-feed in the compilers tested here)
    C) It reaches the end of file
    D) A read error occurs.

    Hope this helps.
    Best regards,
    Vince_4D

    Comment

    Working...
    X