Moving rows

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

Moving rows

Post by gerry »

Hi!

I have a file test.txt and it contains text on several lines

Code: Select all

1. test1
2. test2
3. test3
3. test4
4. test5
The file can be larger and with a large number of lines (this is just an example!)

I need to move the first 10 kilobytes to the end of the file and then return to the original state before the change

Thanks!
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Moving rows

Post by dodicat »

Keep it simple with strings I would say.

Code: Select all



Function savefile(filename As String,p As String) As String
      Dim As Long n=Freefile
      If Open (filename For Binary Access Write As #n)=0 Then
            Put #n,,p
            Close
      Else
            Print "Unable to save " + filename:Sleep:End
      End If
      Return filename
End Function

Function loadfile(filename As String) As String
      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":Sleep:End
      End If
End Function

Function pre_pend(filename As String,txt As String) As String
      Dim As String s=loadfile(filename)
      If Len(s) Then savefile(filename,txt+s)
      Return filename
End Function

Function ap_pend(filename As String,txt As String) As String
      Dim As String s=loadfile(filename)
      If Len(s) Then savefile(filename,s+txt)
      Return filename
End Function

'==========================================================
Sub startToend(filename As String,size As Long)
      Var L=loadfile("teststring")
      Var fpart=Mid(L,1,size)
      Var epart=Mid(L,size+1) 
      savefile("teststring",epart)
      ap_pend("teststring",fpart)
End Sub


Sub endTostart(filename As String,size As Long)
      Var L=loadfile("teststring")
      Var fpart=Mid(L,1,Len(L)-size)
      Var epart=Mid(L,Len(L)-size+1)
      savefile("teststring",fpart)
      pre_pend("teststring",epart)
End Sub


Dim As String s
For n As Long=1 To 1000
      s+=Str(n)+".     "+"test"+Str(n)+Chr(10)
Next

savefile("teststring",s)

startToend("teststring",10000)

Shell "notepad teststring"

endTostart("teststring",10000)
Shell "notepad teststring"


Sleep

Kill ("teststring")

 
Last edited by dodicat on Sep 07, 2022 10:14, edited 2 times in total.
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Moving rows

Post by gerry »

dodicat wrote: Sep 06, 2022 19:07 Keep it simple with strings I would say.

Code: Select all


Function savefile(filename As String,p As String) As String
      Dim As Long n=Freefile
      If Open (filename For Binary Access Write As #n)=0 Then
            Put #n,,p
            Close
      Else
            Print "Unable to save " + filename:Sleep:End
      End If
      Return filename
End Function

Function loadfile(filename As String) As String
      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":Sleep:End
      End If
End Function

Function pre_pend(filename As String,txt As String) As String
      Dim As String s=loadfile(filename)
      If Len(s) Then savefile(filename,txt+s)
      Return filename
End Function

Function ap_pend(filename As String,txt As String) As String
      Dim As String s=loadfile(filename)
      If Len(s) Then savefile(filename,s+txt)
      Return filename
End Function


Dim As String s
For n As Long=1 To 1000
      s+=Str(n)+".     "+"test"+Str(n)+Chr(10)
Next

savefile("teststring",s)

Var L=loadfile("teststring")

Var fpart=Mid(L,1,10000)

Var epart=Mid(L,10001)

savefile("teststring",epart)
ap_pend("teststring",fpart)


Print "Length of chunk to append ";Len(fpart)
Shell "notepad teststring"



Sleep
'savefile("teststring",L) '' original if required 
Kill ("teststring")


 
Thanks!
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Moving rows

Post by gerry »

dodicat wrote: Sep 06, 2022 19:07 Keep it simple with strings I would say.

Code: Select all


Function savefile(filename As String,p As String) As String
      Dim As Long n=Freefile
      If Open (filename For Binary Access Write As #n)=0 Then
            Put #n,,p
            Close
      Else
            Print "Unable to save " + filename:Sleep:End
      End If
      Return filename
End Function

Function loadfile(filename As String) As String
      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":Sleep:End
      End If
End Function

Function pre_pend(filename As String,txt As String) As String
      Dim As String s=loadfile(filename)
      If Len(s) Then savefile(filename,txt+s)
      Return filename
End Function

Function ap_pend(filename As String,txt As String) As String
      Dim As String s=loadfile(filename)
      If Len(s) Then savefile(filename,s+txt)
      Return filename
End Function


Dim As String s
For n As Long=1 To 1000
      s+=Str(n)+".     "+"test"+Str(n)+Chr(10)
Next

savefile("teststring",s)

Var L=loadfile("teststring")

Var fpart=Mid(L,1,10000)

Var epart=Mid(L,10001)

savefile("teststring",epart)
ap_pend("teststring",fpart)


Print "Length of chunk to append ";Len(fpart)
Shell "notepad teststring"



Sleep
'savefile("teststring",L) '' original if required 
Kill ("teststring")


 
Only you need to return the modified file to its original state and not create and just save it
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Moving rows

Post by dodicat »

gerry wrote: Sep 06, 2022 21:46
dodicat wrote: Sep 06, 2022 19:07 Keep it simple with strings I would say.
...
Only you need to return the modified file to its original state and not create and just save it
I have modified the code (added two subs to automate the process)
startToend .. flipping the size (bytes) from file start to file end

endTostart .. flipping the size (bytes) from file end to file start.
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Moving rows

Post by gerry »

dodicat wrote: Sep 07, 2022 10:15
gerry wrote: Sep 06, 2022 21:46
dodicat wrote: Sep 06, 2022 19:07 Keep it simple with strings I would say.
...
Only you need to return the modified file to its original state and not create and just save it
I have modified the code (added two subs to automate the process)
startToend .. flipping the size (bytes) from file start to file end

endTostart .. flipping the size (bytes) from file end to file start.
Thanks!
Post Reply