[Closed] stringstream Read /Find/Skip
After number of ours playing with code to read stringstream I gave up.
I need a little help with I think very simple stuff.
I need access to remote txt file. I know how to open file and read line by line but
text file can have different amount of lines and I dont know how to read all of them
what is essential for me how to:
- check if in entire stringstream s is word “failed” if exist then
- skip two lines above(or lower- for this case is better upper-above) then
-take this line and turn it into value
- skip two lines above(or lower- for this case is better upper-above) then
and also essential how to:
- skip into last line and check for word “failed” if exist then …
It would be grateful if somebody could help me with this.
File is a list of objects from scene and is depend on amount of objects so it’s hard to maintain how log list can be, it can be 4 lines but can be also 4444 lines long.
There is 3 things on my list: object number, object name, “success” or “failed” as marker for operation.
I’m try to search if operation for each object was success or failed so I look for “failed” word if exist, then go 2 lines above to get number of object for “failed” operation and start action- do…
Script should look for last “failed” word because it can be few “failed” markers however most likely should be one only.
Part 2
Also need as separate action to get all objects ad check if there is “failed” world for some of them and collect their name to list array, but crucial is part one. I would be super happy if you could give me some help with this because it will take number of days or more to figure this out by my self.
text inside txt file is(there will be nothing else except this):
“start”
1
“Torus001”
“success”
2
“Box001”
“success”
3
“Torus002”
“success”
4
“Teapot001”
“success”
5
“Teapot003”
“success”
6
“Plane001”
“success”
7
“Plane002”
“success”
8
“Pyramid001”
“success”
9
“Pyramid002”
“success”
10
“GeoSphere001”
“success”
11
“Teapot004”
“success”
… etc … etc…
fn makeTestFile file: count:100 seedvalue: show:off =
(
if file == unsupplied do file = getdir #temp + @" emp_scene_data.txt"
if (ss = createfile file) != undefined do
(
format "start
" to:ss
if seedvalue != unsupplied do seed seedvalue
for k=1 to count do
(
format "%
%
%
" k (formattedprint (random 0 1e9) format:"08X") (if (random 1 5) == 4 then "failed" else "succeed") to:ss
)
flush ss
close ss
if show do edit file
file
)
)
fn getAllData file: type:#all =
(
if file == unsupplied do file = getdir #temp + @" emp_scene_data.txt"
if (ss = openfile file) != undefined and (readline ss) == "start" do
(
data = #()
while not eof ss do
(
d = #(readvalue ss, readline ss, readline ss)
if type == #all or type == d[3] do append data d
)
close ss
data
)
)
(
makeTestFile count:20000
t1 = timestamp()
d = getAllData()
format "all: % >> time:% data:%
" d.count (timestamp() - t1) d
t1 = timestamp()
d = getAllData type:"failed"
format "failed: % >> time:% data:%
" d.count (timestamp() - t1) d
t1 = timestamp()
d = getAllData type:"succeed"
format "succeed % >> time:% data:%
" d.count (timestamp() - t1) d
)
for super big files the method probably will slow … so the better:
change the format to a smarter one (xml, json, anything else) and use the power of these formats
or read all lines and check every third. like:
fn readAllData file: type:#all =
(
if file == unsupplied do file = getdir #temp + @" emp_scene_data.txt"
data = (dotnetclass "System.IO.File").ReadAllLines file
for k=4 to data.count by 3 where type == #all or data[k] == type collect #(data[k-2] as integer, data[k-1], data[k])
)
Great thank you for You for helping mi with this.
Maybe I will compromise my self but excuse me next question but my basic maxscript knowledge is
it’s not enough to get what you gave me.
I spent some time to figure out what is what in your code
and I realise it’s still black magic to me. Could You tell me what is what?
First) I have to open my text file which will be always – c:\lmaxrecovery-log.txt
second) I have to get number of failed object as value – which variable in your code is this, and where is it?
Third) Where can I placed rest of my code (I know it’s stupid question, but I have no idea how your script work,
so I don’t want to mess with it)
Forth)Where is list of failed objects in your code, I tried d in your code it’s da = getAllData()
but it always gives me all objects no matter if succes or failed.
To give you glipmse of my poor programing skills this’s code I started working on to
read txt, and serach word “failed” hovewer I failed:)Maybe it would be better if you also could
tell me How can I make my loop read existing numer of lines only
and then check for word “failed” go 2 lines above, take line as value
s=openFile “c:\lmaxrecovery-log.txt”
–n=eof s
for s=0 to 3 do
(
skipToNextLine s
rdo=skipToString s “failed”
rdo=rdo as string
if rdo==undefined as string then print “start action”
else
(
print “stop action”
)
)
you are new on this forum and probably don’t know me well…
i usually don’t explain the code that i show. you can copy and past it into your code, but… if you want to learn, if you want to know how and why it works you have to get it yourself.