Processing file contents in different places

New to FreeBASIC? Post your questions here.
Post Reply
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Processing file contents in different places

Post by gerry »

Hi!

Task: I have a folder and there are several text files of 10 kilobytes in it

I need to translate the contents of files in different places using the StringToHex function. (At the beginning of the file, at the end of the file and somewhere in the middle)

That is, random sections of the file should be processed

And of course, so that it could all be returned to its previous state

I will be grateful for any solutions to the issue.. :)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Processing file contents in different places

Post by dodicat »

Here is an outline of altering somewhere in the middle.
The hex size is saved with the altered file, and retrieved when loading the altered file.

Code: Select all

Function StrToHex (Byref convstr As String) As String
    Dim as string c
    c = convstr
    Dim As Uinteger i
    Dim As String ftext
    For i = 1 To Len(c)
        ftext += Hex(Asc(Mid$(c,i,1)),2)
    Next i
    Return ftext
End Function

Function HexToStr (Byref convstr As String) As String
    Dim as string c
    c = convstr
    If Len(c) Mod 2 = 1 Then c += "0"
    Dim As Uinteger i
    Dim As String   f
    For i = 1 To Len(c) Step 2
        f += Chr$(Val("&H"+Mid$(c,i,2)))
    Next i
    Return f
End Function


