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!

how to get values from a notepad? 2

Status
Not open for further replies.

Mandy_crw

Programmer
Jul 23, 2020
578
0
16
PH
Hi everyone... Good day.... I have here a code in my program... may i ask if can get the value of commport from a notepad file? If so, Can you teach me how? Thanks and God bless....

DEFINE CLASS mySMS as Container

nTimeOut = 10
CommPort = 9
code,,,,
code.....

endefine
 
Can we assume that your "notepad file" is just an ordinary text file? If so, it is easy to read the contents into a VFP variable:

[tt]lcText = FILETOSTR("c:\data\myfile.txt")[/tt]

(substituting the actual filename and path, of course).

Once you have the contents of the file in lcText, you will need to pick out the value of your comm port. How you do that will depend on exactly what text is contained in the file. If you can post a (small) sample, we should be able to advise further.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Got it Mike!!!! Thank you so much.... God bless,...
 
Problem solved, I see.

If the "notepad file" is actually containing the DEFINE CLASS, well, that's a VFP code file, a PRG with VFP code of a class and you create an object of that to use it. Let's assume the file is sms.prg

These lines directly after DEFINE CLASS are class properties:
Code:
DEFINE CLASS mySMS as Container
[highlight #FCE94F]nTimeOut = 10
CommPort = 9[/highlight]


For example it's possible to use NEWOBJECT or CREATEOBJECT with this and get at the properties this way
Code:
LOCAL loSMS
loSMS = NEWOBJECT('mySMS','\some\path\to\sms.prg')
* Now, after that class is instanciated as loSMS, the properties can be read out:
? loSMS.CommPort
? loSMS.nTimeOut

The alternative with CREATEOBJECT needs a SET PROCEDURE to the PRG file first, just the same as we teached you to use when using a function that's defined in a PRG. So anytime you have a PRG you can't simply DO as it only contains definitions of classes or functions, but even if it just is code you can call directly, it's never wrong to SET PROCEDURE to such a prg file.

I just wonder if that's relevant, as you talked of a "notepad" and not a PRG. A "notepad" actually isn't really a sensible term.

Anyway, in the case the part you posted after your question was the "notepad", it's code you should use as code and not as text.

Chriss
 
Thank you Chris and Mike... I might have reacted to soon.... Mike is this correct.. it returned "varaiable not found" please help..
the comport notepad file contains number "5"

DEFINE CLASS mySMS as Container

komport = FILETOSTR("C:\comport.txt")

nTimeOut = 10
CommPort = komport

ENDDEFINE
 
Yes, Mandy, I can see that that won't work. That's because komport is not a variable; it is a property of the class.

But the following should be OK:

Code:
DEFINE CLASS mySMS as Container

nTimeOut = 10
CommPort = FILETOSTR("C:\comport.txt")

ENDDEFINE

Alternatively, you could do it like this:

Code:
DEFINE CLASS mySMS as Container

komport = FILETOSTR("C:\comport.txt")

nTimeOut = 10
CommPort = [highlight #FCE94F]this.[/highlight]komport

ENDDEFINE

Give it a try.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
If CommPort needs to be a numeric value, just reading in the file won't work.

If you really have a comport.txt file containing only a 5, this still is a string read in by FILETOSTR(), as the name of the function tells, it reads a file to a string variable. There are much better ways to store configuration values, for example as you use VFP a DBF. Sure, a DBF with a numeric field storing a 5 would be much larger than the single byte txt file, but it's obviously not the end of what configuration values can be in data.

Another very old school way is the INI format, it's easy to maintain, as it's just a text file, too, and therefore still in use in many cases, though today many configurations are stored into XML files, still also text, but not very maintenance friendly for just using an editor. But there are also many new tools that format XML for easier handling.

I'd consider moving away from the single number in a text file, to anything else, but for the moment you'd need VAL(FILETOSTR("comport.txt")) as your comport number.

Chriss
 
Chris is right. Any value that you read in with FILETOSTR() will be stored as a string - as the name of the function indicates. If your CommPort property expects a number (which it almost certainly does), you need to use the VAL() function to convert it.

Sorry I didn't spot that earlier.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks Mike and Chriss… its working now… while taking note of all your tips… God bless…
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top