Indexed files - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Indexed files (/showthread.php?tid=2140) Pages:
1
2
|
RE: Indexed files - mnrvovrfc - 11-04-2023 Use ```SPACE$(1)``` instead of ```CHR$(0)```. What if you need to port the program to Freebasic or other BASIC that can't handle ASCII0 as well? Yes I know it's a placeholder but ```SPACE$()``` was created especially for allocating a fixed number of characters into a variable-length string. Alternatively ```Z$``` could have been declared as ```STRING * 1``` with the same result. RE: Indexed files - PhilOfPerth - 11-04-2023 Thanks to those who tried to help me, but... I guess I'm too old or too dense, or both, to make sense of any of the examples. I can't get my head around the Type structures etc. So I guess I'll go back to my (very primitive) original plan (crawls back into cave and pulls bush across doorway). The responses were very much appreciated. RE: Indexed files - bplus - 11-04-2023 (11-04-2023, 07:44 AM)PhilOfPerth Wrote: Thanks to those who tried to help me, but... You know I was thinking after my posts there is a really easy way to do this: Just load the file into an array and then you have an indexed access! When done processing whatever with array just load it back into a newly rewritten file. Then you have indexed access without any fixing up of your file as long as 1 file line is 1 record. The array will have to be dynamic which means you can change the size of the array ie for adding new records = lines So don't say DIM MyArray say REDIM MyArray that sets up an array to be dynamic. THEN you can use REDIM _PRESERVE to make the array bigger to add more records. Just keep track total Records or lines = UBOUND of array. Also use base 1 array so REDIM MyArray(1 to MaxRecords) so the Ubound of array IS the number of records you have, no fiddling with +-1 like with 0 based arrays. Piece of Cake man! RE: Indexed files - SMcNeill - 11-04-2023 (11-04-2023, 07:44 AM)PhilOfPerth Wrote: Thanks to those who tried to help me, but... Give this a shot -- it's as simple as they come, with no Types or anything fancy. Just a file, a string, and some minor GET/PUT statements at work: Code: (Select All)
RE: Indexed files - Dimster - 11-04-2023 Hi Phil Back on the old site I had a similar discussion with Steve on a project I was trying to get off the ground. I recall the topic we were using was called "Random Access to a Sequential File". The major stumbling block was finding a particular record in a sequential file. The first thing I remember Steve pointing out is that it can't be done accurately without knowing the length of each record. It's easy to determine the size of each variable within a record (ie a Long = 4 byte and an Integer = 2 etc). The challenge is the string variables. Back then Steve pointed out that the safest was to determine what the words were in the longest string and to be sure to pad with spaces those string words which were less than the longest. If you have another way to calculate the exact length of each string's length, then you wouldn't need any spacing. It is a simple matter then of opening the file once and use Seek #1 (or Seek #100 whatever the file number you have chosen to open) to position the search at the beginning of the sequential file, Record 1 would be record 1 (ie Seek #1, 1). If memory services me right, the pointer finding the records will always end on the last data item so to get to the next record you have to add one (assuming you want the next record) The way the random access worked, and I'm struggling with my memory here - say you started with search for record 22, then it was Seek #1,(record length *21+1). and the next search was for record 14, you would reset the search back to the beginning and do the same thing (Seek #1,1 to get back to the beginning) then Seek #1,(record length *14+1) Sorry Phil if this is not what you were thinking of, and of course if I'm way out to lunch on the structural layout of the coding. RE: Indexed files - bplus - 11-04-2023 Well the nice thing about Random Access is you deal directly with file so no worries about Saving after Open it but you do have to deal with fixed length lines. So I made a very, very, very simple line editor for text files: You enter a number not starting with 0 for line to see / edit You enter a non number and rewrite the line or you enter escape to quit and save the data to file. I think I have all bugs out, let me know if you found one: Code: (Select All) _Title "Random Access text file" ' bplus 2023-11-04 Come to think @PhilOfPerth why not just use an editor? RE: Indexed files - Kernelpanic - 11-04-2023 An example of a random access database can be found here (Hello, Pete! ): Select Cases & Database RE: Indexed files - PhilOfPerth - 11-05-2023 Thanks again to all. You got me back out of my cave! @bplus: I've gone through your Line Editor, line by line, and commented an explanation of what you did, and finally got it! It has functions in it that I probably won't need (Saving and Backup), but it does what I wanted - I can search for and read any line of my file, randomly. Thanks again. RE: Indexed files - bplus - 11-05-2023 (11-05-2023, 12:21 AM)PhilOfPerth Wrote: Thanks again to all. You got me back out of my cave! If you just want a reader then yes, no need to save or backup. But if you want to add and edit lines you want to save your work and back up your data from old file first just in case something happens to new data file. But again talking simplest ways to go about all this is just use a Word Processor, maybe number the lines yourself but mine has line numbers like QB64 IDE. That way you can view more than one line at a time. You could also load a text file into an array to sort the file by lines, say if all the lines start with a date. With my little line editor that would not work because lines start with numbers but using a WP and starting all lines with a Date: Year-MM-DD format you can order all your entries like I do with my accounting accounts transactions. RE: Indexed files - PhilOfPerth - 11-05-2023 (11-05-2023, 01:55 PM)bplus Wrote:(11-05-2023, 12:21 AM)PhilOfPerth Wrote: Thanks again to all. You got me back out of my cave! Thanks bplus; got it. |