\n Not working when writing files.

Hi,

I’m trying to create text and write it to a file using the following code.

local function msg( ... ) local str = tostring(arg[1]) for i = 2, #arg do str = str .. "\n" .. tostring(arg[i]) end return str end local saveData = msg("test", "test 2", "test 3") local path = system.pathForFile( "test.txt", system.DocumentsDirectory ) local file, errorString = io.open( path, "a" ) if not file then print( "File error: " .. errorString ) else file:write( saveData ) io.close( file ) end file = nil

The code works great but once I build the app the \n within the text file doesn’t actually break the text it just shows as a \n…?

Is there a way to fix this so when the app exports the .txt file it actually has a break in the text?

Thanks,

-B

Thanks for the code post, but a functional sample project in a zip file would be way cooler. 

I just ran this and it is fine on a Windows machine.

So, back to my original question mac or windows?

Windows actually. It’s for an android app.

I guess I don’t see the problem.  It works great on my system.
 
Wait.  Do you mean it is not working as expected in the simulator on Windows or on the device under Android?
 
When I open that file (attached) in sublime it looks fine to me.

test test 2 test 3

PS - It should not matter, but I’m running 2018.3246 on a Windows 10 machine.

It works fine via the simulator/ on the pc but once it is built for the app it does not work on the android tablet I am testing with.

BTW after I write it I export it via the social plugin to a dropbox account. (This is on the App since the social plugin doesn’t work on the simulator).

Well that should work as ‘\n’ is the escape code for newline.

Try

"abc" .. string.char(10) .. "xyz"

If that doesn’t work try:

"abc" .. "\r" .. "xyz" -- or "abc" .. string.char(13) .. "xyz"

Question, is this content meant to be shown in a web-browser?

To be clear, are you seeing this?:

test\ntest 2\ntest 3

Do you notice this when you go to open the file in dropbox or when you download it then view it in a non-formatting text editor?

You said the perfect thing. I was opening it in notepad and it wasn’t broken. I just tried in wordpad and it is. It must be the non-formatting of notepad. I can’t believe I didn’t try it in another program… duh! Thank you!

I’m trying to understand the end-goal and purpose of this.  Can you tell me why you’re doing this and how you expect the end-user to interact with the app and how they will get the files later?

You can’t use a formatting program like word, wordpad, etc.  You must use a code editor or notepad.

Still want to know reason for doing this and general usage concepts.  May be able to provide suggestions based on that info.

Basically all the information from the app is stored to a file and saved. At the end of the day the user will upload it to dropbox so they can have it for there records. I have formatted it exactly how my client wants it so when they open it its easy to see/edit. 

Thanks for the code post, but a functional sample project in a zip file would be way cooler. 

I just ran this and it is fine on a Windows machine.

So, back to my original question mac or windows?

Windows actually. It’s for an android app.

I guess I don’t see the problem.  It works great on my system.
 
Wait.  Do you mean it is not working as expected in the simulator on Windows or on the device under Android?
 
When I open that file (attached) in sublime it looks fine to me.

test test 2 test 3

PS - It should not matter, but I’m running 2018.3246 on a Windows 10 machine.

It works fine via the simulator/ on the pc but once it is built for the app it does not work on the android tablet I am testing with.

BTW after I write it I export it via the social plugin to a dropbox account. (This is on the App since the social plugin doesn’t work on the simulator).

Well that should work as ‘\n’ is the escape code for newline.

Try

"abc" .. string.char(10) .. "xyz"

If that doesn’t work try:

"abc" .. "\r" .. "xyz" -- or "abc" .. string.char(13) .. "xyz"

Question, is this content meant to be shown in a web-browser?

To be clear, are you seeing this?:

test\ntest 2\ntest 3

Do you notice this when you go to open the file in dropbox or when you download it then view it in a non-formatting text editor?

You said the perfect thing. I was opening it in notepad and it wasn’t broken. I just tried in wordpad and it is. It must be the non-formatting of notepad. I can’t believe I didn’t try it in another program… duh! Thank you!

I’m trying to understand the end-goal and purpose of this.  Can you tell me why you’re doing this and how you expect the end-user to interact with the app and how they will get the files later?

You can’t use a formatting program like word, wordpad, etc.  You must use a code editor or notepad.

Still want to know reason for doing this and general usage concepts.  May be able to provide suggestions based on that info.