coson
asked on
Hopefully Easy VB6 read & write data to a file
Good Day All,
I can't believe this is giving me problems. Every day, a file is deposited on our web server.
Unfortunately, the file that is deposited is exported from an application before it is dumped on
our web server. As a result, some nasty characters work it's way into the file.
My dilemma, I'm writing a small VB app that takes this file reads it one character at a time,
filters the character (example: if asc(character) = 9 then disregard ' No tabs wanted...) to
see if it's between A-Z, and if so, write the character out to a new file. Fortunately for me,
the file will never be over 2K.
But I can't seem to find code out there that does what I want.
PSUEDOCODE---------------- ---------- --
open input file
open output file
while not eof(input file)
read a character from the input file
if the character <> ASC(9) then ' do not want TABS in output file
write character to the output file
end if
wend
close input file
close output file
PSUEDOCODE---------------- ---------- --
Any help will be appreciated. If needed, I'll add points if the solution warrants it.
TIA,
coson
I can't believe this is giving me problems. Every day, a file is deposited on our web server.
Unfortunately, the file that is deposited is exported from an application before it is dumped on
our web server. As a result, some nasty characters work it's way into the file.
My dilemma, I'm writing a small VB app that takes this file reads it one character at a time,
filters the character (example: if asc(character) = 9 then disregard ' No tabs wanted...) to
see if it's between A-Z, and if so, write the character out to a new file. Fortunately for me,
the file will never be over 2K.
But I can't seem to find code out there that does what I want.
PSUEDOCODE----------------
open input file
open output file
while not eof(input file)
read a character from the input file
if the character <> ASC(9) then ' do not want TABS in output file
write character to the output file
end if
wend
close input file
close output file
PSUEDOCODE----------------
Any help will be appreciated. If needed, I'll add points if the solution warrants it.
TIA,
coson
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
'This code will clean all character below ascii 32 and above ascii 122
Private Sub Command1_Click()
Dim myLine As String, posChar As Integer, newLine As String
Open "sourceFilePath&name" For Input As #1
Open "targetfilePath&name" For Output As #2
Do Until EOF(1)
Line Input #1, myLine
For posChar = 0 To 31
newLine = Replace(myLine, Chr(posChar), "")
Next posChar
For posChar = 123 To 255
newLine = Replace(newLine, Chr(posChar), "")
Next posChar
Print #2, newLine
Loop
Close #2
Close #1
End Sub
S
Private Sub Command1_Click()
Dim myLine As String, posChar As Integer, newLine As String
Open "sourceFilePath&name" For Input As #1
Open "targetfilePath&name" For Output As #2
Do Until EOF(1)
Line Input #1, myLine
For posChar = 0 To 31
newLine = Replace(myLine, Chr(posChar), "")
Next posChar
For posChar = 123 To 255
newLine = Replace(newLine, Chr(posChar), "")
Next posChar
Print #2, newLine
Loop
Close #2
Close #1
End Sub
S
ASKER
Idle_Mind,
While all the solutions worked, I'm going to give you the points. But I'm intrigued by your use of:
entireFile = Space(LOF(1))
Why the use of the Space function?
coson
While all the solutions worked, I'm going to give you the points. But I'm intrigued by your use of:
entireFile = Space(LOF(1))
Why the use of the Space function?
coson
When you open a file in Binary mode and use a statement like
Get #1, , entireFile
the number of bytes read is determined by how big entireFile is. By making it as large as the file with
entireFile = Space(LOF(1))
we read the whole contents of the file into entireFile.
The Space() function creates a string of x spaces so
Dim s As String
s = Space(5)
would result in a string of five spaces like " ".
Regards,
Idle_Mind
Get #1, , entireFile
the number of bytes read is determined by how big entireFile is. By making it as large as the file with
entireFile = Space(LOF(1))
we read the whole contents of the file into entireFile.
The Space() function creates a string of x spaces so
Dim s As String
s = Space(5)
would result in a string of five spaces like " ".
Regards,
Idle_Mind
For your pseudocode example, here's the .NET version.
Dim strInputFileName As String = "C:\input.txt"
Dim strOutputFileName As String = "C:\output.txt"
Dim oneChar As String
FileOpen(1, strInputFileName, OpenMode.Binary, OpenAccess.Read)
FileOpen(2, strOutputFileName, OpenMode.Output)
While Not EOF(1)
oneChar = InputString(1, 1)
If Asc(oneChar) <> 9 Then
Print(2, oneChar)
End If
End While
FileClose(1)
FileClose(2)
And here's the VB6 version (I don't have a copy of VB6 on this computer, so I can't test this version.)
Dim strInputFileName As String
Dim strOutputFileName As String
Dim oneLine As String
Dim newLine As String
strInputFileName = "C:\input.txt"
strOutputFileName = "C:\output.txt"
Open strInputFileName For Input As #1
Open strOutputFileName For Output As #2
Do While Not EOF(1)
newLine = ""
Line Input #1, oneLine
for i = 1 to len(oneLine)
If Asc(Mid(oneLine, i, 1)) <> 9 Then
newLine = newLine & Mid(oneLine, i, 1)
End If
next i
Print #2, newLine
Loop
Close #1
Close #2
Hope that helps.