WriteLine not doing what it says on the packet

BlitzMax Forums/BlitzMax Beginners Area/WriteLine not doing what it says on the packet

Mordax_Praetorian(Posted 2006) [#1]
I'm trying to change the last character in a line in a file (Opened using OpenFile and pointed to by the variable "Chainsaw") from a 0 to a 1

At first I tried using WriteString, however this insited on writing boxes after the line in question, I'm guessing this is some sort of special character that denotes it as a String Variable

So I resigned to use WriteLine and redundantly re-writing the entire line

I have the line stored in "RLine" and the position of the cursor is at the beggining of the line just after that

Print StreamPos(Chainsaw)
SeekStream(Chainsaw,StreamPos(Chainsaw)-(Len(RLine)+2)) 'Move the stream position back the length of the line, plus an extra 2 for the "new line" character
Print StreamPos(Chainsaw)
RLine = Left(RLine,Len(RLine)-1) 'Chop the last character (0) off of RLine
RLine = RLine + "1" 'Replace it with a 1
WriteLine(Chainsaw,RLine) 'Write the line back again
Print StreamPos(Chainsaw)


I'm using the Print statements to keep track of the file positions as the code runs

The problem is, that the above code doesnt do anything, the line appears in the file exactly as it did before running it

The cursor positions print as:
25
11
25

So the WriteLine command is at least moving the cursor forward as it should do

But it isnt writing the modified line, its writing the old one, or isnt actualy writing at all

It simply doesnt make any sense to me


Dreamora(Posted 2006) [#2]
I'm really sorry but your code then must be trashed.
This code works without any problem on Windows:

Strict

Local Chainsaw:TStream = OpenFile("test.txt")
Local RLine:String = "Testline 3"

If Not Chainsaw
	chainsaw = WriteFile("test.txt")
	chainsaw.writeline("Testline 1")
	chainsaw.writeline("Testline 2")
	chainsaw.writeline("Testline 3")
EndIf

Print StreamPos(Chainsaw)
SeekStream(Chainsaw,StreamPos(Chainsaw)-(Len(RLine)+2)) 'Move the stream position back the length of the line, plus an extra 2 for the "new line" character
Print StreamPos(Chainsaw)
RLine = Left(RLine,Len(RLine)-1) 'Chop the last character (0) off of RLine
RLine = RLine + "1" 'Replace it with a 1
Chainsaw.WriteLine(RLine) 'Write the line back again
Print StreamPos(Chainsaw)

Chainsaw.close()



Keep in mind that with OSX or Linux, it would be +1 on the SeekStream as those have only a single char "newline" representation (either cr or lf, don't know anymore), not a double char (cr + lf) as windows


ziggy(Posted 2006) [#3]
@Mordax_Praetorian: Are you sure you're not using 16 bits chars? the function Len returns the number of characters in a string, not the number of bytes (so adding 2 chars for a single 16 bit new line char, can be wrong, but if you're using ansi or latin1 encoding, then the new line char is splited in two characters, $0D and $0A, on windows. In MacOs I think the new line char is $FF0A (16 bits unicode))


GfK(Posted 2006) [#4]
Doesn't look like you're closing the file when you're done with it. That's possibly the problem.


(tu) ENAY(Posted 2006) [#5]
Is your file accidentally read only?


Mordax_Praetorian(Posted 2006) [#6]
The "Chainsaw" variable is global, the file is loaded into Chainsaw by another function, and gotten to the right position by yet another function

RLine is what is returned by that function, and printing RLine reveals that it is what it should be, and thus the stream is set to the start of the line after

I can't close the file yet, its still needed for a multitude of other functions

Even when the Close File command is in place, I still have to fix several other issues before the program can ever get that far, but its best to handle one issue at a time, and this is the one the code hits first

If Closing the file will fix it, then I will ignore the issue for now, and fix/code up until the close file command