Function loadfile(filename As String) As String export 'optional
      Dim As Long  f=Freefile
      If Open (filename For Binary Access Read As #f)=0 Then
            Dim As String text
            If Lof(f) > 0 Then
                  text = String(Lof(f), 0)
                  Get #f, , text
            End If
            Close #f
            Return text
      Else
            Print filename;" not found"
      End If
End Function

Sub savefile(filename As String,p As String) export 'optional
      Dim As Long f=Freefile
      If Open (filename For Binary Access Write As #f)=0 Then
            Put #f,,p
            Close #f
      Else
            Print "Unable to save " + filename
      End If
End Sub

function split(s as string,FirstPart as string,PartToChange as string,LastPart as string,start as long,lngth as long) as long
       FirstPart=mid(s,1,start-1)
      PartToChange=mid(s,start,lngth)
      LastPart=mid(s,start+lngth)
    return len(StrToHex(PartToChange))
end function 

function split2(s as string,a as string,b as string,n as long) as long
      a=""
      b=""
      for i as long=0 to n-1
            a+=chr(s[i])
      next
      for i as long=n-1+1 to len(s)-1
            b+=chr(s[i])
      next
    return len(StrToHex(a))
end function

dim as string g="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for n as long=1 to 8
      g+=g
next
print "File length (kb) ";len(g)\1000
print "Part to change at position 3 for 20 digits"
print "As follows:"+chr(10)+"Original file 1"+chr(10)+"changed file 2"+chr(10)+"back to original file 3"


dim as string PartToChange,EndPart,FirstPart

savefile("testfile.dat",g)

var L=loadfile("testfile.dat")
print "file 1 "; mid(L,1,80)

var size=split(L,FirstPart,PartToChange,EndPart,3,20)'start at position 3 for 20 digits

PartToChange=StrToHex(PartToChange)

savefile("testfile2.dat",str(size)+chr(0)+FirstPart+PartToChange+EndPart) 'Add the length of the hex bit to the beginning of the saved file
'=====================================================================
'some time later . . .
'=====================================================================
'New section -loading the file, extracting the hex part length and saving a new file (= original)
var L2=loadfile("testfile2.dat")

var size2=val(L2)'get the hex part length

 L2=mid(L2,1+instr(L2,chr(0)))'restore the string without hex length info
 print "file 2 "; mid(L2,1,80)


split(L2,FirstPart,PartToChange,EndPart,3,size2)'start at 3 for size2 digits

PartToChange=HexToStr(PartToChange)
savefile("testfile3.dat",FirstPart+PartToChange+EndPart)
L2=loadfile("testfile3.dat")
print "file 3 "; mid(L2,1,80)
print
print iif(loadfile("testfile.dat")=loadfile("testfile3.dat"),"OK","ERROR")

kill "testfile.dat"
kill "testfile2.dat"
kill "testfile3.dat"
sleep
  
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Processing file contents in different places

Post by gerry »

dodicat wrote: Feb 21, 2022 16:43 Here is an outline of altering somewhere in the middle.
The hex size is saved with the altered file, and retrieved when loading the altered file.

Code: Select all

Function StrToHex (Byref convstr As String) As String
    Dim as string c
    c = convstr
    Dim As Uinteger i
    Dim As String ftext
    For i = 1 To Len(c)
        ftext += Hex(Asc(Mid$(c,i,1)),2)
    Next i
    Return ftext
End Function

Function HexToStr (Byref convstr As String) As String
    Dim as string c
    c = convstr
    If Len(c) Mod 2 = 1 Then c += "0"
    Dim As Uinteger i
    Dim As String   f
    For i = 1 To Len(c) Step 2
        f += Chr$(Val("&H"+Mid$(c,i,2)))
    Next i
    Return f
End Function


Function loadfile(filename As String) As String export 'optional
      Dim As Long  f=Freefile
      If Open (filename For Binary Access Read As #f)=0 Then
            Dim As String text
            If Lof(f) > 0 Then
                  text = String(Lof(f), 0)
                  Get #f, , text
            End If
            Close #f
            Return text
      Else
            Print filename;" not found"
      End If
End Function

Sub savefile(filename As String,p As String) export 'optional
      Dim As Long f=Freefile
      If Open (filename For Binary Access Write As #f)=0 Then
            Put #f,,p
            Close #f
      Else
            Print "Unable to save " + filename
      End If
End Sub

function split(s as string,FirstPart as string,PartToChange as string,LastPart as string,start as long,lngth as long) as long
       FirstPart=mid(s,1,start-1)
      PartToChange=mid(s,start,lngth)
      LastPart=mid(s,start+lngth)
    return len(StrToHex(PartToChange))
end function 

function split2(s as string,a as string,b as string,n as long) as long
      a=""
      b=""
      for i as long=0 to n-1
            a+=chr(s[i])
      next
      for i as long=n-1+1 to len(s)-1
            b+=chr(s[i])
      next
    return len(StrToHex(a))
end function

dim as string g="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for n as long=1 to 8
      g+=g
next
print "File length (kb) ";len(g)\1000
print "Part to change at position 3 for 20 digits"
print "As follows:"+chr(10)+"Original file 1"+chr(10)+"changed file 2"+chr(10)+"back to original file 3"


dim as string PartToChange,EndPart,FirstPart

savefile("testfile.dat",g)

var L=loadfile("testfile.dat")
print "file 1 "; mid(L,1,80)

var size=split(L,FirstPart,PartToChange,EndPart,3,20)'start at position 3 for 20 digits

PartToChange=StrToHex(PartToChange)

savefile("testfile2.dat",str(size)+chr(0)+FirstPart+PartToChange+EndPart) 'Add the length of the hex bit to the beginning of the saved file
'=====================================================================
'some time later . . .
'=====================================================================
'New section -loading the file, extracting the hex part length and saving a new file (= original)
var L2=loadfile("testfile2.dat")

var size2=val(L2)'get the hex part length

 L2=mid(L2,1+instr(L2,chr(0)))'restore the string without hex length info
 print "file 2 "; mid(L2,1,80)


split(L2,FirstPart,PartToChange,EndPart,3,size2)'start at 3 for size2 digits

PartToChange=HexToStr(PartToChange)
savefile("testfile3.dat",FirstPart+PartToChange+EndPart)
L2=loadfile("testfile3.dat")
print "file 3 "; mid(L2,1,80)
print
print iif(loadfile("testfile.dat")=loadfile("testfile3.dat"),"OK","ERROR")

kill "testfile.dat"
kill "testfile2.dat"
kill "testfile3.dat"
sleep
  
Thanks! :)
It's a pity that there is no reputation system, I would always put +
Post Reply