PDA

View Full Version : Searching an Open File for a Specific String


plinydogg
01-12-2007, 09:23 PM
Does anyone know if it's possible to search an open text file for a particular string? I'm trying to create a simple database without having to learn SQLite (I'm obviously not ready for that yet). For any given record, there might be two or three lines of information and I was hoping to put it all in one text file (unless this is a bad idea) and then search through that file.

For example, let's say the user wants to read the first record, I want to be able to open the file using FOpen and then obtain the info by searching for a string. Something like this:

SearchFor(file$, "###record1###")

Then, once I find the spot I want to start reading into my edit control, I would use readstring() until another string were reached. Something like (in pseudocode):

While(readstring(file$) is not equal to "###endofrecord1###)
Keep adding the next line into the edit control;
end;

Does any of this make sense?

PointOfLight
01-12-2007, 09:49 PM
If these files aren't going to be too large, I would suggest another approach:

s$ = LoadStr(FileName$, &sz$);
StrToList(s$, "\n", &data$);

val$ = "";
Find(data$, -1, -1, "#record1#", "", 0, 0, FO_POS);
Next(data$);
while(data$ <> "#endrecord#")
val$ = val$ + data$ + "\n";
Next(data$);
end;
Edit_Set(Edit$, val$);

You can look up these functions in the help file for all the details, but here's a quick synopsis. LoadStr loads an entire file into a string, then StrToList adds the file contents to a list, separating the file by a specified delimiter, in this case the carriage return. You could conceivably do this part once and just keep the list in memory for future reference.

To actually find the start of the record, you use the Find function. Supply it with the list to search, the start and end elements to search (-1 in both spots will search the whole list), the value to find, and FO_POS to search anywhere in the string. The two parameters with 0 are irrelevant to what you're doing, so if you really want to know look it up :)

Once you've found the list element that contains your record start delimiter, move to the next list element, and keep adding the list elements to a variable until you hit the first element that contains an end of record marker. Now you've got a nice string containing the record you want, and you can add it to the edit control.

Sorry for the long explanation. I hope this makes sense and is useful.

plinydogg
01-12-2007, 11:43 PM
Sorry for the long explanation? What are you talking about?!?! I NEED long explanations! I may not be a PPL pro anytime soon but a lot of what I HAVE learned so far has been due to your tireless efforts to address my newbie questions. I hope someone's paying you for this (and I promise to buy PPL once my skills are a little more up to par).

In the meantime, I hope you won't mind if I ask some more questions about what you posted above as I run into issues, which I most assuredly will.

PointOfLight
01-12-2007, 11:46 PM
Ask away. That's what I'm here for. I'm just glad you're learning something.

kornalius
01-13-2007, 12:44 AM
PointOfLight is doing an incredible job in the forums, thanks a lot!

matteo.m
01-13-2007, 04:51 AM
yes,true, i will never be tired to thank you Eric....for the nice sharing of your know-how

PointOfLight
01-13-2007, 05:26 AM
Oh stop. You all are making me blush !shy

dennishea
01-13-2007, 08:26 PM
I want to add my Thank you to the group and hope you don't over heat from the red. :)

Denny

P.s. I think you have a way of making it look easy and most of what you post does make it easier to understand.

PointOfLight
01-13-2007, 09:38 PM
That's actually nice to hear, Denny, because we wouldn't be very good at our jobs if we weren't making this easier for you.

plinydogg
01-14-2007, 10:20 PM
s$ = LoadStr(FileName$, &sz$);
StrToList(s$, "\n", &data$);

val$ = "";
Find(data$, -1, -1, "#record1#", "", 0, 0, FO_POS);
Next(data$);
while(data$ <> "#endrecord#")
val$ = val$ + data$ + "\n";
Next(data$);
end;
Edit_Set(Edit$, val$);

POL: This code works great! My problem now is using this stuff in other controls. How can I make sure that the list created with strtolist() will be available in other procedures? For example, let's say I created a button that would move to the next record, I would need to put some kind of code in the button's OnClick event, but to do this should I (1) make some of the variables in the code you used global; or (2) put all the code in the OnClick event also (so that each time the button is clicked the file will be reloaded into a list and searched again)?

PointOfLight
01-14-2007, 11:59 PM
I would make data$ (or whatever variable you end up using) global. There's no reason to mess with loading the file any more than you have to.