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


    • #3
      Hi Vince,
      I would like to use your example to solve my problem, but it doesn't work.
      I need to call a subroutine that read a single line text in a file, passing the name of the file.
      The subroutine is:
      Code:
      func CaricaFrase(var NomeFile, var Indice)
      var private Frase[20];
      var private pFrase;
          pFrase := str_Ptr(Frase);
          to (Frase); print ("____");
          if (file_Exists(NomeFile))
              HandleF := file_Open(NomeFile, 'r');
              if (!HandleF)
                  print("Unable to open ", [STR]NomeFile, " \n");
              else
                  Res := file_Size(HandleF, &i, &n);       //calcolo lunghezza file -> n
                  file_Rewind(HandleF);
                  for (i := 0; i < Indice; i++)
                      pFrase := str_Ptr(Frase);
                      file_GetS(Frase, n, HandleF);
                  next
                  Res := file_Close(HandleF);
              endif
          else
              print([STR]NomeFile, " does not exists\n");
          endif
          return pFrase;
      endfunc
      Now, if I call the subroutine with:
      Code:
              p := CaricaFrase("TitIta.txt", 1);
              str_Printf(&p, "%s");
      All works properly,
      But, if i call the subroutine with:
      Code:
              to(NomeFile); putstr("TitIta.txt");   // or "to(NomeFile); print("TitIta.txt");" give the some result
              p := CaricaFrase(NomeFile, 1);
              str_Printf(&p, "%s");
      I always reach the error "TiTIta.txt does not exists".
      I don't understand where is the error, whereas the error message shows the correct file name.
      Can you help me to understand and solve the problem?
      I need this "variable" name because I need to change some characters in the filename, according to the language.
      I can save in SD a lot of text with similar names, changing only three characters (Ita -> Eng -> Fra -> and so on) I give to the operator the same message translated in appropriate language.
      Best regards, Valter

      Comment


      • #4
        Hi Valter,

        On your subroutine, you need to add string pointer str_Ptr(NomeFile). Try this code.
        HTML Code:
        func CaricaFrase(var NomeFile, var Indice)
        var private Frase[20];
        var private pFrase;
        var i, n;
        var ptr;
            pFrase := str_Ptr(Frase);
            ptr := str_Ptr(NomeFile); // string Pointer
            to (Frase); print ("____");
        
            if (file_Exists(ptr))          //change from NomeFile
                HandleF := file_Open(ptr, 'r');         //change from NomeFile
                if (!HandleF)
                    print("Unable to open ", [STR]NomeFile, " \n");    
                else
                    Res := file_Size(HandleF, &i, &n);       //calcolo lunghezza file -> n
                    file_Rewind(HandleF);
                    for ( i := 0; i < Indice; i++)
                        pFrase := str_Ptr(Frase);
                        file_GetS(Frase, n, HandleF);
                    next
                    Res := file_Close(HandleF);
                endif
            else
                print([STR]NomeFile, " does not exists\n");
            endif
            return pFrase;
        endfunc
        Hope this helps.

        Best Regards,
        Last edited by frsantiago; 3 weeks ago.

        Comment


        • #5
          Hi frsantiago,
          Your code works fine!
          Problem solved, many thanks.
          Best regards, Valter

          Comment


          • #6
            Hi frsantiago
            Only a clarification:
            You suggest me to change the #7 line from:
            print("Unable to open ", [STR]NomeFile, " \n"); to
            print("Unable to open ", [STR]ptr, " \n"); //change from NomeFile ok, done! but I can't create the error, so I can only suppose this work.
            If I do the same to line #18, from:
            print([STR]NomeFile, " does not exists\n"); To:
            print([STR]ptr, " does not exists\n"); I can create the error, and, with the original statement (you don't change) the error appear correctly, while, with the modified statement, the name of the file is not icluded in the error message.
            I don't understand this. This clarification is not essential, as everything works well, it is only to understand well.
            Best regards, Valter

            Comment


            • #7
              Hi Valter,

              Good Day to you.

              Thank you for clarification.

              It is a find and replace error while trying to debug your code which I overlooked when it was done. It will not work either if you create an error.

              Just use print("Unable to open ", [STR]NomeFile, " \n");and
              print([STR]NomeFile, " does not exists\n");

              You can refer to DIABLO16 - Graphics Processor - internal Functions Manual regarding str_Ptr() for reference.

              Hope this is helpful.

              Best Regards,
              Ferdinand

              Comment


              • #8
                Hi Ferdinand,
                thanks for the reply, now it's clearer to me.
                Best regards, Valter

                Comment

                Working...
                X