Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write to file and seach record in it
#1
Hello Gintaras, hello all,

I use a file to store informations.
Each record is composed of 3 string variables.
The numbers of records is from 100 to 5000 (maximum).

*** To write records, if gather information (3 items per record at the moment, could be 5 later), add them to a variable and then write them down in the file

str datas
datas.addline("Item1")
datas.addline("Item2")
datas.addline("Item3")
datas.setfile("c:\myfile.txt" -1 -1 1)

myfiles.txt then is:
________________
Item1
Item2
Item3

Item1-1
Item2-1
Item3-1

Item1-3
Item2-3
Item3-3

etc etc
-----------------------

*** Searching

To search in the file, i simply seach for a string matching Item2 (i have a routine to seach for each item, but it's the same code except
the matched line search)


str queue.getfile("c:\myfile.txt")
str s
str f="texttofind"
int i(1)

** search for matching line
foreach s queue
if(!StrCompare(s f)) break
i+1

str infos

** get line before and after to get record missing infos of matched pattern
for _i i-1 i+2
_s.getl(queue _i)
infos.addline(_s)

** Choose information needed through a list
_i=list(infos "Quoi copier?" "Copier les informations" 0 0 0 0 1)
ARRAY(str) a=infos
_s=a[_i-1]

finally, _s handles what I needed to find.

My question : this code works very well, but i'm sure it can be optimized by using some other technique.

How can I improve it for speed, memory usage and efficiency?

Thanks.
#2
If it is a text file, difficult to improve something. I would use Sqlite database.
Test speed etc with max expected file size, then you'll know whether need optimizations.
#3
Nope, txt file is enough.
If code OK, i'll stick to it.
Thanks.
#4
This is >8 times faster than with foreach, but it gives character index not line index.
i=findw(queue f 0 "[]")
if(i<0) end "not found"


Forum Jump:


Users browsing this thread: 1 Guest(s)