Ask a Question

Reading a text file and passing to a Function

SOLVED
NB1024
New Contributor

Reading a text file and passing to a Function

Hi All,

 

Hopefully a quick question,

 

Im trying to read a number from a text file and pass it to a function.

 

The "ScanItem" function i am trying to pass the textfile data to already works fine in tests and is below.

However currently it relies on specifying the value to read in in the script.

I would like to pass in the numerics into the scanitem as read from the text file.

Do I need to look at pointers or anything?

 

Any examples would be a great help.

 

Thanks for your time!

 

Pre exisiting ScanItem function

===============================

Sub ScanItem(NUM)
Call Delay(350)
Dim SerialPort, Port, BaudRate, Parity, DataBits, StopBits
Port = "COM11"
BaudRate = 9600
Parity = None
DataBits = 8
StopBits = 1
Call Delay(250)
Set SerialPort = dotNet.System_IO_Ports.SerialPort.zctor(Port, BaudRate, Parity, DataBits, StopBits)
SerialPort.Open
SerialPort.Write NUM & Chr(13)
SerialPort.Close
End Sub

 

 

What Ive tried

========================

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, MyFile, FileName, TextLine

Set fso = CreateObject("Scripting.FileSystemObject")

 

' Open the file for output.
FileName = "c:\temp\textfile1.txt"

Set MyFile = fso.OpenTextFile(FileName, ForWriting, True)

 

' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)

 

' Read from the file and display the results.
Do While MyFile.AtEndOfStream <> True
TextLine = MyFile.ReadLine


'Document.Write TextLine & "<br />"

 

Loop
MyFile.Close

 

ScanItem (TextLine) 

 

4 REPLIES 4
shankar_r
Community Hero

Hi,

 

@NB1024 wrote


Im trying to read a number from a text file and pass it to a function.


  • Only number will get displayed in your text file or how it will look like?
  • Are you going to use whole text of a file as parameter?
  • If not, How you know what value to take?

Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”

Extension Available

Thanks Shankar.

 

Please see clarification below:

  • Only number will get displayed in your text file or how it will look like?
    • in the text file c:\temp\voucher1.txt when opened in e.g.notepad.exe would show just a 13 digit number e.g. 
    • 1234567890123
  • Are you going to use whole text of a file as parameter?
    • correct I would like to read in the 1234567890123 and pass the 13 digit number into scanitem function.
  • If not, How you know what value to take?
    • see above.

 

 

Currently as per original post the ScanItem which works in scripts has this in the script

 

e.g.

ScanItem ("1112223334445")

 

however I would like to pass in the voucher1.txt files content of e.g. 1234567890123   as the script will need to handle dynamic content.

 

 

Regards

n

 

 

 

 

Hi,

 

If that is the case, you can do like below

 

function test()
{
         ScanItem(getTextFromFile(<filepath>))
}

function getTextFromFile(filepath)
{
	if(filepath != null)
	{
		try
		{
			return aqFile.ReadWholeTextFile(filepath,20);
		}
		catch(e)
		{
			Log.Error(ex.stack);
		}
	}
	return null;
}

Ref: https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfile/readwholetextfile.h...


Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”

Extension Available

Thanks Shankar!

This helped alot, especially the reference link too!

Kind Regards

N

 

 

cancel
Showing results for 
Search instead for 
Did you mean: