Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add text to specific line in text file???

Status
Not open for further replies.

Mtlca401

Programmer
Mar 18, 2003
42
US
I am working with a text file and adding lines to it using the for append as. I have five different controls that add a file name to the text file by clicking them. If I click on the first control it adds lets say text1 which is fine. Then if I click on the third control it adds text3. Which would look like this:

text1
text3

The problem is that if click on the second control it adds text2 and the out come looks like this:

text1
text3
text2

How can i get the out come to look like this:

text1
text2
text3

The reason for this is the user right clicks on a control(image box) and can add a picture/thumbnail to the control. Then the file name of that picture gets put into the text file so when the user opens the program again it reads the text file and the picture is still there rather than being blank.

I am trying to keep the order of the file names so they match the control the user assigned them to by simply reading each line of the text file.

Does that make sense?
 
Is your user going to be naming these files or are they predetermined?
 
I would use ini files or something similiar such as defining file structure, where you assign values to name, instead of worring about what order they are in.

Tom
 
A simple random access file would work.

For Example:

Place this code in a module
Code:
Public Type ImageFiles
    imgfileStr As String * 10 'increase 10 for longer file names
End Type

And on your form place 3 command buttons and 1 ListBox

then paste form code:
Code:
Option Explicit

Dim record_numberInt As Integer

Private Sub Command1_Click()
    'as in your example we call this file Text1
    Dim myfile As ImageFiles
    myfile.imgfileStr = "Text1"
    record_numberInt = 1 'this insures this will always be record one
    Put #1, record_numberInt, myfile
End Sub

Private Sub Command2_Click()
    Dim myfile As ImageFiles
    myfile.imgfileStr = "Text2"
    record_numberInt = 2
    Put #1, record_numberInt, myfile
End Sub

Private Sub Command3_Click()
    Dim myfile As ImageFiles
    myfile.imgfileStr = "Text3"
    record_numberInt = 3
    Put #1, record_numberInt, myfile
End Sub



Private Sub Form_Load()
    'create random access file
    Dim myfile As ImageFiles
    Dim filenameStr As String
    
    filenameStr = "C:\ImgFiles.txt"
    Open filenameStr For Random As #1 Len = Len(myfile)
    record_numberInt = 0
    'the file is now open and ready for processing
    
    'loop available records
    For record_numberInt = 1 To LOF(1) / Len(myfile)
        Get #1, record_numberInt, myfile
        List1.AddItem myfile.imgfileStr
        'of course instead of adding to list1
        'this is where you do your own process
    Next record_numberInt
    
    
End Sub


Private Sub Form_Unload(Cancel As Integer)
    'close random access file
    Close #1
End Sub
 
Are the five filenames in alphabetical order. If so you can sort them before writing to the file.

Or if not, will all five filenames be added to the file in one session. If so you can store the filenames in an array and then write them to the file when all five elements of the array are complete.

Element(3) = text3
Element(1) = text1
Element(5) = text5
Element(2) = text2
Element(4) = text4

for n = 1 to 5
print #1, Element(n)
next

[gray]Experience is something you don't get until just after you need it.[/gray]
 
I thought about that too Error7, but I think .357's solution is better.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top