Announcement

Collapse
No announcement yet.

Trouble copying an array to a string

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

  • Trouble copying an array to a string


    I am wondering if someone can point me to how this is best done.

    My objective is to write a function that will read text from a file and return the string read.

    The following code has a memory leak in that I can't run the line
    mem_Free(array);
    without it messing up the result returned from this function.

    The commented out parts are my many failed attempts to do this some other way.

    Here is the code:

    Code:
     #platform "uOLED-32028-P1T" 
    /*************************************************
    * Filename: Read_Text_File.4dg
    * Created: 24/02/2010
    * Author: Pro AV
    * Description: Reads text from a file and returns
    the result.
    **************************************************/
    
    /*----------------------------------------------------------------*/
    /*Read text of /str_length/ from /src_file/ and return the string -----*/
    /*----------------------------------------------------------------*/
    func Read_Text_File(var src_file, var str_length)
    
    var n, ch;
    var TextFile;
    var array;
    var result;
    array := mem_AllocZ(str_length);
    
    if(!file_Mount())
    result := "Cannot mount uSD card";
    else
    //print("uSD mounted\n");
    /* If the configuration file does not exist */
    if(!file_Exists(src_file)) 
    result := "File does not exist\n";
    else
    /* load values from TXT file */
    TextFile := file_Open(src_file, 'r');
    /*
    repeat
    ch := file_GetC(TextFile);
    str_Append(array); putch(ch); //append a character from the src_file to array 
    until(!isprint(ch) || n++ >= str_length-1);
    */
    file_GetS(array, str_length, TextFile);
    file_Close(TextFile);
    endif
    endif
    
    /* finished with file system */
    file_Unmount(); 
    
    //to(result), putstr(array);
    //mem_Copy(array, result, str_length);
    //result := print([STR]array);
    if(str_Length(result) <= 0) result := array;
    //mem_Free(array);
    return(result);
    
    endfunc
    
    /*----------------------------------------------------------------*/
    /*MAIN CODE-----------------------------------------------------*/
    /*----------------------------------------------------------------*/
    func main()
    var my_text;
    
    my_text := Read_Text_File("SCROLL.TXT", 31);
    
    print("\nText Read:\n",[STR]my_text,"\n");
    //print("\nText Read:\n",my_text,"\n");
    repeat forever
    endfunc
    Perhaps my error is in not understanding correctly when to use a string pointer or something like that?

    Thanks,
    --------Steve


    P.S. It seems the "CODE" tags mess up some of the symbols. Namely the > and < ones. I will attach the file as well...

  • #2


    [quote]
    [HTML]if(str_Length(result)

    Comment


    • #3


      The result is empty unless there is an error. The line
      if(str_Length(result)

      Comment


      • #4


        Actually you don't need the variable array, do you? Do the mem_alloc for result and then, in case no error occurred, just load the values from the file directly into result. If an error occured, do a mem_Free and return 0. Am I missing somethin?

        Comment


        • #5


          The issue I have with that rautenberg, is that I end up allocating memory that I don't free up. If, perchance, this subroutine was called many times in a program, it would end up wasting memory without freeing it up at the end.

          Although it does not really matter much to my program, I believe it's bad practice to allocate memory that you don't free up...

          I also can't load the text strait into the variable 'result' without allocating it some memory whereas I can just assign a string value to a variable without reserving memory permanently for the string.

          Comment

          Working...
          X