WriteLine fails after ReadLine

Archives Forums/BlitzMax Bug Reports/WriteLine fails after ReadLine

col(Posted 2012) [#1]
Title says it all.

EDIT:- More Info is here

This code:-


produces this result:-



Last edited 2012


skidracer(Posted 2012) [#2]
To open a file for writing use WriteFile.

To open a file for reading use ReadFile.

If you want to open a file and partially overwrite the contents in a random access manner then use OpenFile.

Last edited 2012


col(Posted 2012) [#3]
Hi skidracer,

Just found this -

from fopen

When a file is opened with update mode ( '+' as the second or third character in the mode argument), both input and output may be performed on the associated stream. However, the application shall ensure that output is not directly followed by input without an intervening call to fflush() or to a file positioning function ( fseek(), fsetpos(), or rewind()), and input is not directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file.

BMax opens the file using the same update mode, so this explains it all.

As BlitzMax has no SeekFile I don't think there is a situation where OpenFile should ever be used

However the OpenFile() method has separate flags for opening a file for reading and/or writing. If you're going to include such a command then it should work as expected. I can imagine an additional flag in the underlying TStream methods to know the previous operation wouldn't be too hard to incorporate to get this working.

Thanks.

Last edited 2012


TomToad(Posted 2012) [#4]
Blitzmax does have a SeekFile(). It is called SeekStream(). Modified code, works as expected.

Output
Testing ReadThenWrite
	OpenFile: OK
	ReadLine: FirstLine
	WriteLine: Writing "Testing" as a line: OK

	FileContents:
		FirstLine
		Testing
		e
		ThirdLine

Testing WriteOnly
	OpenFile: OK
	WriteLine: OK

	FileContents:
		Testing
		
		Testing
		e
		ThirdLine


Last edited 2012