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

How to change the Line Number accessed during a "fopen" session

Status
Not open for further replies.

matt1981m

Technical User
Jul 1, 2006
32
US
I have been trying to use fopen as well as other fXXX commands to open a current .was file, move the cursor position to a specific line, capture everything on that line (lets say that line only contained the text [s1 = "oldpassword"] without the brackets) and assign that text to a string (we will call the string "oldline"). I would then replace everything on that line using fputs with a different predetermined string (lets say the string name was "newline" and it contained [s1 = "newpassword"] without the brackets). I will then use strcat to edit the "oldline" string I captured to look like this [;s1 = "oldpassword"] without the brackets, and call this newly created string "updtdoldline". I want to then move the cursor to the bottom of the script, to a line after the endproc command. I will there overwrite the contents of that line with the "updtdoldline" string. Basically what this script is going to be is a password change script. It will allow the user to edit a "password" file that sets a global variable to what the users password is. I use the same password for different switches. So far I can only get the script to work if I use multiple [if fgets 0 XXXXXX] commands in a row (however many needed to get to the desired line..). I know there has to be a way to go to a specific line number, but I cant figure it out. I dont want to use just a character number because that would change based on what the previous string lengths were (a username string is the line before in the password script). Below is the current "password" script format I have been using. I "modified" my approach for this new script I am trying to create while I was typing this post, so I will post my password change script once I have applied the changes I want to make. How do I get the script to move the cursor position to a specific line????
 
i think part of my answer is going to reside in closed (thread448-927541). Knob had posted:
Knob said:
proc main
string sLine

fopen 0 "file.txt" READ TEXT
while not feof 0
fgets 0 sLine
if strfind sLine "alarm"

endif
endwhile
endproc

Of course, you'll need to modify file.txt with the path and name of the file you want to open.

The if strfind command will check each line of text for the string alarm. You would want to place whatever commands need to be executed when an alarm is found between if strfind and endif lines

I believe if i use the basic modifications below it would correct the prob... i will test it and find out...
me said:
......
fopen 0 "file.txt" READWRITE TEXT
while not feof 0
fgets 0 sLine
if strfind sLine "s1"
fgets 0 OLDLINE
fputs 0 NEWLINE
ENDIF
if strfind sLine2 ";s1"
fputs 0 UPDTDOLDLINE
endif
endwhile
fclose
endproc

i know i left out the strcat section i talked about as well as a few other sections... but I think this is the basic idea...i will post more when i figure it out...
 
here is my "password" script...

MAIN_SCRIPT.WAS said:
;MAIN PASSWORD SCRIPT. THE SECTION YOU NEED TO CHANGE IS BELOW. GOOD LUCK!
;SEE SAMPLE IN "MAIN_SCRIPT.TXT" FOR INSTRUCTIONS!!!
proc main
set duplex 0
s0 = "currentusername"
s1 = "currentpassword"
pwtitlebar "Logging into Passwerks..." PERMANENT
endproc

;s1 = "OLDPASSWORD"
here is my password change book....for those who are pay close attention to this script... i dont have the username change section scripted yet. i wanted to atleast get the password section done first...

CHNGPSWD said:
;script for changing switch password.. only run this script to change the password. you may also change the username if you check the box on the dialog box.
string newpswd,newusr,pswdend,usrstr,pswdstr,usrend
integer chngusr,ENTER,LEN
proc main
pswdstr = "`s1 = `""
pswdend = "`""
usrstr = "`s0 = `""
usrend = "`""
dialogbox 0 148 83 242 107 2 "Change Password"
editbox 17 68 22 98 16 newpswd
pushbutton 7 73 51 40 20 "OK" OK
pushbutton 8 125 51 40 20 "CANCEL" CANCEL
text 11 58 1 120 17 "PLEASE ENTER YOUR NEW SWITCH PASSWORD:" CENTER
checkbox 20 63 72 112 9 "ALSO CHANGE USERNAME" CHNGUSR
text 6 43 85 148 19 "[NOTE: IF SELECTED, A NEW DIALOG BOX WILL APPEAR FOR THE USERNAME.]" center
enddialog
while 1
dlgevent 0 ENTER
switch ENTER
case 0
endcase
case 7
exitwhile
endcase
case 8
halt
endcase
endswitch
endwhile
dlgdestroy 8 CANCEL
strlen newpswd LEN
if nullstr NEWPSWD
usermsg "YOU DID NOT ENTER A NEW PASSWORD!!"
call main
ENDIF
IF ( ! CHNGUSR )
call SAMEUSR
ELSE
call EDITUSR
endif
strcat pswdstr newpswd
strcat pswdstr pswdend
usermsg pswdstr
call pswdupdt
endproc

