PDA

View Full Version : Having problems with filestream...


grov
03-03-2006, 12:30 PM
Hi people,

I'm trying to create a kind of model database file containing some stuff. Right now it looks like this:

[1]
modelData1
modelData2
[2]
modelData1
modelData2
[3]
modelData1
modelData2
[4]
modelData1
modelData2


What I would like to do is to be able to update this data through a script.
I figured I'd do it by updating one "block" at a time (both model data strings).
So, the code for this would be something like:


modelID = "1"
newData = "New data 1\nNew data 2\n"


f = openFile "c:\\modelData.dat"

skipToString f ( "[" + modelID + "]\n" )
firstBlockEnd = (filePos f) - 1
seek f 0
firstBlock = readChars f firstBlockEnd

skipToString f "[" -- this won't work for the last model, but nevermind that
seek f ((filePos f) - 1)
secondBlock = readChars f 99999 errorAtEOF:false -- crappy temporary code again
close f

format "%%%" firstBlock newData secondBlock to:(newScript()) -- just testing what it'll look like


So, running this script with modelID = "1" works fine:

[1]
New data 1
New data 2
[2]
modelData1
modelData2
[3]
modelData1
modelData2
[4]
modelData1
modelData2


modelID = "2" gives me this result:

[1]
modelData1
modelData2
[2]
modNew data 1
New data 2
[3]
modelData1
modelData2
[4]
modelData1
modelData2

and modelID = "3":

[1]
modelData1
modelData2
[2]
modelData1
modelData2
[3]
modelDNew data 1
New data 2
[4]
modelData1
modelData2

Any ideas why this might happen, or rather, what I'm doing wrong? :)

Thanks!

RyanT
03-04-2006, 12:54 AM
So I took a different rount from you. This script will work for you. Now why is yours not working? Well I did some testing and I think readChars is inconsistant. The reason its not working on anything but [1] is onces you get to [2] or [3] you have to subtract more and more from the position it returns. I am not completely sure why this is happening yet, but I figured I would post a script to help you get moving forward.


modelID = "1"
newData = "[1]\nNew data 1\nNew data 2\n"
f = openFile "c:\modelData.txt" mode:"r+"

PostData = ("" as stringstream)
EndData = ("" as stringstream)

state = "before"
while not eof f do
(
newLine = readLine f
if newLine == ("[" + modelID + "]") then
(
nextBlock = false
while nextBlock == false do
(
testLine = readLine f
if (findstring testLine "[") != undefined do
(
nextBlock = true
state = "after"
format "%\n" testLine to:EndData
)
)
)
else
(
if state == "before" then
(
print newLine
format "%\n" newLine to:PostData
)
else
(
print newLine
format "%\n" newLine to:EndData
)
)
)

close f
format "%%%" (PostData as string) newData (EndData as string) to:(newScript())



-RyanT

grov
03-04-2006, 08:34 PM
That does work, Ryan. Much appreciated!

I noticed that subtraction thing earlier also. It isn't, in this case at least, really inconsistent though as it is always off by (modelID-1)*3 chars. Weird...

CGTalk Moderation
03-04-2006, 08:34 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.