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

Go to line no. in .prg file programmatically

Status
Not open for further replies.

KALP1

Programmer
Aug 26, 2016
102
IN
I want to open a .prg file , go to specified line no.(programmatically) and make some changes there . Is there any way to go to say line n in a .prg file. I require this as I know line no. where error occured , on entering ok button I want to go to that line no. in program file
 
Yes, there are several ways of doing this.

One option would be to use ALINES() to get the file into an array, then make the required change, and then write the array back to the file. Something like this:

Code:
lcFile = FILETOSTR(<path to input file>)
lnLine = 10 && this is the required line number

* Get the lines into an array
ALINES(laLines, lcFile, CHR(13) + CHR(10))

* Make the required edit, for example:
laLines(lnLine) = "This is some edited text"

* Write the lines back
lcText = ""
FOR lnI = 1 TO ALEN(laLines, 1)
  lcText = lcText + laLines(lnI) + CHR(13) + CHR(10)
ENDFOR 

STRTOFILE(lcText, <path to output file>)

As always, the above is not meant to be finished code. It's just to give you the general idea.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
But keep in mind that if you are doing this within an executable program, you won't necessarily have access to the PRG. This would normally be compiled into an FXP, which would then be bound into the EXE. The only way to edit the PRG from within the executable would be to distribute the PRG separately (with all that that implies for security).

On the other hand, if your aim is to simply find the line number that caused an error, so that you can edit the PRG in the development environment, you should use the LINENO() function for that.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Ah, I understood that you wanted to edit the line programmatically. If you just want to open the file programmatically, but edit it interactively, then of course Tore's solution is the one to go for. (But, again it assumes you are working in the development environment.)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Also be aware that line numbers reported by error messages in VFP have always appeared to be approximations to me. [bigsmile]
 
Thanks tbleken,this was what I exactly wanted.

 
You are right Mike,I am working in the development environment
 
Dan, a mismatch rather points out the sources changed in comparison to the compiled prgs. Also there is the difference of LINENO() vs LINENO(1), counting from top of a file or counting from top of a single Procedure/Function.

Besides a reported line number may also be 0 in case of errors happening on eg evaluating properties or within some stored proc triggered, besides errors at starting a form or instanciating a class can point to row numbers of SCX/VCX files, but that's off topic of line number approximations.

But this can also be about providing to jump into editing a PRG from found places as Code References does or GoFish.

Bye, Olaf.


 
I require this as I know line no. where error occurred , on entering ok button I want to go to that line no. in program file
I am working in the development environment

I guess that I am not clear as to why you want to go to that line PROGRAMMATICALLY.

Since you are in the Development Environment, if you have the PRG file open, all you have to do is to go to the VFP Top-Menu - Edit - Go To Line...
Once there you enter the line number and you are taken to that line in the PRG file.

Sure it is not a Programmatic approach, but it takes just about the same amount of time (or less) as launching some other program to do it for you - and it already exists for your use.

Good Luck,
JRB-Bldr

 
Since you are in the Development Environment, if you have the PRG file open, all you have to do is to go to the VFP Top-Menu - Edit - Go To Line...
Once there you enter the line number and you are taken to that line in the PRG file.

I can see this approach being useful in an error handler that detects whether you're running in runtime or in the IDE. Seems like a lot more trouble than it's actually worth by the time you handle errors in PRG, SCX, VCX, etc., but hey .... give a kid a toy!
 
Why not simply select Suspend when an error occurs? Then VFP will stop exactly where the error is, and you can change the code immediately.
 
I have an error handler in place using a messagebox. That lacks a Suspend button, but I do SET STEP ON, so I can be in the spot of the error. It's often enough too late to prevent the error or fix some variables and settings at debugtime, to see what's missing in previous code, but often enough that indeed IS possible. You can't really edit the classes or PRGS or SCXEs loaded into memory, though.

I rather see EditSource() as agood facility for entering code from a central search facility, and there may be some other cases than a simple search, eg finding places for refactoring and opening them up in an editor. The automation of editing sources does not need to end at that step, once open, you can also modify source code programmatically. Though you may also do that via FILETOSTR, ALINES and later writing the changed PRG back, there are ways to automate the editor itself, as Christof Wollenhaupt demonstrated with ISX (see second download of before Intellisense was introduced in VFP7, you have some possibilities derived from foxtools.fll, starting with _AGetEnv() and some more undocumented foxtools functions (at least in a current foxtools.chm). Overall a more elegant and official way to modify code than via eg HackSCX/VCX. You don't change the Code memo, you EditSource of a class or form and use foxtools functions to edit code and save.

Bye, Olaf.
 
I have an error handler which keeps track of errors with prog. and lineno in runtime. I have a short program to check those errors scanning that file and the place where I press specific key, that program is opened. I wanted my cursor to take at that line no. where error occured in exe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top