Announcement

Collapse
No announcement yet.

to(str) and str_Length

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

  • to(str) and str_Length

    Hi to all
    I would like to write a line on a file on SD card:
    the inizialization of the SD card is copied from the examples.
    The part of program that doesn't work is:
    var Res, n, HandleFile, String[100];
    if (file_Exists("Mario.txt"))
    print("Append on Mario.txt");
    to(Stringa); putstr("Append on existing file");
    n := str_Length(Stringa);
    print("\n", n, " characters (23)");
    HandleFile := file_Open("Mario.txt", 'a');
    file_Write(Stringa, n, HandleFile);
    file_PutC(13, HandleFile);
    Res := file_Close(HandleFile);
    else
    print("Create Mario.txt");
    to(Stringa); putstr("File just created");
    n := str_Length(Stringa);
    print("\n", n, " characters (17)");
    HandleFile := file_Open("Mario.txt", 'w');
    file_Write(Stringa, n, HandleFile);
    Res := file_Close(HandleFile);
    HandleFile := file_Open("Mario.txt", 'a');
    file_PutC(13, HandleFile);
    Res := file_Close(HandleFile);
    endif
    Now:
    - I start without Mario.txt in the sd card
    - if Mario.txt doesn't exist, n is calculated as 4 characters and (of course) "File just created" is not writed to file, I find only "/.<CR>"
    - if Mario.txt exists, n is calculated as 31 characters instead of 23, and I read in the file "/.<CR>/...Append on existing file>CR>"
    - if I delete Mario.txt with file_Erase(Mario.txt), the next time I try to write it, n is calculated as 25 characters, and the phrase is correct, wit the add of "/.." at the begin of the String
    - other non-printing characters are also added.
    It seems that to(str) doesn't load correctly and consequently str_Lenght give an erroneus value.
    Did I forget any initialization?
    Where am I wrong?
    Many thanks, Valter

    _

  • #2
    Hi Valter,

    - if Mario.txt doesn't exist, n is calculated as 4 characters and (of course) "File just created" is not writed to file, I find only "/.<CR>"
    - if Mario.txt exists, n is calculated as 31 characters instead of 23, and I read in the file "/.<CR>/...Append on existing file>CR>"

    It seems that to(str) doesn't load correctly and consequently str_Lenght give an erroneus value.
    Following the code, you haven't assigned a String Pointer to compute for the string length, and on using File_Write command what you used is the buffer instead of the string pointer. Please refer to the Diablo16 Reference Manual for proper implementation of the functions.

    Can you try this code, and see if it works for you.

    Code:
      var Res, n, HandleFile, Stringa[100];
      var ptr;
    
    if (file_Exists("Mario4.txt"))
        print("Append on Mario4.txt");
        to(Stringa); putstr("Append on existing file");
        ptr:=str_Ptr(Stringa); // assign a pointer
        n := str_Length(ptr);
        print("\n", n, " characters (23)");
    
        ptr:=str_Ptr(Stringa); // direct the pointer to first address
        HandleFile := file_Open("Mario4.txt", 'a');
        file_Write(ptr, n, HandleFile); //source is byte/string pointer
        file_PutC(13, HandleFile);
        Res := file_Close(HandleFile);
    
    else
        print("Create Mario4.txt");
        to(Stringa); putstr("File just created");
        ptr:=str_Ptr(Stringa); // assign a pointer
        n := str_Length(ptr);
        print("\n", n, " characters (17)");
    
        ptr:=str_Ptr(Stringa); // direct the pointer to first address
        HandleFile := file_Open("Mario4.txt", 'w');
        file_Write(ptr, n, HandleFile); //source is byte/string pointer
        Res := file_Close(HandleFile);
    
        HandleFile := file_Open("Mario4.txt", 'a');
        file_PutC(13, HandleFile);
        Res := file_Close(HandleFile);
    endif
    Best Regards,
    Kevin

    Comment


    • #3
      Hi Kevin
      This work fine!
      I read the manual but I don't understand the use of the pointer. Your example and comments are clear!
      Thanks again, Valter

      Comment


      • John Kevin
        John Kevin commented
        Editing a comment
        Hi Valter,

        I am glad that it helps you. Should you need any further assistance, please don't hesitate to ask.

        Best Regards,
        Kevin
    Working...
    X