3057: Variable could not be converted to a valid number.

Started by jmburton2001, May 15, 2018, 05:55:59 PM

Previous topic - Next topic

jmburton2001

Please assume I know nothing and really need your help. I've been reading and searching for the better part of 3 hours because I figure it's a really simple thing. Even so, I've looked at, and studied it to the point that I know I'm blind to the solution at this point...

filename = "C:\Binary Read Test\abc.txt"
StrToFind = "Exiting importMxfFile"
OutputFile = "C:\Binary Read Test\Output.txt"
size = FileSize(filename)
hnd = BinaryAlloc(size)
ret = BinaryRead(hnd,filename)
start = 0

While @TRUE
 
  found = BinaryIndexEx(hnd,start,@CRLF,@FWDSCAN,0)
  If found == -1
    If start >= size - 1
      Break
    EndIf
    found = size - 1  ; last line does not end in a crlf
  EndIf
  line = BinaryPeekStr(hnd,start,found-start+1)
  Foundit = StrIndexNC (line,StrToFind,0,@FWDSCAN) ;This returns an integer greater than 1
  If Foundit != "0" Then FileOpen (OutputFile, "WRITE") ;This creates the OutputFile
  If Foundit != "0" Then FileWrite(OutputFile, line) , ;This returns Error 3057 (Argument number 1 could not be converted)
  If Foundit != "0" Then FileClose (OutputFile)

  ;Message("read",line)
 
  start = found + 2
  If start >= size Then Break
 
EndWhile

BinaryFree(hnd)

Exit


The FileOpen line creates the OutputFile without fail. It's the next line "FileWrite(OutputFile, line)" that keeps giving me the 3057: Variable could not be converted to a valid number. error.

At the point it fails Foundit = 23 which (according to the old math) is not equal to 0. I've tried it as 0, "0", a predefined variable.

All I'm trying to do is write out the "line" that matches "StrToFind" into an output file. I've always used the FileRead loop for this type of analysis, but the log files I'm working with now are getting pretty big and I wanted to see if the binary operations would speed up processing.

I'm sure there's a more elegant way to do this but I'm stumped on this... Thank you in advance for your wisdom!

JTaylor

You need to use FileOpen() and then use that with FileWrite().

Jim

td

The Consolidated WIL Help file topic for FileOpen states:

Returns:
(i) filehandle.

The filehandle returned by the FileOpen function may be subsequently used by the FileRead, FileWrite, and FileClose functions.

For FileWrite the help file states:

Parameters:
(i) filehandle same integer that was returned by FileOpen.  <---------------------------

(s) output-data data to write to file.

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

jmburton2001

Thank you for your replies...

For my future reference:

filename = "C:\Binary Read Test\abc.txt"
StrToFind = "Exiting importMxfFile"
OutputFile = "C:\Binary Read Test\Output.txt"
OutputFile = FileOpen ("C:\Binary Read Test\Output.txt", "WRITE")
size = FileSize(filename)
hnd = BinaryAlloc(size)
ret = BinaryRead(hnd,filename)
start = 0

While @TRUE

  found = BinaryIndexEx(hnd,start,@CRLF,@FWDSCAN,0)
  If found == -1
    If start >= size - 1
      Break
    EndIf
    found = size - 1  ; last line does not end in a crlf
  EndIf
  line = BinaryPeekStr(hnd,start,found-start+1)
  Foundit = StrIndexNC (line,StrToFind,0,@FWDSCAN)
If Foundit != "0" Then FileOpen (OutputFile, "WRITE")
  If Foundit != "0" Then FileWrite(OutputFile, line)
If Foundit != "0" Then FileClose (OutputFile)

  ;Message("read",line)

  start = found + 2
  If start >= size Then Break

EndWhile

BinaryFree(hnd)
FileClose (OutputFile)

Exit