Results 1 to 7 of 7

Thread: Problem writing files

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 1999
    Location
    Sydney NSW Australia
    Posts
    23
    When I write to a file using output access it always puts a return at the end of the text and makes it go to the next line. This is causing problems in my program. It's an encryption program that writes encrypted text to a file, and then can decrypt it. But because the return at the end is not encrypted, it decrypts to the wrong character.

    This is the code I'm using to write it:

    Open CurrentFile For Output As #1
    Print #1, txtEncrypt.Text
    Close #1

    Anyone know how to make it not add the return at the end of the file?

    Thanks

  2. #2
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425
    VB does this by default. If you don't want a return at the end of the line, try writing to the file in Random mode.

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'example uses a textbox Text1
    '2 command buttons, Command1, Command2
    '
    'encrypts and decrypts a file showing results in textbox
    '
    'use write instead of print when writing to the file
    'this will remove the unwanted characters at end of file.
    '
    
    Option Explicit
    'Encrypt/Unencrypt a file
    '
    Private Function EncryptFile(sFile As String, iKey As Integer)
    
        Dim iFake#
        Dim x As String * 1
         
        Dim lP As Long, z
        Dim intnum#
        intnum = FreeFile
    
        iFake = Rnd(-1)
        Randomize (iKey)
         
         
        lP = 1
        Open sFile For Binary As intnum
         
        While lP <= LOF(intnum)
         
        Get #intnum, lP, x
         
        z = Asc(x) + Int(Rnd * 256)
        If z > 255 Then z = z - 256
         
        x = Chr(z)
         
        Put #intnum, lP, x
         
        lP = lP + 1
         
         
        Wend
         
        Close #intnum
    
        MsgBox "Your file has been encrypted."
    
    End Function
    
    
    Private Function DecryptFile(sFile As String, iKey As Integer)
    
        Dim iFake As Integer
        Dim x As String * 1
         
        Dim lP As Long, z
        Dim intnum#
        
        intnum = FreeFile
      
        iFake = Rnd(-1)
        Randomize (iKey)
         
         
        lP = 1
        Open sFile For Binary As #intnum
         
        While lP <= LOF(intnum)
         
        Get #intnum, lP, x
         
        z = Asc(x) - Int(Rnd * 256)
        If z < 0 Then z = z + 256
         
        x = Chr(z)
         
        Put #intnum, lP, x
         
        lP = lP + 1
         
        Wend
         
        Close #intnum
        
        MsgBox "Your file has been unencrypted."
        
    End Function
    
    Private Sub Command2_Click()
        
        Text1 = ""
        
        Dim intnum As Integer
        intnum = FreeFile
        
        Dim CurFile As String
        CurFile = "C:\my documents\mytext.txt"
        
        Call DecryptFile(CurFile, 4)
          Open CurFile For Input As intnum
             Text1 = StrConv(InputB(LOF(intnum), intnum), vbUnicode)
          Close #1
    End Sub
    
    
    
    Private Sub Command1_Click()
        Text1 = ""
        
        Dim intnum As Integer
        intnum = FreeFile
        
        Dim CurFile As String
        CurFile = "C:\my documents\mytext.txt"
        
        Text1 = "Help me out here will you." & vbCrLf
        Text1 = Text1 & "Ok, I'll try." & vbCrLf
        Text1 = Text1 & "Hope this works."
        
        Open CurFile For Output As intnum
           Write #intnum, Text1
        Close intnum
       
        Call EncryptFile(CurFile, 4)
        
        Text1 = ""
        
        Open CurFile For Input As intnum
        MsgBox LOF(intnum)
            Text1 = StrConv(InputB(LOF(intnum), intnum), vbUnicode)
         Close #1
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 1999
    Location
    Sydney NSW Australia
    Posts
    23
    HeSaidJoe, thanks but I've already done my own encryption and decryption code.

    I just need to know how to stop VB from adding a return at the end of the file when i write it.

    Thanks

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 1999
    Location
    Sydney NSW Australia
    Posts
    23
    And also, using Write instead of Print still puts a return at the end.

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    the example tells you that..take only what you need.
    check the write functions as compared to yours.

    I like to check before I post...that's why it's all there.



    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 1999
    Location
    Sydney NSW Australia
    Posts
    23
    ok thanks ive worked it out.
    i just needed to change the code to this:

    Open CurrentFile For Binary As #1
    Put #1, 1, txtEncrypt.Text
    Close #1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width