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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

working with multiple files

Status
Not open for further replies.

hankm3

Programmer
Jan 27, 2002
284
US
Hello,

This what I have. File 1 contains Data. File 2 is a List. If the First 3 Characters in File 2 do NOT match the First 3 Characters in File 1, send the Line in File 1 to File 3.

This is what the Script Looks like; but it always sends Just the First Line of File 1 to File 3. I know it's a While Logic Problem..

proc Main
string sLine1
string sLine2
fopen 1 "C:\TEXT FILES\390NNX.TXT" READ TEXT
fopen 2 "C:\TEXT FILES\RCF_ORIG.TXT" READ TEXT
fopen 3 "C:\TEXT FILES\390_UPD.TXT" CREATE TEXT
while not feof 1
fgets 1 sLine1
while not feof 2
fgets 1 sLine2
if not strnicmp sLine1 sLine2 2
fputs 3 sLine1
endif
endwhile
endwhile
fclose 1
fclose 2
fclose 3
usermsg "DONE"
endproc


Any Ideas are Welcome
 
In this block of code:

fgets 1 sLine1
while not feof 2
fgets 1 sLine2

I'm betting that the second fgets command should be "fgets 2 sLine2" instead.

 
Knob,

Tried that but it still Prints the First line of Fopen 1 repeatedly to Fopen 3 for the number of occurrances in Fopen 2. If Fopen 2 has 200 entries then the Script prints Line 1 of Fopen 1 200 times to Fopen 3.

If you have any other ideas, please let me know.

Thanks again



 
One other thing I noticed is that the line:

if not strnicmp sLine1 sLine2 2

should be changed to:

if not strnicmp sLine1 sLine2 3

if you want to check the first three characters of sLine1 and sLine 2. The way your script is currently written, it is only comparing the first two characters of each string.

I think I see now why the script is writing sLine1 repeatedly to the file. The way the script is written, each line in file #2 will be compared against sLine1. Each non-match will be written to the file, so you will see the value of sLine1 written to the file more than once.

Another problem is that after one iteration of reading all of the values in from file #2, any future attempts to read from that file will result in an EOF message. You can get around this problem by placing "rewind 1" after the while not eof 2/endwhile block.
 
Knob,

Still having the same Issues. Will continue to work on this and if I get it working, I'll post the results.

Thanks
 
Here's something I came up during lunch today. I may have misled you on the rewind command - you want to use rewind 2 instead of rewind 1 as I mentioned (this would throw you into an endless while loop). The script I have right now is still outputting multiple instances of sLine1, but at least it is reading past the first line of the input file.

proc Main
string sLine1
string sLine2
fopen 1 "C:\aaa.txt" READ TEXT
fopen 2 "C:\bbb.txt" READ TEXT
fopen 3 "C:\results.txt" CREATE TEXT
while not feof 1
fgets 1 sLine1
while not feof 2
fgets 2 sLine2
if not strnicmp sLine1 sLine2 3
fputs 3 sLine1
endif
endwhile
rewind 2
endwhile
fclose 1
fclose 2
fclose 3
usermsg "DONE"
endproc

I think the multiple output is due to the way your script is written currently, but maybe we can find a way to do what you want differently. If your data file looks like:

AAA

and your list file contains:

AAA
BBB
CCC

then you will be outputting AAA for each occurrence in the list file that does not match AAA. What it seems the script should do is only output AAA if it does not find any matches in the list file. If the script finds one match, then it shouldn't output anything at all. If I'm summing this up right, then this script should do the trick:

proc Main
string sLine1
string sLine2
fopen 1 "C:\aaa.txt" READ TEXT
fopen 2 "C:\bbb.txt" READ TEXT
fopen 3 "C:\results.txt" CREATE TEXT
while not feof 1
fgets 1 sLine1
while not feof 2
fgets 2 sLine2
if not strnicmp sLine1 sLine2 3
fputs 3 sLine1
endif
endwhile
rewind 2
endwhile
fclose 1
fclose 2
fclose 3
usermsg "DONE"
endproc

What I did was use a variable (iFound) that is set to zero right before you start reading from the list file. If I find a match between the data file and list file, then I set iFound to 1. When I have read through all of the data in the list file, I check if iFound is 0. If it is, then no matches were found and I output sLine1 to the results file.
 
Knob,

I played with the code and I can get it to work, somewhat !! The code below will print all lines that appear in both File 1 and File 2. But it's a start. Not sure why it's working the way it does. It only print Line 1 Once. Any ideas ??

proc Main
integer x
string sLine1
string sLine2
fopen 1 "C:\TEXT FILES\389ONE.txt" READ TEXT
fopen 2 "C:\TEXT FILES\389TWO.txt" READ TEXT
fopen 3 "C:\TEXT FILES\389_out.txt" CREATE TEXT
while not feof 1
fgets 1 sLine1
while not feof 2
x=0
fgets 2 sLine2
if strcmp sLine1 sLine2
x++
endif
if x != 0
fputs 3 sLine1
endif
endwhile
rewind 2
endwhile
fclose 1
fclose 2
fclose 3
usermsg "DONE"
endproc


Thanks again
 
If you change this code:

if x != 0
fputs 3 sLine1
endif
endwhile
rewind 2

to be:

endwhile
if x != 0
fputs 3 sLine1
endif
rewind 2

then I think you should be OK. What is happening is that you are checking to see if a match was found before you have finished going through all of the entries in file #2. Since the chances of the first three characters of sLine1 being equal to the first three characters of all the data in file #2 is fairly small, this would result in your variable x being non-zero, and in each entry being output to the results file.

I just realized that I didn't post the correct version of the final script I came up with, so here it is below. Hopefully my earlier comments make a little more sense now!

proc Main
string sLine1
string sLine2
integer iFound

fopen 1 "C:\aaa.txt" READ TEXT
fopen 2 "C:\bbb.txt" READ TEXT
fopen 3 "C:\results.txt" CREATE TEXT
while not feof 1
fgets 1 sLine1
iFound = 0
while not feof 2
fgets 2 sLine2
if strnicmp sLine1 sLine2 3
iFound = 1
endif
endwhile
if iFound == 0
fputs 3 sLine1
endif
rewind 2
endwhile
fclose 1
fclose 2
fclose 3
usermsg "DONE"
endproc
 
Knob,

Thanks... That did the Trick.. expanded it for Tokens on a Line String (sLine1 / sLine2)..

Knew it was Just a While Logic Issue.

Thanks Again for the Help..

Hank
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top