Help writing multiple lines to a text file.

Hello,

I’ve been looking for about an hour, and can’t find anything on this.
I have this problem, when I hit the “Generate Item” button, it writes it as a giant line, And I have so many lines, it would be crazy amount of work just to get it to write right.
(if I use individual “WriteLine ()” for each line) So is there anyway so when it see’s "
" it will write a new Line?
Any help would be much appreciated.

function Update () {
	info = iname + itemname + n+ iid + itemid + n + iicon + itemicon + n + itype + itemtype + n + istack + itemstack + n + customfunction + n;
	if (itemtypeInt == 6) info += "grenadetime=" + grenadetime + n + "grenadethrowforce=" + grenadethrowforce + n + "damage=" + tooldamage + n + "maxuses=" + tooluses + n;
	if (itemtypeInt == 2||itemtypeInt == 3||itemtypeInt == 4||itemtypeInt == 5) info += "maxuses=" + tooluses + n + "armorname=" + armortexture + n;
	if (itemtypeInt == 1) info += "healamount=" + healamount + n + "maxuses=" + tooluses + n;
	if (doortypeInt == 0 && itemtypeInt == 7) info += "iddoor=" + iddoorblock + n;
	if (doortypeInt == 1 && itemtypeInt == 7) info += "texturefilebottom=" + texturefilebottom + n + "iddropped=" + iddropped + n;
	if (tooltypeInt == 1 && itemtypeInt == 0) info += "harvestblocks=pickaxe" + picktype + n;
	if (infiniteair == true && itemtypeInt == 2||itemtypeInt == 3||itemtypeInt == 4||itemtypeInt == 5) info += "infiniteair=true" + n;
	if (nofalldamage == true && itemtypeInt == 2||itemtypeInt == 3||itemtypeInt == 4||itemtypeInt == 5) info += "nofalldamage=true" + n;
	if (noburning == true && itemtypeInt == 2||itemtypeInt == 3||itemtypeInt == 4||itemtypeInt == 5) info += "noburning=true" + n;
	if (itemtypeInt == 0) info += "damage=" + tooldamage + n + "hitmob=damageitem(" + mobdamagetool + ")" + n + "blockdestroyed=damageitem(" + blockdamagetool + ")" + n + "efficiencyblocks=" + tooltype + n + "maxuses=" + tooluses + n;
}

function OnGUI () {
    if (GUI.Button (Rect (625,205,150,50), "Generate Item")) {
		info2 = "Creating Item";
		Debug.Log (info);
		fdir2 = fdir + ".item";
		var sw : StreamWriter = new StreamWriter(fdir2);
		info2 = "Writing To File";
		sw.WriteLine(info);
		info2 = "Saving";
	    sw.Close();
		info2 = "Finished";
		}

3 Answers

3

newline is made with a control code called "
" thats backslash and ‘n’ for new line.

its a C/C++ standard which is continued into C# and javascript.

So you just add "
" in your strings where you need a linebreak.

edit
On windows computers a txt file uses a “carriage return” (\r) followed by a “line feed” (
) character to signal a new line. Unix based operating systems usually only use a single line feed. That includes MacOS, Unix and Linux

Thanks for the reply, But yeah, I have that in the code, the variable 'n' in the code is just the string 'backslash "\n"'. While it makes a newline in the debug console, it doesn't write a newline in the text file it creates.

Plain text doesn't recognize \n alone. You need to do \r\n.

Okay, sweet that worked, Thanks.

http://www.editpadpro.com/tricklinebreak.html

Thanks for the reply,
I tried that, and it worked for the debug console, but it doesn’t work when it writes the data to the file.
I forgot to mention that the ‘n’ variable in my code, is just the string "
".

"
" instead of just "
".