Writing a file without line feed.
#1
I want to replace text strings of a binary file (a P2000T .CAS file). Unfortunately, QB64 adds a line feed character. When I use "PRINT" it adds a CHR$(13) or CHR$(10), when I use "WRITE" it put a double quote at the beginning of the file. How can I prevent this?


Code: (Select All)
[quote]
oldstring$ = "beatles"
newstring$ = "stones"
filename$ = "original.BIN"
oldlength% = Len(oldstring$)
newlength% = Len(newstring$)
f1% = FreeFile
f2% = f1% + 1
Open filename$ For Binary As f1% 'Bron
Open "replace.tmp" For Output As f2% 'Temp
While Not EOF(f1%)
    Line Input #f1%, line$
    lineptr% = 1

    Do
        foundptr% = InStr(lineptr%, line$, oldstring$)
        If foundptr% > 0 Then
            line$ = Left$(line$, foundptr% - 1) + newstring$ + Mid$(line$, foundptr% + oldlength%)
            'Vervang oude door nieuwe string
            lineptr% = foundptr% + newlength%
            If lineptr% > Len(line$) Then Exit Do
        Else Exit Do ' Oude string niet gevonden, door naar volgende regel
        End If
    Loop
    Print #f2%, line$ ' Regel toevoegen
Wend

Close f1%: Close f2%
Kill filename$ ' Wis bronbestand
Name "replace.tmp" As filename$ ' Temp wordt nu bron

[/quote]
Reply
#2
You could write your file at end of processing as one big long line with Chr$(10) or Chr$(13) delimiters for lines, file it as Binary too.
b = b + ...
Reply
#3
Hi,

it's very easy, just put an semi-colon behind the print:
Code: (Select All)
PRINT "Hello" 'this will actually print Hello +  CHR$(13) + CHR$(10)
PRINT "Hello"; 'this will suppres CHR$(13) + CHR$(10), hence just printing Hello
Of course, this also works with file printing PRINT #.....;
Reply
#4
I tried that. When I type "Print #f2%, line$;" it still adds a character. Sad
Reply
#5
Oh load file as string, use replace$ sub and then file as string, even more efficient!
b = b + ...
Reply
#6
Do you have an example? Does that work with file names of any size?
Reply
#7
It shows up here.
[Image: dezedeze.png]
Reply
#8
(07-08-2022, 05:08 PM)SquirrelMonkey Wrote: I tried that. When I type "Print #f2%, line$;" it still adds a character. Sad

That's odd, then I'd say that character is already part of line$ and not added by the PRINT, but as far as I see you use LINE INPUT, which usually skips CRLF.

However, try this:
Code: (Select All)
OPEN "B", #1, "YourOriginalFile"
orgDat$ = SPACE$(LOF(1))
GET #1, , orgDat$
CLOSE #1

newDat$ = StrReplace$(orgDat$, "oldString", "newString")

OPEN "B", #1, "YourNewFile"
PUT #1, , newDat$
CLOSE #1

'include QB64's string tools
'$INCLUDE: 'source\utilities\string.bas'
Just replace the filenames in the OPEN lines and the strings in the StrReplace$() call.
Reply
#9
Roughly,

Open file for binary as #1
buf$ = space$(LOF(1))
get #1, , buf$
close #1

flie$ = replace$(buf$, old$, new$) ' find replace in QB64.exe code files

Open NewFile for binary as #1
put #1, , file$
close #1

' check new file
kill oldfile
name  Newfile as OldFile ' EDIT: help has old and new the other way and confused me but you want the new file to have the old name.

Faster than digging around QB64 files:
Code: (Select All)
Function strReplace$ (s$, replace$, new$) 'case sensitive  2020-07-28 version
    Dim p As Long, sCopy$, LR As Long, lNew As Long
    If Len(s$) = 0 Or Len(replace$) = 0 Then
        strReplace$ = s$: Exit Function
    Else
        LR = Len(replace$): lNew = Len(new$)
    End If

    sCopy$ = s$ ' otherwise s$ would get changed
    p = InStr(sCopy$, replace$)
    While p
        sCopy$ = Mid$(sCopy$, 1, p - 1) + new$ + Mid$(sCopy$, p + LR)
        p = InStr(p + lNew, sCopy$, replace$)
    Wend
    strReplace$ = sCopy$
End Function
b = b + ...
Reply
#10
Wow, that worked! Thank you so much!
Reply




Users browsing this thread: 4 Guest(s)