Link to home
Start Free TrialLog in
Avatar of CobraBytez
CobraBytez

asked on

Input, Output to specific lines

I have a basic understanding of the input, append and output commands.
I want the program to make a text file where each line of the text controls something (Example Below)

name1
name2
name3
name4
name5

So lets say i bress a button in VB and i want to make it change "name4" to "hello", but leave all the other lines the same how can i do this?
I also need to know how to make a program do this the opposite way for example:

name1
name2
name3
hello
name5

The program reads line 4 only and puts the word hello into a variable.
I am happy to accept any site on the web that can answer my questions as well...links are fine.

Sorry about the points guys I have a lot of questions and thats all I have left :(

Thank you!

Matthew
ASKER CERTIFIED SOLUTION
Avatar of rspahitz
rspahitz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry...the above assumes the user will change the data.

For the program, try something like this in addition to the above:

Private Sub cmdChangeText_Click()
  dim strTextArray() as String

  strTextArray = split(Text1.Text, vbCrLf)
  strTextArray(3) = "hello" ' index 3 is line 4
  Text1.Text = Join(strTextArray, vbCrLf)
End Sub

Sometimes there are some assumptions that can allow for other elegant solutions to different problems.  For instance, if you fix the line size within the file such that each line contains a fixed number of characters (followed by a vbCrLf) then there might be another way to do this rather then trying to first make room for your variable length string and then place it in the file.  Or, if this is a file that is being read by another program (or code within your own program) then maybe you could get away with using records (structures, types) to store and retrieve the data from the file.  Then you can use random access methods to change the file.

However, if this is a text file that both your program and some end user interacts with (for instance, if the end user loads this file into WordPad or something after your program has created it), then the only solution is to either load the file into memory like rspahitz is suggesting (though I would be curious what the limitations are on a textbox size--probably 64K bytes but I can't remember off hand).  Or you would have to make room in the file by moving each line down working your way back from the end of the file to the point where you want to insert your variable length text string.  Such code isn't too diffiicult to write.  I can probably throw it together if you think you need it.  But first, are there any other assumptions that we can make about the nature of this file you are creating (like I said above)?
C-grades usually indicate that the answer was not complete.  Please explain what was missing from the answer.  Usually user-feedback before accepting a comment will give you better results.

Yea, you know I think I'd rather not recieve points at all if I'm gonna get a low grade.  People should have the oppertunity to deny points that someone gives if they don't want a bad grade.  That way the grade stays high and you don't get a C grade for 20 points.  Please forgive me, but I'm happy he gave you the points instead of me.  They really need to fix that somehow.
Avatar of CobraBytez
CobraBytez

ASKER

rspahitz i am very sorry

Your answer was fine I gave you an A but when i clicked OK a new page was not brought up and internet explorer froze...I have no idea what happened I jsut noticed these emails today and was wondering what was...I have looked for a way to change the grade but cannot seem to find one...

Again I am Very Sorry

Matthew
If you would like to change the grade, you'll have to drop a message in the customer support area, and reference this Q #.

Thanks for the response :)