YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Read and Write Keys from/in File
KevY
#1 Posted : Monday, August 1, 2016 3:24:04 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 8/1/2016(UTC)
Posts: 1

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
I searched everywhere but couldn't find a answer so I tried to make my own Read values of keys from a file and set them I havent made the set one yet but there is a problem with my reading one, It wont stop reading until another = comes.. I dont know how should I stop it in Squirrel. This is my code
Code:

function get_key(file, key)
{
    local r = file_readAll(file);
    local read = split(r, " = ");
    local pos = 0;
    for(pos = 0; pos < read.len(); pos++)
    {
        if(read[pos] == key) return read[pos+1];
    }
    return 0;
}

So if I read something like
Code:

UserName=Test_Bot_93829
Password=Never123

So it will read it like this:
Code:

Test_Bot_93829
Password

I dont want it like this, I only want it to read the value of the key not to read the name of next key with the value of the old key.. Also can I have a even better explanation and sample code or maybe you can create sorry I am still a beginner, Can anyone provide me a proper one or make it for me please.. I have been searching 13 hours.
absence
#2 Posted : Monday, August 1, 2016 1:37:37 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 109
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 1 times
Was thanked: 10 time(s) in 10 post(s)
try it this way:

Code:


function read_ini(file)
{
  local r=file_readAll(file);  //make sure it will read newlines!!!
  local lines = split(r, "\n"); r=null ;
  local ret={} ;
  foreach (line in lines) {
    local tmp=line.find("-") ;
    if (tmp!=false) {
        local key=strip(line.slice(0,tmp)) ;
        local val=lstrip(line.slice(tmp)) ;
        ret[key]<-val ;
    }
  }
}

//example:
local inifile=read_ini("myfile.ini") ;  //returns a TABLE with key and value according to ini file
local mykey=null ;
if ("mykey" in inifile) mykey=inifile.mykey ;



NOTE: Again, this is done by heart without really trying, so it maybe won't compile or run properly. But it shall give you an idea of how to handle the given problem:
As it seems to be an inifile, split the file into LINES first. Then search the lines for a = character, and split the line at that character manually (slice)
Create a table from the keys and values to return. Now you need to read the file ONCE and can lookup each key in the table without re-reading the file.

(I presume your read_fileAll function will return contained linefeed characters)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.156 seconds.