Edit a specific row in a text file

Hello Group. I am trying to manage a text file. I was able to read and write in append. The text file that interests me is organized in lines. If I wanted to change the second line, I have to loop for example: While Not strIN.EndOfFile with a counter to locate the line and change it? or is there some other method?

What is this text file for? Is it a configuration file? If so, I would have thought an SQLite database would be better suited.

Yes it is a file for some configurations … the database I already use. More than anything else it is to practice the language, I come from VB6 and I am not a programmer by profession, but I write code for fun. The problem is that I can’t understand a thing … when I read do I have to use TextInputStream, when I write TextOutputStream, and when should I edit?

Hello @Federico_Giannetti

Not sure if you have already seen this learning recourse but if not maybe it can help clear some learning curves for you

Xojo: Learn Xojo Programming

Much simpler:

var lines() as string
lines = myText.split(endofline) // If this is text outside of platform, see replacelineendings https://documentation.xojo.com/api/data_types/string.html#string-replacelineendings
var SecondLine as string = lines(1)
1 Like

Ok, I was thinking of such a solution, with an index to point me to the line, but I can’t get the example you wrote to work.

strIN = TextInputStream.Open(f)

Dim i as integer=0
Dim yourText as string

While Not strIN.EndOfFile
  i=i+1
  yourText = strIn.ReadLine
  messagebox yourText
  if i=2 then
    messagebox "I WANT CHANGE " +yourText + " with " + textfield2.Text
    
    var lines as string
    lines = yourText.split(endofline) 
    messagebox ">>" + lines
    'var 2ndLine as string = lines(1)
    
  end if
Wend
strIN.Close


in lines retunr error:  
Type mismatch error.  Expected String, but got String()

Sorry. Oversight.

var lines() as string

What does split do with this example, is it used to replace characters with others, in this case the whole line?

But then, how do I save the line in the file?

My file looks like this:

1111
2222
3333
4444
555
666
For example, I want to replace the second line, 2222 with AAAA

I understand why your code doesn’t work … because I don’t read the file completely, but one line at a time. And anyway, the fact remains, I don’t understand how to save the line of the file I want to change.

If you read the file completely then you can use that code, then:

lines(1) = "AAAA"

then you can use something like string.FromArray to have yourText again and save that.

It all depends on what you are doing.

So, to modify a specific line, in my case for example the second … do I have to put the whole text file in an array, modify the second line, and save everything by overwriting the old file?

Here is my code, what can I improve? There is a faster way to rebuild the file instead of reading all the lines (** AA **)

Dim strIN As TextInputStream = TextInputStream.Open(f)

''READ ALL FILE TXT
Dim AllTextFromFileRead as string
AllTextFromFileRead=strin.ReadAll.ToText

''' I separate the lines, knowing that each line wraps with enter
Dim words() As string
words = AllTextFromFileRead.Split(chr(13))

''I check the inserted lines
For i As Integer = words.FirstIndex To words.LastIndex
  messagebox "??" + words(i)
Next

'I replace the value of the second line with a value of my choice  -->2222 in AAAA
words(1)="AAAAA"


'(** AAA **) I rebuild the file in MyText and then I can rewrite the modified file
Var MyText As String 
For i As Integer = words.FirstIndex To words.LastIndex
  messagebox "??" + words(i)
  MyText=MyText+words(i)+chr(13)
Next

messagebox "All array for a text file " + MyText

You can remove .ToText

Use EndofLine instead of chr(13)

Your final loop can be replaced with:

mytext = String.FromArray (words, EndOfLine)

.

Unless you know the file was specifically saved using that end of line version.

Hi Tim, But my file has some lines (max 20) that I have to read … with chr (13) read the lines and recomposes them well, using String.FromArray (words, EndOfLine) it wraps me leaving an empty line and this does not All right.

There are no Rows in a Text file… only text data…

In 40 years, I never had to do what you asked.

The best answer was done yesterday: read the file line after line and wrote the “new” data in a new file. When done, delete the original.

Yes, that’s what I was saying.
I think the lines are there since the writeline command exists … and the lines are read one at a time.
For the rest it works, I was just wondering about space management.

Thank you.