Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21090

Creating File & Writing Data Causes a Permissions Error

$
0
0
I get a "permission denied" error while running the following code.

vb6 Code:
  1. Public Function DeleteLines(ByVal from_name As String, ByVal to_name As String, ByVal target As String) As Long
  2.     Dim strlen As Integer
  3.     Dim from_file As Integer
  4.     Dim to_file As Integer
  5.     Dim one_line As String
  6.     Dim deleted As Integer
  7.  
  8.     ' Open the input file.
  9.     from_file = FreeFile
  10.     Open from_name For Input As from_file
  11.  
  12.     ' Open the output file.
  13.     to_file = FreeFile
  14.     Open to_name For Output As to_file
  15.  
  16.     ' Copy the file skipping lines containing the
  17.     ' target.
  18.     deleted = 0
  19.    
  20.     Do While Not EOF(from_file)
  21.         Line Input #from_file, one_line
  22.         If InStr(one_line, target) = 0 Then
  23.             Print #to_file, one_line
  24.         Else
  25.             deleted = deleted + 1
  26.         End If
  27.     Loop
  28.  
  29.     ' Close the files.
  30.     Close from_file
  31.     Close to_file
  32.    
  33.     Kill (from_name)
  34.     Name to_name As from_name
  35.    
  36.     DeleteLines = deleted
  37. End Function

It happens on Print #to_file, one_line. Something must have changed, because it was working fine before. But I don't see anything that could've affected this. So just for clarifiction: the code creates a new file from target. This is working correctly. Then it's supposed to copy the lines from the old file (from_name) to the new file and then deletes the old file. The copying part is where it's failing.

Thanks

Viewing all articles
Browse latest Browse all 21090

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>