proc sameusr

endproc

proc editusr
if sdlginput "Change Username" "Please enter a new username." newusr
isfile newusr
endif
strlen newusr LEN
if nullstr NEWUSR
usermsg "YOU DID NOT ENTER A USERNAME!!"
call editusr
else
strcat usrstr newusr
strcat usrstr usrend
usermsg usrstr
call usrupdt
endif

endproc

proc usrupdt
endproc

proc pswdupdt
string Fname = "MAIN_SCRIPT.TXT"

STRING UPDTDOLDPSWD = ";"
STRING OLDPSWD
STRING SLINE
STRING SLINE2


fopen 0 Fname READWRITE TEXT
while not feof 0
fgets 0 sLine
if strfind sLine "s1"
fgets 0 OLDPSWD
fputs 0 PSWDSTR
STRCAT UPDTDOLDPSWD OLDPSWD
USERMSG UPDTDOLDPSWD
fgets 0 sLine2
strfind sLine2 ";s1"
fputs 0 UPDTDOLDPSWD
endif

endwhile
fileview Fname
FCLOSE 0
endproc

everytime i try this now i get the following as a result (note: i entered MYNEWPSWD as the new password....

INCORRECT RESULT said:
;MAIN PASSWORD SCRIPT. THE SECTION YOU NEED TO CHANGE IS BELOW. GOOD LUCK!
;SEE SAMPLE IN "MAIN_SCRIPT.TXT" FOR INSTRUCTIONS!!!
proc main
set duplex 0
s0 = "currentusername"
s1 = "currentpassword"
pwtitlebar "Logging into Passwerks..." PERMANENT
s1 = "MYNEWPSWD"
OLDPASSWORD";pwtitlebar "Logging into Passwerks..." PERMANENT

this is what i should see

DESIRED RESULT said:
;MAIN PASSWORD SCRIPT. THE SECTION YOU NEED TO CHANGE IS BELOW. GOOD LUCK!
;SEE SAMPLE IN "MAIN_SCRIPT.TXT" FOR INSTRUCTIONS!!!
proc main
set duplex 0
s0 = "currentusername"
s1 = "MYNEWPSWD"
pwtitlebar "Logging into Passwerks..." PERMANENT
endproc

;s1 = "currentpassword"

what should i do?!?!?!?!?!
 
Unless each line in the file is a constant length (i.e. all lines are 20 characters line), the only way to get to a specific line is to use multiple fgets commands. If each line were the same length, you could use the fseek command to jump the appropriate number of characters into the text file.

 
Each line is a different length... The length will also vary depending on the user as well... How do i use the "multiple" fgets commands... And how do you get the script to change lines?... That is the main thing i don't understand...
 
Looking back at your original message, what you are basically needing to do is update the password that is hardcoded in the script, correct? If so, it may be easier just to have the script read the password from a separate text file than have to modify the script when the password changes. This could be something as simple as having one text file per device with the password, one text file with all device name and passwords (along with a way to find the correct line and read the password), up to using DDE with an Excel spreadsheet to read passwords from the spreadsheet.

 
i thought of that way but i would prefer to do it this way... i just go into the password script and update it manually.... the problem is that other people i work with use the scripts... i have had to edit "broken" scripts too many times because of someone deleting the wrong thing when they update their password.... this would be nice, simple, and fool proof....i would think that there would be a way to tell the script to go to line 6 and replace line 6 with XXXX text....you would think that would be possible...lol
 
It is possible, just a bit tricky. You would need to use fgets six times to get to that line (could have it in a loop), then you would use the rewind/delblock/insblock commands to replace the old line with the new line. I have a couple sample scripts on my site that show how to do this.

 
Cool... I will give it a try when i get in to work tomorrow...
 
Try using the profileWr and profileRd to read or write the old or new passwords.

Manually edit your main_script.was to look like this noting the necessary absence of spaces around the '=' operator.

#comment
[pwTopic]
#endcomment
proc main
set duplex 0
s0 = "currentusername"
s1="MYNEWPSWD"
pwtitlebar "Logging into Passwerks..." PERMANENT
endproc

#comment
oldPW="currentpassword"
#endcomment

Then in whatever script you use to read or write these values, use the following code:

string mainScriptFilePath = "main_script.was" ;
string oldS1
string newPassword
string prevOld

;To read the old password from oldPW -
profilerd mainScriptFilePath "pwTopic" "oldPW" prevOld

;To read the current password from s1 -
profilerd mainScriptFilePath "pwTopic" "s1" oldS1

;To write oldS1 value to oldPW -
profilewr mainScriptFilePath "pwTopic" "oldPW" oldS1

;To write a new password to s1 -
strquote newPassword ;Enclose in double quotes. <--<<<
profilewr mainScriptFilePath "pwTopic" "s1" newPassword


The beauty is that there is no manual file operations like: opening, closing, searching, deleting or inserting blocks, checking string length, etc.

The #comment and #endcomment pairs are to "hide" what is needed for profilewr/rd from the compiler so you can use your .was file as, what is traditionally called, a .ini or config file.
...To the profilewr/rd commands, main_script.was "looks like" this:

[pwTopic]
s1="MYNEWPSWD"
oldPW="currentpassword"

... and to the compiler, it looks like this:
proc main
set duplex 0
s0 = "currentusername"
s1="MYNEWPSWD"
pwtitlebar "Logging into Passwerks..." PERMANENT
endproc




 
that is awesome... i will try that right now!!!
 
i still cant get it to work... here is my update password script.... i even added a "usermsg oldS1" command after the script is supposed to read the MAIN_SCRIPT.WAS file and capture the s1 string as oldS1...the dialog box that shows up is blank instead of having the string....

CHANGEPASSWORD.WAS said:
;script for changing switch password.. only run this script to change the password. you may also change the username if you check the box on the dialog box.
string newusr
string oldS0
string mainScriptFilePath = "MAIN_SCRIPT.WAS"
string oldS1
string newpswd
STRING prevOld
integer chngusr,ENTER,LEN

proc main
dialogbox 0 148 83 242 107 2 "Change Password"
editbox 17 68 22 98 16 newpswd
pushbutton 7 73 51 40 20 "OK" OK
pushbutton 8 125 51 40 20 "CANCEL" CANCEL
text 11 58 1 120 17 "PLEASE ENTER YOUR NEW SWITCH PASSWORD:" CENTER
checkbox 20 63 72 112 9 "ALSO CHANGE USERNAME" CHNGUSR
text 6 43 85 148 19 "[NOTE: IF SELECTED, A NEW DIALOG BOX WILL APPEAR FOR THE USERNAME.]" center
enddialog
while 1
dlgevent 0 ENTER
switch ENTER
case 0
endcase
case 7
exitwhile
endcase
case 8
halt
endcase
endswitch
endwhile
dlgdestroy 8 CANCEL
strlen newpswd LEN
if nullstr NEWPSWD
usermsg "YOU DID NOT ENTER A NEW PASSWORD!!"
call main
ENDIF
IF (CHNGUSR)
call EDITUSR
ELSE
call SAMEUSR
endif
call pswdupdt

endproc




proc sameusr
;do not remove this procedure. It is placeholder and must exist without any modifications inorder for the change password script to work
endproc




proc pswdupdt
;To read the old password from oldPW -
profilerd mainScriptFilePath "pwTopic" "oldPW" prevOld
;this next line will read the current password saved in the main_script file (s1), and assign it to a new string (oldS1)
profilerd mainScriptFilePath "pwTopic" "s1" oldS1
usermsg oldS1
;this next line will take that new string (oldS1), and write it to the oldPW line of the main_script file
strquote oldS1
profilewr mainScriptFilePath "pwTopic" "oldPW" oldS1
;the next 2 lines will take the new password (newpswd), and assign it to the password string (s1) within the main_script file
strquote newpswd ;Enclose in double quotes. <--<<<
profilewr mainScriptFilePath "pwTopic" "s1" newpswd
usermsg newpswd
endproc




proc editusr
if sdlginput "Change Username" "Please enter a new username." newusr
isfile newusr
endif
strlen newusr LEN
if nullstr NEWUSR
usermsg "YOU DID NOT ENTER A USERNAME!!"
call editusr
else
usermsg newusr
endif
;this next line will read the current username saved in the main_script file (s0), and assign it to a new string (oldS0)
profilerd mainScriptFilePath "pwTopic" "s0" oldS0
;this next line will take that new string (oldS0), and write it to the oldUSR line of the main_script file
strquote oldS0
profilewr mainScriptFilePath "pwTopic" "oldUSR" oldS0
;the next 2 lines will take the new username (newusr), and assign it to the username string (s0) within the main_script file
strquote newusr ;Enclose in double quotes. <--<<<
profilewr mainScriptFilePath "pwTopic" "s0" newusr
endproc

and here is my main password script....


MAIN_SCRIPT.WAS said:
;MAIN PASSWORD SCRIPT. THE SECTION YOU NEED TO CHANGE IS BELOW. GOOD LUCK!
;SEE SAMPLE IN "MAIN_SCRIPT.TXT" FOR INSTRUCTIONS!!!

#comment
[pwTopic]
#endcomment

proc main
set duplex 0
s0="CURRENTUSERNAME"
s1="CURRENTPASSWORD"
pwtitlebar "Logging into Passwerks..." PERMANENT
endproc

#comment
oldPW="OLDPASSWORD"
oldUSR="OLDUSERNAME"
#endcomment
 
i got it to work.... i saw this in the aspect help files
profilewr said:
filespec The name of the target .ini file. If a path is not specified, the Windows directory ($WINPATH) will be used.

i then edited the string mainScriptFilePath = "MAIN_SCRIPT.WAS" to point to the default script path and got it to work... the only problem i could see is if the user had a different script path than specified it wouldnt work. i had a great idea though, i used fetch scriptpath dfltpth to capture the default script path and assign it to the dfltpth string... it then added a forward slash by using strcat dfltpth "\". i then used strcat dfltpth mainScriptFilePath and then mainScriptFilePath = dfltpth to set the mainScriptFilePath string to the full path based on the individual users settings... i know i made this a little more complicated than it had to be, but i am a difficult person, so it is ok...lol

here is my final (AND WORKING!!!!!!!!!!!) password change script. Thanks to ambl and knob who replied to my archaic request, and to my gf for not getting mad when i ignored her the past couple weeks while trying to figure this *&($*&$ script out...

CHANGEPASSWORD.WAS said:
;script for changing switch password.. only run this script to change the password. you may also change the username if you check the box on the dialog box.
string newusr
string oldS0
string mainScriptFilePath = "MAIN_SCRIPT.WAS"
string oldS1
string newpswd
STRING prevOld
STRING WINDFLTPTH = "1"
STRING DFLTPTH
integer chngusr,ENTER,LEN

proc main
FETCH SCRIPTPATH DFLTPTH
STRCAT DFLTPTH "\"
STRCAT DFLTPTH mainScriptFilePath
mainScriptFilePath = DFLTPTH
dialogbox 0 148 83 242 107 2 "Change Password"
editbox 17 68 22 98 16 newpswd
pushbutton 7 73 51 40 20 "OK" OK
pushbutton 8 125 51 40 20 "CANCEL" CANCEL
text 11 58 1 120 17 "PLEASE ENTER YOUR NEW SWITCH PASSWORD:" CENTER
checkbox 20 63 72 112 9 "ALSO CHANGE USERNAME" CHNGUSR
text 6 43 85 148 19 "[NOTE: IF SELECTED, A NEW DIALOG BOX WILL APPEAR FOR THE USERNAME.]" center
enddialog
while 1
dlgevent 0 ENTER
switch ENTER
case 0
endcase
case 7
exitwhile
endcase
case 8
halt
endcase
endswitch
endwhile
dlgdestroy 8 CANCEL
strlen newpswd LEN
if nullstr NEWPSWD
usermsg "YOU DID NOT ENTER A NEW PASSWORD!!"
call main
ENDIF
IF (CHNGUSR)
call EDITUSR
ELSE
call SAMEUSR
endif
call pswdupdt

endproc




proc sameusr
;do not remove this procedure. It is placeholder and must exist without any modifications inorder for the change password script to work
endproc




proc pswdupdt
;To read the old password from oldPW -
profilerd mainScriptFilePath "pwTopic" "oldPW" prevOld
;this next line will read the current password saved in the main_script file (s1), and assign it to a new string (oldS1)
profilerd mainScriptFilePath "pwTopic" "s1" oldS1
;this next line will take that new string (oldS1), and write it to the oldPW line of the main_script file
strquote oldS1
profilewr mainScriptFilePath "pwTopic" "oldPW" oldS1
;the next 2 lines will take the new password (newpswd), and assign it to the password string (s1) within the main_script file
strquote newpswd ;Enclose in double quotes. <--<<<
profilewr mainScriptFilePath "pwTopic" "s1" newpswd
endproc




proc editusr
if sdlginput "Change Username" "Please enter a new username." newusr
isfile newusr
endif
strlen newusr LEN
if nullstr NEWUSR
usermsg "YOU DID NOT ENTER A USERNAME!!"
call editusr
endif
;this next line will read the current username saved in the main_script file (s0), and assign it to a new string (oldS0)
profilerd mainScriptFilePath "pwTopic" "s0" oldS0
;this next line will take that new string (oldS0), and write it to the oldUSR line of the main_script file
strquote oldS0
profilewr mainScriptFilePath "pwTopic" "oldUSR" oldS0
;the next 2 lines will take the new username (newusr), and assign it to the username string (s0) within the main_script file
strquote newusr ;Enclose in double quotes. <--<<<
profilewr mainScriptFilePath "pwTopic" "s0" newusr
endproc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top