Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Elegant way to exit a nested While?

Status
Not open for further replies.

Mpking

Technical User
Mar 2, 2002
23
0
0
US
I'm currently checking two files against each other using a nested WHILE.

If I hit the first error condition using the while, I have to do a sepearte IF looking for FEOF. is their an easier way, or should I just check twice?


Basicly my psudocode looks like this:

While not FEOF 0
szline=""
while check(&szline) ;Check for szline being NULL (Empty string)
fgets 0 szline ; get a record
if FEOF 0
fputs 1 "out of records in out.txt"
exitwhile
endif
endwhile

if FEOF 0
exitwhile ; This is what I'm wondering if is Nessacary??
; Is there a more elegant way?

While not FEOF 2
while check(&szrecord) ;Check for szline being NULL (Empty string)
fgets 2 szrecord ; get a record
if FEOF 2
fputs 1 "out of records in new.txt"
exitwhile
endif
endwhile

Do more stuff here

endwhile


 
Usually what I do is check the length of the line read via fgets using the strlen command. If it is zero, this means the line was blank, since fgets strips the carriage return and linefeed from all lines it reads. Chances are this what is happening in your case - the script is reading a blank line but is not truly at the end of the file just yet. If the file has truly reached the end, then the while loop will end on its own accord without you needing to use the exitwhile command.



aspect@aspectscripting.com
 
That is the short answer of what the problem is. I'm reading a blank line. I've been checking to see whether it was greater than 3 characters. (I didn't know it stripped the CR/LF off) This is the code from the Check Fuction.

Code:
func check : integer
	param string temszline
	integer y=0
	strlen temszline y
	if y<3
		return 1
	else
		return 0
	endif
endfunc

I've been working on this for about 8 hours now, and I have a mondo major 20 dollar bottle of asprine headache.

I applogize if this get's unorganized, it's just kinda flowing from me at this point.

Here's what I know. This psudeo code works, with it's related problem below:
Code:
while not FEOF 0	; out.txt
   fgets 0 szline	; get a record
   while not FEOF 4	; tndnlookup.txt
     fgets 4 szrecord  ; get a line
     if strcmp szline szrecord
        .... Do some stuff.....
        found =1
     endif
    endwhile
   if found =1
     delete a line from tndnlookup (FILE 4)
     add szrecord to tndatabase (FILE 5)
   endif
endwhile
This snippets problem is that if I have a blank line in out.txt (File 0) and tndnlookup (FILE 4), then it get's written to tndatabase (FILE 5). The process that creates FIles 0 and 4 always ends with a blank line. (I can't change this) so there is always a blank line in those files. so every time the script runs, i will add a blank line to FILE 5.

(Short problem, does not detect blanklines, and they get replicated)

The Psudeo code referenced in the first post detects the blank lines and exits the script. But something in the way that it works, it moves the filepointer in tndnlookup. (in the first post, FILE 2, in this post FILE 4 sorry I did not use the same file ID's) This causes a later process to delete a line, to be in the wrong position and not delete the entire line, leaving some characters and but no CR/LF, so when the next write occurs, it effectivaly changes the string I search for. This error only occurs on the last line of the TNDNlookup file, not when it's the first line or in the middle.

(Short problem, detects blanklines, but in doing so, moves the filepointer and screws up a later script in one specfic exmaple)

Now, as a temporary measure, I added checking to the script that uses the TNDATABASE (FILE 5) so that if it get's a blank line, to just get another one. I'd like to avoid adding the blanklines to the script. No need to increase the file size if it's not nessacary. I'll proably leave the blank line detection in however.

As I type this, I see two solutions.
1. Chase my first example, and find out why the file pointer is moving on the Last line of the script.

2. Chase my second example, and use a different blank line detection code. This could end up being solution one as well.

After all that, here is my question.

Which way should I proceed?

Secret options 3 thru infinity are also welcome for consideration. (IE I missed the obvious, easy answer)

(I wonder if I forgot anything, important or otherwise)
 
Ok, it's the next day, and I've had a little time to clear my head.

I'm gonna give this a shot. I think it might solve my problem. I'll report back. (I didn't know the loopwhile command existed.

Code:
	while not FEOF 0	; out.txt

	   fgets 0 szline	; get a record
	   if nullstr szline   ; if it's blank
	   	loopwhile
	   endif
            .....Do lots of stuff.......
         endwhile

 
Don't know whether this is causing a problem but in the example you posted above, it looks as though you are only searching through file 4 from the beginning only on the first pass. Subsequent passes start searching file 4 from where the last pass exited. For instance, your example
Code:
while not feof 1
   fgets 1 strvar1
   while not feof 4
      fgets 4 strvar2
      ; compare strings
   endwhile
   ; process strings
endwhile
On the first pass you get the first line of file 1 and move file pointer for file 1 to next line. Then, you get each line from file 4 progressly moving the file pointer for file 4 to the following line. On subsequent passes of your first while loop, the file pointer for file 4 is not at the beginning and therefore your are not searching the entire file.

If you do not meet your conditions at any stage, the file pointer for file 4 will progressively move to the end of file 4. Then, on subsequent passes, the second while will not meet the feof condition because the file pointer for file 4 is already at the end of the file. You need to move the file pointer for file 4 back to the beginning if you wish to search through the entire file again. Perhaps your code should look like this.
Code:
while not feof 1
   fgets 1 strvar1
   fseek 4 0 0          ; restart file pointer for file 4 
   while not feof 4
      fgets 4 strvar2
      ; compare strings
   endwhile
   ; process strings
endwhile
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top