Read/Save file.txt - string

Can anybody help with following:

I have file named MYFILE.TXT already created in the folder where sits my application (they both are i in the same folder, the same level, no subfolders).
I need to insert a code into my program, which existing content of MYFILE.TXT will replace by a content of string named MYSTRING. Basicaly if MYSTRING=“123456” then MYFILE.TXT will be rewritten and will contain “123456”. NO DIALOG WINDOW needed. The length of MYSTRING will be less than 2000 characters.

Also I need the same but other way round: The content from existing MYFILE.TXT to put into MYSTRING. NO DIALOG WINDOW.

Please give me an exact code for OS X with all definitions/declarations which I can simply copy and paste into my code.

Thanks.
Vlad

So basically you are asking someone, quite arrogantly, to make your code for you?

Please detail exactly what procedures you have tried, why you think what you tried should work, what error message what you tried produced and provide the code that you came up with.

Once you have made an appropriate attempt at doing the research youself, and proven that you are willing to do what is required to be a developer, then we will help you with specific issues that arise from your misunderstanding of concepts or procedures as shown in the code you provided.

But the purpose of this forum is to share and educate each other, not to provide design and coding services on demand. And perhaps it is a language barrier, but to be “Please give me” doesn’t show any initiative.

Show us that you tried, and we will be more than glad to help

Look some code read the txt file

Dim f As FolderItem
Dim dd As String
Dim t As TextInputStream
Dim MYSTRING as String

  f=GetFolderItem("").Parent.Child("Sqlite.txt")


  
  If f <> Nil then
    t=TextInputStream.Open(f)
    t.Encoding=Encodings.UTF8
      dd=t.ReadLine
      If dd.len > 10 Then
         MYSTRING=dd
      end If
  End if

I didn’t mean to ask something arrogantly. I just wanted to write my question in the way so everybody understand what I need to help with (at least I tried). I’m new in xojo and in programming like that. If you call it “make a code for me…”? OK. But I really needed to help as I asked for.
Vlad

So show us what you tried.

Then please take a look at https://forum.xojo.com/927-please-use-head-shots-for-your-avatars. Please let me take your message as random about new xojo people in the forum.

Hm… Not the best way to introduce yourself to Xojo community. But, hey, you are not the first one… just try to ask things in a different way and you will find a lot of people willing to help you.

Thanks Alexis, Dave for replies.
Im trying to adapt what Alexis sent to me:

Dim f As FolderItem
Dim dd As String
Dim t As TextInputStream

f=GetFolderItem("").Parent.Child(“text.txt”)

If f <> Nil then
t=TextInputStream.Open(f)
t.Encoding=Encodings.UTF8
dd=t.ReadLine
msgbox dd
End if

But doesn’t work:-(

You encoding code could be problem ( don’t have a computer nearly ). dd=t.readline

Try to define the encoding on ReadLine or convert later and try.

I mean…

dd=t.ReadLine(Encodings.UTF8)

Thanks Amando. I used your encodings and still doesn’t work.

Vlad,
I highly recommend going through the Introduction .pdf at: https://www.xojo.com/learn/
Maybe it will take you 2 or 3 days but it’s fun and a very good investment if you’re serious with Xojo.

does f exist ?

try

If f <> Nil and f.exists then t=TextInputStream.Open(f) dd= DefineEncoding(t.ReadLine, Encodings.UTF8) msgbox dd else msgbox " f not exists" End if

Thanks Alex.

Is says “f not exists”!
But I’ve got file text.txt in the same folder like built application…

Here is the latest code:

Dim f As FolderItem
Dim dd As String
Dim t As TextInputStream

f=GetFolderItem("").Parent.Child(“text.txt”)

If f <> Nil and f.exists then
t=TextInputStream.Open(f)
dd= DefineEncoding(t.ReadLine, Encodings.UTF8)
msgbox dd
else
msgbox " f not exists"
End if

Maybe something wrong with this line: f=GetFolderItem("").Parent.Child(“text.txt”) ?
Vlad

Honestly, this is best advice I can give: https://youtu.be/RgK23y6Lq4Y. Xojo Debugger is your best mate.

Don’t try to debug with MsBox. Breaks are fine.

try
Step 1
f=GetFolderItem(“text.txt”)
Step 2
use Build Step on the insert menu on the XOJO IDE and add you text file

move text.txt to Documents

f=SpecialFolder.Documents.Child(“text.txt”)

f=GetFolderItem(“text.txt”) WORKS!
Thanks Alexis! Thanks Axel, thanks to all who tried to help me!

This is what I needed.

Latest code is:

Dim f As FolderItem
Dim dd As String
Dim t As TextInputStream

f=GetFolderItem(“text.txt”)

If f <> Nil then
t=TextInputStream.Open(f)
dd= DefineEncoding(t.ReadLine, Encodings.UTF8)
msgbox dd
End if

If I can ask for little more, what commands shall I use if I want to do save string dd back to text.txt?

Thanks a lot!
Vlad

Welcome To Xojo community. [quote=235103:@V L]Thanks a lot!
[/quote]

Welcome to Xojo.

There are examples on
TextOutputStream