|
-
Dec 1st, 2006, 09:49 PM
#1
Thread Starter
Lively Member
[RESOLVED] Manipulate Text File
I saw this example in another thread and decided it would make a good example for me to learn VBs file manipulation syntex. That thread is closed so I was asked to start a new thread.
Lets say this data was in a file and it had numerous records. I think I'm close down there, but am doing something wrong. Thanks!
ps. I guess what I'm trying to come up with is a standard way to open and write a file, and use the middle area to manipulate the text.
MyString.txt.
1This is the string I want to keep.****12/1/2006 USG8503
2This is the string I want to keep.****12/1/2006 USG8503
3This is the string I want to keep.****12/1/2006 USG8503
4This is the string I want to keep.****12/1/2006 USG8503
[Highlight=VB]
Private Sub cmdClick()
Dim sFileInfo() As String
Dim i As Long
Open "C:\MyString.txt" For Input As #1
sFileInfo(i) = Left$(i, Len(i) - 21)
Close #1
Open "C:\AllFixedUp.txt" For Append As #1
Print #1, sFileInfo(i)
Close #1
Next
End Sub
-
Dec 1st, 2006, 10:02 PM
#2
Re: Manipulate Text File
Code that you posted (btw, you missed the ending [/Highlight] tag) makes almost no sense - it's either you posted incomplete copy or you got incomplete copy from elsewhere.
Anyway, if you look in our VB FAQ forum you will find article demonstrating how to work with the files.
-
Dec 2nd, 2006, 06:10 AM
#3
Re: Manipulate Text File
Open "C:\MyString.txt" For Input As #1
sFileInfo(i) = Left$(i, Len(i) - 21)
Close #1
i is meaningless here as it has no value
you need to get the text from the file using
VB Code:
mystring = Input(LOF(1),#1)
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 2nd, 2006, 07:34 AM
#4
Thread Starter
Lively Member
Re: Manipulate Text File
I read the FAQ and just don't get the syntex. This is where I'm at but it still doesn't work.
VB Code:
Private Sub cmd1Click()
Dim mystring() As String
'open file
Open "C:\MyString.txt" For Input As #1
mystring = Input(LOF(1), #1)
Close #1
'manipulate data
mystring() = Left$(mystring, Len(mystring) - 21)
'create new file
Open "C:\AllFixedUp.txt" For Append As #1
Print #1, mystring()
Close #1
Next
End Sub
-
Dec 2nd, 2006, 07:55 AM
#5
Re: Manipulate Text File
Don't declare the mystring variable as an array. Remove the (). Also remove "Next". There is no loop in that code. Next is for loops.
-
Dec 2nd, 2006, 08:40 AM
#6
Thread Starter
Lively Member
Re: Manipulate Text File
I removed the () and Next. Still does not create the new file.
VB Code:
Private Sub cmd1Click()
Dim mystring As String
'open file
Open "C:\MyString.txt" For Input As #1
mystring = Input(LOF(1), #1)
Close #1
'manipulate data
mystring = Left$(mystring, Len(mystring) - 21)
'create new file
Open "C:\AllFixedUp.txt" For Append As #1
Print #1, mystring
Close #1
End Sub
-
Dec 2nd, 2006, 09:00 AM
#7
Re: Manipulate Text File
You may like to check these things:
Is the text in your file less than 21 characters?
Does the end of your file contain 21 or more white space characters (space, enter, tab etc.)?
Pradeep
Last edited by Pradeep1210; Dec 2nd, 2006 at 09:07 AM.
-
Dec 2nd, 2006, 09:12 AM
#8
Re: Manipulate Text File
Since you are learning now is a great time to build a good habit.
VB6 has the ability to step through code a line at a time.
Click on the F8 button to start RUNNING your program.
You will notice that the first executable line of code is highlighted in yellow. As you click F8 repeatedly you will "step" through the code one line at a time.
Even better - if you hover over variables as you step you get to see the values in them. This is a great way to really start understanding what's going on in the code.
Plus it's a good habit - DEBUGGING is probably 80% of programming!
PraDeep wouldn't have to ask you questions about what's in your file and whether it's 21 characters - you would already know.
Learn about debugging - learn about setting breakpoints. Learn about using the immediate window.
-
Dec 2nd, 2006, 09:41 AM
#9
Thread Starter
Lively Member
Re: Manipulate Text File
Thanks for the F8 tip. I didn't know it could be started that way. I have a VB CBT that has a lot on debugging, I'll review it again. Here was one of the problems:
Private Sub cmd1Click() should be Private Sub cmd1_Click()
Now what it is doing is only removing the last line's "****12/1/2006 USG8503".
1This is the string I want to keep.****12/1/2006 USG8503
2This is the string I want to keep.****12/1/2006 USG8503
3This is the string I want to keep.****12/1/2006 USG8503
4This is the string I want to keep.
It is grabbing the entire file instead of line-by-line.
-
Dec 2nd, 2006, 04:38 PM
#10
Re: Manipulate Text File
VB Code:
Private Sub cmd1Click()
Dim mystring As String,[B] mylines() \As String, i As Long[/B]
'open file
Open "C:\MyString.txt" For Input As #1
mystring = Input(LOF(1), #1)
Close #1
'manipulate data
mylines = Split(mystring, vbNewLine) 'split file into lines
for i = 0 to Ubound(mylines) 'process each line
mylines(i) = Left$(mylines(i), Len(mylines(i)) - 21)
Next
'create new file
Open "C:\AllFixedUp.txt" For Append As #1
Print #1, Join(mylines, vbNewLine) 'join all lines into file
Close #1
End Sub
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 2nd, 2006, 05:13 PM
#11
Thread Starter
Lively Member
Re: Manipulate Text File
Thanks Westconn1, that is a perfect template to use when a file needs to be opened, the text needs to be manipulated, then saved as another file name (or appended).
I'm trying to learn this and do not understand what this does. You document it by calling it "process each line."
For i = 0 To UBound(mylines)
Thanks again. This will go a long way in helping me understand text manipulation.
-
Dec 2nd, 2006, 05:24 PM
#12
Re: Manipulate Text File
note, that is only one way to do it
another way many people prefer to use line input to read the file one line at a time, process each line and write the processed line to the new file, for very large files that maybe better, as the whole file does not have to be in memory
there are more choices, such as loading into a byte array, or Random access which is used for fixed length fields type text datafiles
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 2nd, 2006, 08:40 PM
#13
Re: [RESOLVED] Manipulate Text File
VB Code:
Dim ff As Integer
Dim mystring As String
ff = FreeFile
Open "C:\MyString.txt" For Input As #ff
mystring = Input(LOF(ff), #ff)
mystring = Replace(mystring, Right(mystring, 21), "")
Close #ff
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|