# Is it possible to parse file from end to beginning?

**URL:** <https://tech-community.robotics.abb.com/t/is-it-possible-to-parse-file-from-end-to-beginning/8670>\
**Category:** RAPID Programming\
**Created:** [January 25, 2019, 12:33pm UTC](https://tech-community.robotics.abb.com/t/is-it-possible-to-parse-file-from-end-to-beginning/8670 "2019-01-25T12:33:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![egor](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/egor/32/2735_2.png) [@egor](https://tech-community.robotics.abb.com/u/egor)\
**Post date:** [January 25, 2019, 12:33pm UTC](https://tech-community.robotics.abb.com/t/is-it-possible-to-parse-file-from-end-to-beginning/8670/1 "2019-01-25T12:33:05Z")

</div>

I can read and parse txt file from the beginning to the end this way:

Open “HOME:/Test.txt”,file\Read;  
Rewind file;  
WHILE EOF=TRUE DO  
Read\_Str:=ReadStr(file);  
ENDWHILE

But how to read the file in reverse order? Is it possible using “ReadStr” function?

---

<div class="post-metadata">

**Author:** ![soup](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/soup/32/1592_2.png) [@soup](https://tech-community.robotics.abb.com/u/soup)\
**Post date:** [January 25, 2019, 4:07pm UTC](https://tech-community.robotics.abb.com/t/is-it-possible-to-parse-file-from-end-to-beginning/8670/2 "2019-01-25T16:07:43Z")

</div>

You could probably count the lines of the file with a FOR loop from beginning until EOF, then use ReadStr with \Line to start with the last line and FOR loop from the last line to the first line.

---

<div class="post-metadata">

**Author:** ![soup](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/soup/32/1592_2.png) [@soup](https://tech-community.robotics.abb.com/u/soup)\
**Post date:** [January 25, 2019, 8:23pm UTC](https://tech-community.robotics.abb.com/t/is-it-possible-to-parse-file-from-end-to-beginning/8670/3 "2019-01-25T20:23:32Z")

</div>

Seemed to work – as long as the line is not too long and can fit into a single string…

PROC exampleReadFileInReverse()  
VAR string text;  
VAR iodev infile;  
VAR num lineCounter:=0;

Open “HOME:/test.txt”,infile\Read;

! count number of lines in the file  
lineCounter:=0;  
WHILE text \<\> EOF DO  
IF text \<\> EOF THEN  
text := ReadStr(infile\Line:=lineCounter+1);  
IF text \<\> EOF lineCounter:=lineCounter+1;  
ENDIF  
ENDWHILE

TPWrite “File has “+NumToStr(lineCounter,0)+” lines.”;

! read file in reverse  
FOR i FROM lineCounter TO 1 STEP -1 DO  
text := ReadStr(infile\Line:=i);  
TPWrite “Line #”+NumToStr(i,0)+" = "+text;  
WaitTime 1;  
ENDFOR

ENDPROC

---

<div class="post-metadata">

**Author:** ![egor](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/egor/32/2735_2.png) [@egor](https://tech-community.robotics.abb.com/u/egor)\
**Post date:** [January 28, 2019, 6:10am UTC](https://tech-community.robotics.abb.com/t/is-it-possible-to-parse-file-from-end-to-beginning/8670/4 "2019-01-28T06:10:58Z")

</div>

> [@soup](#):
>
> Seemed to work – as long as the line is not too long and can fit into a single string…

Thanks for answer! I will definitely try your code. I figured out how to solve the problem through a temporary array. It’s working with any amount of strings in file.  
[Reverse\_File.rar](https://tech-community.robotics.abb.com/uploads/short-url/kAICiBm0TWoBDymnkthZ9ejilDa.rar) (488 Bytes)

---

<div class="post-metadata">

**Author:** ![soup](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/soup/32/1592_2.png) [@soup](https://tech-community.robotics.abb.com/u/soup)\
**Post date:** [January 28, 2019, 4:16pm UTC](https://tech-community.robotics.abb.com/t/is-it-possible-to-parse-file-from-end-to-beginning/8670/5 "2019-01-28T16:16:53Z")

</div>

Looks good.

To clarify: I was stating that you’d have a problem if a single line is long, rather than a large quantity of lines – make a test line with a hundred chars to see what I mean.
