Announcement

Collapse
No announcement yet.

Saving strings from external text file to an array

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

  • Saving strings from external text file to an array

    Hello!

    I'm having some trouble reading and storing string values from a .txt file onto a string array for later use. I've managed to read, apply and modify numeric values from two other different .txt files (a Config file and a Time file that stores program use time) but I'm having no luck when it comes to storing strings. The idea is to store several files into the SD card to localize most of the text in the program (hopefully I'll be able to use the String Widget for that) reading the selected language file.

    For clarity purposes, all .txt files are formatted as follows:
    Code:
    variable=value;
    Made that way so in case someone has to modify the values, they'll know what to touch (somewhat). On the other hand, it means I can't read the lines directly since I'd grab unwanted data too, so instead I read each char and save the ones between '=' and ';' ... Or that was the idea (though this approach does work on files with numeric characters only, which is what baffles me. It's true that I convert the string to int in that one, though).

    Here's what I've got so far:
    Code:
    //Read Language File
    func apply_language()
        print("\nReading language file.\n");
        pause(1000);
        var handle;  //Handle
        var info[100]; //Buffer
        var res;    //Results
        var count:=0;  //Byte counter
        var p;  //String pointer
        var saveValue:=0; //If =1, save value
        var currentValue:=0; //Save value index
        var val[20]; //Buffer for new values
        var vp; //String pointer for values extracted
        var invalid:=0;
        switch(language)
            case 1:   //ENGLISH
                if(file_Exists("ENG.txt"))
                    handle:=file_Open("ENG.txt",'r');
                    count:=file_Read(info,200,handle);
                else
                    invalid:=1;
                endif
                break;
            case 2:   //SPANISH
                 if(file_Exists("CAS.txt"))
                    handle:=file_Open("CAS.txt",'r');
                    count:=file_Read(info,200,handle);
                else
                    invalid:=1;
                endif
                break;
            case 3:   //GERMAN
                 if(file_Exists("DEU.txt"))
                    handle:=file_Open("DEU.txt",'r');
                    count:=file_Read(info,200,handle);
                else
                    invalid:=1;
                endif
                break;
            default:
                invalid:=1;
                break;
        endswitch
        if(invalid == 0) //If we found the language file
            p:= str_Ptr(info);
            vp:= str_Ptr(val);
            while(str_GetC(&p,&res))
                if(saveValue == 1) //Saving chars
                    str_PutByte(vp,res);//Add char to buffer
                    vp++;
                    if(str_MatchI(&p,";")) //If we find ';', we're at the end of assignment
                        saveValue:=0;//Stop saving chars
                        langArray[currentValue]:=val;
                        currentValue++; //Goto next value
                        mem_Set(val,'',40); //Clear buffer
                        vp:=str_Ptr(val); //Reset pointer for extracted value
                    endif
                endif
                //Begin saving value if we find '='
                if(str_MatchI(&p,"="))
                    saveValue:=1;
                endif
            wend
            file_Close(handle);
            print("Done reading Language file. Printing content:");
            var x:=0;
            while(x<28)
                print("\n",x,": ",[STR]langArray[x]);
                x++;
                pause(500);
            wend
            pause(1000);
        endif
    endfunc
    Thanks in advance!

    EDIT: All vars not declared in the chunk of code are declared outside: language goes from 1 to N; langArray is an array to hold up to 29 strings (size of the language file).
    Last edited by MotoC450; 12th December 2016, 08:59 PM.

  • #2
    Nevermind that last question, I went ahead and made it easy on myself - Language files now contain raw text without indications so I can use file_GetS to cycle through all content.

    In case someone may find it helpful:
    Code:
    func apply_language()
        var handle;  //Handle 
        var invalid:=0; 
        switch(language)
            case 1:   //ENGLISH
                if(file_Exists("ENG.txt"))
                    print("\nReading ENG language file.\n");
                    handle:=file_Open("ENG.txt",'r');
                else
                    invalid:=1;
                endif
                break;
            case 2:   //SPANISH
                 if(file_Exists("CAS.txt"))
                    print("\nReading CAS language file.\n");
                    handle:=file_Open("CAS.txt",'r');
                else
                    invalid:=1;
                endif
                break;
            case 3:   //GERMAN
                 if(file_Exists("DEU.txt"))
                    print("\nReading DEU language file.\n");
                    handle:=file_Open("DEU.txt",'r');
                else
                    invalid:=1;
                endif
                break;
            default:
                invalid:=1;
                break;
        endswitch
        pause(100);
        if(invalid == 0) //If we found the language file
            var r[40];  //Buffer to store the result
            var c:=0;  //Array index counter
            while(c<29) //We know each language file has 29 lines, and langArray has been defined with that length
                file_GetS(r,80,handle);  //Save string into buffer 'r'
                langArray[c]:=r;      //Save buffer into current array position
                mem_Set(r,'',80);    //Clear buffer to prevent 'garbage' characters
                c++;         //Goto next array position
            wend
            file_Close(handle);
            print("Done reading Language file.");
            pause(100);
        endif
    endfunc
    Had to use the extra 'r' buffer, since attempting to save info directly onto the array caused all retrieved text to be the file name.

    Comment

    Working...
    X