Qbasicnews.com

Full Version: VB Simple file problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was writing a simple XOR encryption routine in VB 6 that encrypts a file, and I ran into a problem. Here's my code:
Code:
Dim inbyte As String
If infile.Text = "" Or outfile.Text = "" Then
  MsgBox "You must specify both an Infile and an Outfile.", vbExclamation, "Error"
  Exit Sub
End If
If code.Text = "" Then
  MsgBox "You must specify a code.", vbExclamation, "Error"
  Exit Sub
End If
If Val(code.Text) < 0 Or Val(code.Text) > 255 Then
  MsgBox "Code specified must be between 0 and 255.", vbExclamation, "Error"
  Exit Sub
End If
Open infile.Text For Binary As #1
Open outfile.Text For Binary As #2
For i = 1 To LOF(1)
  Get #1, , inbyte
  Put #2, , Chr(Asc(inbyte) Xor Val(code.Text))
Next
Close #1, #2
I get a runtime error: "Invalid Procedure Call or Argument" on the Put #2 line.
What wrong?
Try

Code:
Char$ = Chr(Asc(inbyte) Xor Val(code.Text))
Put #2, ,Char$
Why? I'll try it in a minute, I can't at the moment, but would it make a difference?
[EDIT]No, I tried it, and I got the same error.[/EDIT]
in QB at least, you can't use PUT with an expression, it has to be a variable. Dunno about VB...
Hmm...Well, that's obviously not the case in VB.
Anyway, I'll look ask around about this problem. It driving my crazy :evil:
Code:
Dim inbyte As String * 1, okbyte As String * 1
Open infile.Text For Binary As #1
Open outfile.Text For Binary As #2
   l& = LOF(1)
   For i = 1 To l&
      Get #1, , inbyte
      okbyte = Chr$(Asc(inbyte) Xor Val(code.Text))
      Put #2, , okbyte
   Next
Close
Remember to define inbyte and okbyte as String * 1 Wink, and not to have any expression in Put #2. So actually what Plasma said, just 1 variable. Wink
Why not just use the Byte type in VB6? Since you're performing a logical operation on it anyways, might as well use a numeric data type. Big Grin
Quote:
Code:
Dim inbyte As String * 1, okbyte As String * 1
Open infile.Text For Binary As #1
Open outfile.Text For Binary As #2
   l& = LOF(1)
   For i = 1 To l&
      Get #1, , inbyte
      okbyte = Chr$(Asc(inbyte) Xor Val(code.Text))
      Put #2, , okbyte
   Next
Close
Remember to define inbyte and okbyte as String * 1 Wink, and not to have any expression in Put #2. Not even what Plasma said, just 1 variable. Wink
Thanks!
Quote:Not even what Plasma said, just 1 variable. Wink

Huh? Isn't that what I said?
Oh, sorry, I misread. Wink It's fixed now. :oops: