Text file: New line

BlitzPlus Forums/BlitzPlus Programming/Text file: New line

Tomas Khan(Posted 2012) [#1]
I am trying to write a text file in BlitzPlus that can be read in by a C++ program, and I've mostly succeeded thanks to WriteByte. I've managed to write a series of floating-point values separated by spaces, and that seems to be working fine. The one thing that isn't working fine yet is putting new lines in the file. I have
100 5.780 3.894 ...


when I want
100
5.780 3.894
...


I know that different text editors use different combinations of CR (carriage return) and LF (line feed), but I thought I had tried using every combination of them, and none of them actually produced a new line in the text file. I believe using LF at the very end of the file did make a new line appear at the end of the file, but if I tried to write anything else to the file after the LF, there was no new line there. I even tried using ReadByte to find out how a file created in Notepad ended its lines, but that showed me something I had already tried.

What do I need to write to a text file to insert a new line?

Thank you for your help.

Last edited 2012


Floyd(Posted 2012) [#2]
The standard end-of-line for Windows is two characters, CR followed by LF.
The Unix ( and Mac ) standard is just LF.

If I remember correctly on Windows Notepad will only read CR-LF files correctly while Wordpad can handle either format.

If the problem arises when reading with C++ check that you are opening the file for text input, not binary.

I had a particularly baffling experience once going the other way, writing raw data in C++. I had accidentally opened the file in the wrong format. As a result the program was silently replacing the byte value 10 with two bytes 13 and 10, i.e. changing what it thought was a Unix LF to the Windows CR-LF.


Tomas Khan(Posted 2012) [#3]
It works perfectly now. Thank you! I thought I had already tried CR+LF, but I guess not.


Midimaster(Posted 2012) [#4]
text based files and binary based files are two worlds, which you cannot be combined. For example save this values in a binary based file:

65,66,67,68,69,70

then you would get this bytes:

656667686970

but you will see "ABCDE" when open the file with a text editor. Each Byte value becomes a character. 65="A" Commands like CRLF only work, because there are no character-numers below 32. and now those numbers 1- 32 can be used for commands.

If you save "65" in a text-based file it would become 2 bytes

5352

which means "6" and "5". and now you could add a CRLF:

53521013

So, the basic condition for a text based file is there is NO content byte below 32. And this is, what you cannot guarantee, when combining binary values and commands. So, not writing but reading such files would be your main problem.

What could you do?

You could save all your values in a text based file. Use WriteLine() also for Values. You could combine a lot of numbers in one line by using separator characters like EXEL is doing it:

"65;66;67;68;69;70;"

There for you need code to combine them before writing:


Stream=WriteFile("test.txt")
	For i%= 65 To 70
		TextLine$ = TextLine + i +";"
	Next
	WriteLine Stream,TextLine
	WriteLine Stream,"Hello World"
	WriteLine Stream,TextLine
CloseFile Stream


now open it with an editor and you will see the values and lines.

Then you need code to separate them when reading it back:
Graphics 800,600

Stream=ReadFile("test.txt")
	TextLine=ReadLine(Stream)
	Print "textline=" + Textline
	From%=1
	For i%=1 To 12
		Upto%=Instr(TextLine,";",From)
		If Upto=0 Then Exit
		Part%=Mid(TextLine,From,Upto-From)
		Print Part
		From=Upto+1
	Next
CloseFile Stream