How to add text to a text file

29 views
Skip to first unread message

Mansour Ayouni

unread,
Feb 22, 2021, 2:52:22 PM2/22/21
to The Ring Programming Language
Hello All,

To read text from a temp.txt file we can say:

cContent = read("temp.txt")

And we have all the text contained in the file in the cContent variable.

To add text to the file, we use:

write("temp.txt", "my new text goes here").

The problem with this last syntax, is that the old content of the file is removed. Only "my new text goes here" is there...

My question is: how can I add new text to an existing text file without deleting its current content?

Best,
Mansour

Ilir

unread,
Feb 22, 2021, 5:24:54 PM2/22/21
to The Ring Programming Language
Hello Mansour,

hFile = fopen("temp.txt","a+")
cContent = fread(hFile,unsigned(1,15,"<<"))
fwrite(hFile,"my new text goes here");
fclose(hFile)

We use append plus (a+) to open a stream at the beginning of the file because you are reading from it first, otherwise we use only append (a) to concatenate, e.g. if we read nothing.

Mansour Ayouni

unread,
Feb 22, 2021, 5:29:34 PM2/22/21
to Ilir, The Ring Programming Language
Thanks a lot Ilir!
All the best,
Mansour

--

---
You received this message because you are subscribed to the Google Groups "The Ring Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ring-lang+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ring-lang/ce08bea3-5063-4f7c-ad9f-b607cf4df582n%40googlegroups.com.

Ilir

unread,
Feb 22, 2021, 5:34:56 PM2/22/21
to The Ring Programming Language
You're welcome.
Reply all
Reply to author
Forward
0 new messages