New topic: Determine end of text file?
<http://forums.realsoftware.com/viewtopic.php?t=41608> Page 1 of 1 [ 10 posts ] Previous topic | Next topic Author Message DrDungeon Post subject: Determine end of text file?Posted: Mon Nov 28, 2011 12:36 pm Joined: Thu Jan 03, 2008 11:29 am Posts: 99 Hi guys! I'm writing a little app, part of which includes an ever-growing text file, using AppendToTextFile Basically, it stores sentences input by the user, and separates each sentence with an *. // store is a string sentence, " I am happy to see you today " f = Volume(0).Child("sal12000").Child("robby.dat") slotname = store + "*" // slotname is just for storing with the * delimiter. Delimits each sentence. // slotname is now " I am happy to see you today *" t=f.AppendToTextFile t.Write(slotname) t.Close This works fine. But the idea is that it will grow over time to dozens, or even hundreds of entries. I know how to read them back in, but what I don't know is how to determine how many are there? For example, say I have 10 entries in there. Using my system with the *, how could I have the program determine that there are 10 sentences in there - where the last * is? This is probably pretty simple but I can't seem to figure it out. Thanks, -Robert Top ZULU Post subject: Re: Determine end of text file?Posted: Mon Nov 28, 2011 12:48 pm Joined: Mon Jul 31, 2006 1:44 am Posts: 1266 Something like this? Code: dim s as string = "*Hello*say*Oskar*Tore*Hello again*Sven*Joakim" dim k() as String = Split(s,"*") dim count as Integer = UBound(k) break _________________ REAL Studio 2011 r3 OS X 10.7 Lion Top Jason_Adams Post subject: Re: Determine end of text file?Posted: Mon Nov 28, 2011 12:51 pm Joined: Fri Nov 10, 2006 4:10 pm Posts: 1059 Location: Michigan, USA Can the user put in an asterisk themselves? You could use a TextInputStream, set the delimiter to "*", and count the ReadLines until EOF; or you could ReadAll and use CountFields. But I assume the user can't enter the character themselves within the sentence. Hope this helps. _________________ Windows 7 Ultimate x64 Windows XP Pro SP3 Ubuntu 11.04 via Virtual Box RS Enterprise 2011r3 "Christianity has not been tried and found wanting; it has been found difficult and not tried." - G.K. Chesterton Top Jason_Adams Post subject: Re: Determine end of text file?Posted: Mon Nov 28, 2011 12:51 pm Joined: Fri Nov 10, 2006 4:10 pm Posts: 1059 Location: Michigan, USA ZULU wrote:Something like this? Code: dim s as string = "*Hello*say*Oskar*Tore*Hello again*Sven*Joakim" dim k() as String = Split(s,"*") dim count as Integer = UBound(k) break I think he's referring to reading from a file, but in this case I'd use CountFields instead of creating an unused array. _________________ Windows 7 Ultimate x64 Windows XP Pro SP3 Ubuntu 11.04 via Virtual Box RS Enterprise 2011r3 "Christianity has not been tried and found wanting; it has been found difficult and not tried." - G.K. Chesterton Top ZULU Post subject: Re: Determine end of text file?Posted: Mon Nov 28, 2011 1:21 pm Joined: Mon Jul 31, 2006 1:44 am Posts: 1266 I prefer using split, it is a lot faster when using big strings ... _________________ REAL Studio 2011 r3 OS X 10.7 Lion Top npalardy Post subject: Re: Determine end of text file?Posted: Mon Nov 28, 2011 1:44 pm Joined: Sat Dec 24, 2005 8:18 pm Posts: 6910 Location: Canada, Alberta, Near Red Deer DrDungeon wrote:Hi guys! I'm writing a little app, part of which includes an ever-growing text file, using AppendToTextFile Basically, it stores sentences input by the user, and separates each sentence with an *. // store is a string sentence, " I am happy to see you today " f = Volume(0).Child("sal12000").Child("robby.dat") slotname = store + "*" // slotname is just for storing with the * delimiter. Delimits each sentence. // slotname is now " I am happy to see you today *" t=f.AppendToTextFile t.Write(slotname) t.Close This works fine. But the idea is that it will grow over time to dozens, or even hundreds of entries. I know how to read them back in, but what I don't know is how to determine how many are there? For example, say I have 10 entries in there. Using my system with the *, how could I have the program determine that there are 10 sentences in there - where the last * is? This is probably pretty simple but I can't seem to figure it out. Thanks, -Robert Why not just write sentences one per line in the text file. Then you just read line by line To handle any sentences that have a CR, LF, or CR LF embedded in them you just replace it with something else Change Chrb(13) for chrb(3) & Chrb(10) for Chrb(1) and reverse this when you read it back in Then every sentence is one line even though it could be an entire paragraph _________________ My web site Great White Software RBLibrary.com REALbasic learning Top Jason_Adams Post subject: Re: Determine end of text file?Posted: Mon Nov 28, 2011 1:54 pm Joined: Fri Nov 10, 2006 4:10 pm Posts: 1059 Location: Michigan, USA ZULU wrote:I prefer using split, it is a lot faster when using big strings ... Not trying to hijack the thread, but I was curious about this, so I ran some tests with profiling. The results were consistently faster that from 0 - 50000 bytes splitting was indeed faster. However, beyond this point at some threshold I don't know, the results became volatile; both averaging around 50-70ms. Not bad at all, really. _________________ Windows 7 Ultimate x64 Windows XP Pro SP3 Ubuntu 11.04 via Virtual Box RS Enterprise 2011r3 "Christianity has not been tried and found wanting; it has been found difficult and not tried." - G.K. Chesterton Top DrDungeon Post subject: Re: Determine end of text file?Posted: Mon Nov 28, 2011 8:33 pm Joined: Thu Jan 03, 2008 11:29 am Posts: 99 Thanks for all the tips. It was one of those deals where I thought of an answer 5 minutes after posting. All I had to do was load it all in a string, and use the Mid function to count the asterisks! Once its in a string I'm good to go. I can do anything with strings! Where app.longmem is the entire file loaded into a string: q = 0 For k = 1 To Len(app.longmem) If Mid(app.longmem, k, 1) = "*" Then q=q+1 End If Next k Also - does anyone know the practical limit to the characters a string can hold? I sort of remember it was quite large. Remember the old days when it was limited to 255 characters? Also - I'm intrigued by this split statement. Does that actually split a string into an array with each element ending in the "*"? sample = "hi there * how are you * i see the forum * it is cold today *" becomes like: k(1) = "hi there *" k(2) = "how are you *" k(3) = "i see the forum *" k(4) = "it is cold today *" ? If so, that could be a handy feature indeed! Thanks, -Robert Top timhare Post subject: Re: Determine end of text file?Posted: Mon Nov 28, 2011 8:50 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 10509 Location: Portland, OR USA Split eliminates the separators, so there will be no asterisks in the array. It is a very useful function. Top charonn0 Post subject: Re: Determine end of text file?Posted: Tue Nov 29, 2011 6:43 pm Joined: Mon Apr 02, 2007 2:08 am Posts: 581 Location: San Francisco, CA, USA DrDungeon wrote:Also - does anyone know the practical limit to the characters a string can hold? I sort of remember it was quite large. Remember the old days when it was limited to 255 characters? One would imagine that a string can be as large as the process's available address space. Under 32 bit Windows, for example, 2GB minus the memory allocated for the rest of the program. I don't have anything to back this up, though. _________________ Boredom Software Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 10 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
