Results 1 to 9 of 9

Thread: Playing around with files

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    248

    Playing around with files

    I have just been playing around trying different things and thought I would get people's thoughts on it (not that anyone will be interested).

    I have found that you can append a lot of plain text on just about any file without harming the usefulness of the files. (bmp, jpg, gif, dll and even exe).

    I found that nothing prevents you from appending text to the open app's exe file and reading from it. So basically you could store the settings in the exe file by just appending it. (I imagine you would have to be very careful not to erase any part of the file).

    Like I said it is just things I was playing with,

    Mike

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Playing around with files

    Was this something you did just for the heck of it or did you have an end result in mind?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    248

    Re: Playing around with files


    As I said I was just fooling around.
    Why?

    That is why I posted it in this forum instead of a programming forum.

    It does seem, to me at least, that it would have a usefulness.

    Mike

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Playing around with files

    Didn't work for me. I made a crude BMP in MS Paint and added "blahblahblah" to the end, it corrupted the file. I didn't think it would work either, because of the binary-text representation and interchange going on. Got any small examples of what you did? (Give us .jpg or .bmp, no .exes)

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Playing around with files

    Here's one, your logo with 'hello peeps!' at the end,

    edit: here was one.. it's gone awol, hang on no it hasn't, ignore me.
    Attached Images Attached Images  
    Last edited by Milk; Mar 18th, 2008 at 05:53 PM.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Playing around with files

    I opened that file in notepad, then immediately saved it without modifying anything. That corrupted the image. I suppose the next question would be, what did you use to add the text?

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Playing around with files

    Just a hex editor.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    248

    Re: Playing around with files

    Yes if you save it in a text file it will corrupt. It is still a binary file.

    This,

    Open "MyFile.bmp" for append as #1

    ? #1, "This is my string."

    Close #1

    This appends the text.
    Now open the gif in notepad and you will see it at the bottom.
    Close notepad without saving and open the gif in IE.
    The gif looks fine.

    I found that you can't save to the running app (since the app is write protected when it is running), but you can read from it.

    So if I had a key or something that I had appended to my EXE I can have code in that app read the end of the app to get the key (or whatever).

    The big thing is you must always add the information after the end of the file, if you put it anywhere else it will mess it up (JPG and maybe Gif you can, since you can change a bit data - but it will cause the picture to change).

    Mike
    Attached Images Attached Images   
    Last edited by MikeJoel; Mar 15th, 2008 at 05:55 PM.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    248

    Re: Playing around with files

    Here is a quick app to see it at work (it was done quickly so please no comments on the coding )

    The form should have two command buttons and 1 text box set to multiline.
    Also place a BMP file (test.bmp) in the folder with the app.

    Code:
    Dim MyString As String * 45
    
    
    Private Sub Command1_Click()
    
    Dim StartArea As Long
    
    StartArea = GetStartPos(App.Path & "\test.bmp")
    
    Open App.Path & "\test.bmp" For Append As #1
    
    If (StartArea > 0) Then
      Seek #1, StartArea
    
    Else
      MyString = "[START]"
      Print #1, MyString
    End If
    
    MyString = "This is line " & Int(Rnd * 2000)
    Print #1, MyString
    MyString = "This is line " & Int(Rnd * 2000)
    Print #1, MyString
    MyString = "This is line " & Int(Rnd * 2000)
    Print #1, MyString
    MyString = "This is line " & Int(Rnd * 2000)
    Print #1, MyString
    
    Close #1
    
    End Sub
    
    
    
    Private Sub Command2_Click()
    Text1.Text = ""
    
    Dim StartArea As Long
    
    Dim LineIne As String
    Dim FindStart As String
    
    StartArea = GetStartPos(App.Path & "\test.bmp")
    
    Open App.Path & "\test.bmp" For Input As #1
    
    If (StartArea > 0) Then
      Seek #1, StartArea
    End If
    
    Do Until EOF(1)
      Input #1, MyString
      Text1.Text = Text1.Text & Trim(MyString) & vbCrLf
    Loop
    Close #1
    
    End Sub
    
    Public Function GetStartPos(MyFile As String) As Long
    Dim LineIne As String
    Dim FindStart As String
    Dim StartPos As Long
    Dim AFreeFile As Integer
    AFreeFile = FreeFile
    
    
    StartPos = 0
    
    Open App.Path & "\test.bmp" For Input As #AFreeFile
    
    Do Until EOF(AFreeFile)
      If (InStr(1, FindStart, "[START]") <= 0) Then
        Input #AFreeFile, LineIne
        FindStart = FindStart & LineIne
      Else
        StartPos = Seek(AFreeFile)
        Exit Do
      End If
    Loop
    
    Close #AFreeFile
    
    GetStartPos = StartPos
    
    End Function
    It checks if the file has a "[START]" string in it. If not it will be the first item written in. From then on it uses that as a reference for reading and writing.

    Using another method would probably be faster but, like I said, I just did this up as an example.


    Mike

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