I want to read a text file into an array line by line. When the file contains a TAB character inside a line, this TAB character is completely ignored. => how to avoid TAB skipping easily?
Code basically looks like
Open strFilePath For Input As #1
arrIndex = 0
Do While Not EOF(1)
Line Input #1, my_row
ReDim Preserve strArrLines(0 To arrIndex)
strArrLines(arrIndex) = my_row
arrIndex = arrIndex + 1
Loop
Reading the file line by line is a must, since the lines get filtered before they're put into the array (not shown in the code here, to show it as easy as possible).
Related
I would like to write some data to a csv file. I am using this code:
Dim Filename As String, line As String
Dim A As Integer
Filename = "D:" & "\testfile.csv"
Open Filename For Output As #1
For A = 1 To 100
Print #1, "test, test, test"
Next A
Close #1
but the problem is, that this code rewrite this cvs file from the beginning. but I would like to add data at the end of csv file ( For example if I run this code three times, I would like to have 300 lines in this csv file)
what should I do?
In which case you need Open Filename For Append As #1.
You might also find that Write #1, behaves better than Print #1, if you line contains quotation characters.
One last thing, don't hardcode the #1 as someone else may be using that handle. Instead, use
Dim n as Integer
n = Freefile 'Let VBA find a free file handle
'use #n rather than #1 from here.
Here is your Error:
Open Filename For Output As #1
which should be:
Open Filename For Append As #1
This will append your new text to the end of a stream.
How can I modify a text file using VBA, without losing the the format of my text lines - I'm talking about lines which have at least 1 number on it
For example: If I have 5 numbers on a line, in the new modified file, all of those number are on 5 rows.
The lines which contains characters other than numbers are fine!
Please, if you have 3-4 minutes, open any single text file using VBA (containing a line with at least 2 numbers on it) and try to save it to a new file, using the method posted below.
Open Path For Input As #1
Do Until EOF(1)
Input #1, ReadData
File.WriteLine (ReadData)
The problem :The format of the lines with numbers is not preserved.
Original:
1 2 3 4 5 6
Modified:
1
2
3
4
5
6
Any suggestions?
Writeline writes a line (with a line feed).
You will have to assemle a full line and then write it into a file:
dim s as string
s=data1 & " " & data2 & " " '...
file.writeline(s)
Input is designed to assign values to individual variables. Like a record of comma-separated values.
Try Line Input,
Open Path For Input As #1
Do Until EOF(1)
Line Input #1, ReadData
File.WriteLine (ReadData)
more info here
I have a text file that I open and attempt to read the individual lines. I have used the same code before on other files with no problem, but for some reason, this particular file is strange. When I do the following command;
Line Input #1, read_string
the string read_string contains the entire sequence of each line in the file concatenated together. When I look at the special chararcters of the file I do see a cariage return. But just so you know what the file looks like, here are the first two lines (daniweb formatting is too strange to print text here),
k_arr[8'h1C]= {10'b001111_0100,10'b110000_1011} ;
k_arr[8'h1C]= {10'b001111_0100,10'b110000_1011} ;
Anybody know how I can read each line? apparently line input doesnt work for this file.
Try
Dim lines() As String
lines = Split(read_string, vbCr) 'splitting with Carriage Return delimiter
'did it work?
Debug.Print lines(1)
Debug.Print lines(2) Dim lines() As String
Each element of the lines array should now contain one line of your text file.
If it didn't work, try with another delimiter instead of vbCr, e.g. vbLf (line feed).
I have a form with several text boxes and I want to write the contents of each of them to a new line in a .txt file. As in, the user fills in a form, and the info is stored in the file. Then I want to be able to retrieve the info from the file into the same text boxes. I am able to do this, so far, but I encounter problems when one of the text boxes is multiline.
Printline(1, txtBox1.text)
Printline(1, txtBox2.text)´which is the multiline one
Printline(1, txtBox3.text)
When I read this back from the file I get the second line of the multiline text box where I want the text from txtBox3 to be.
LineInput(1, txtBox1.text)
LineInput(1, txtBox2.text)
LineInput(1, txtBox3.text)
How can I get all the lines from the multiline text box to write to one line in the file, and then read it back as separate lines in a multiline text box?
I hope I am making sense? I really would like to keep the logic of "one txtBox - one line in the file"
I guess I need to use different methods of writing and reading, but I am not that familiar with this, so any help is much appreciated.
You can rely on the Lines Property in case of having more than one line. Sample code (curTextBox is the given TextBox Control):
Using writer As System.IO.StreamWriter = New System.IO.StreamWriter("path", True)
Dim curLine As String = curTextBox.Text
If (curTextBox.Lines.Count > 1) Then
curLine = ""
For Each line As String In curTextBox.Lines
curLine = curLine & " " & line
Next
curLine = curLine.Trim()
End If
writer.WriteLine(curLine)
End Using
NOTE: this code puts in one line all the text from the given TextBox independently upon its number of lines. If it has more than one line, it includes a blank space to separate the individual lines (all of them fitting in a single line of the file anyway). You might want to change this last feature by adding a different separating character (replace & " " & with the one you want).
One option would be to escape the newlines so that they aren't in the output, then unescape them on reading back in.
Here's some example code that will do this (I've never written VB before, so this probably isn't idiomatic):
' To output to a file:
Dim output As String = TextBox2.Text
' Escape all the backslashes and then the vbCrLfs
output = output.Replace("\", "\bk").Replace(vbCrLf, "\crlf")
' Write the data from output to the file
' To read data from the file:
Dim input As String = ' Put the data from the file in input
' Put vbCrLfs back for \crlf, then put \ for \bk
input = input.Replace("\crlf", vbCrLf).Replace("\bk", "\")
' Put the text back in its box
TextBox2.Text = input
Another option would be to store your data in XML, JSON, or YAML. Any of those are text-based formats that will require a library to parse, but should cleanly handle the multiline text you have, along with providing increased future flexibility.
the next simple code works for me.
Saving multiline text to a single line in a file:
str = Replace(MyTextBox.Text, Chr(13) & Chr(10), "*LineFeed*") 'something recognizable
Print #1, str 'no quotes
To get the string from the file and put it on a TextBox:
Line Input #1, str
MyTextBox.Text = Replace(str, "*LineFeed*", Chr(13) & Chr(10))
Hope this helps
I just wrote this function to read a series of email addresses from a linebreak-delimited text file. And it does work, but that's not my question.
Function GetEmailArray(FileName As String) As String()
Dim TempArr() As String
Dim i As Integer
Open FileName For Input Access Read As #1
Do While Not (EOF(1))
i = i + 1
ReDim Preserve TempArr(i + 1)
Line Input #1, TempArr(i + 1)
Debug.Print TempArr(i + 1)
Loop
Close #1
GetEmailArray = TempArr
End Function
Reading this, I would expect this to:
Read the first line, store it in TempArr(1)
Loop
Read the first line AGAIN, store it in TempArr(2)
Loop
Etc
I just can't figure out how the while loop goes to the next line in the text file.
You're holding a handle (#1) to the file from the point you call Open, until you call Close. Behind the scenes (on the O/S level), the handle retains a current file position as part of the file descriptor.
Your step 3 should be:
3. Read the next line from the file into TempArr(i + 1)
So you do not read in the first line again, the line input statement reads the line, and places the file-position on the next line.
I believe the magic is happening on Line Input #1, TempArr(i+1). It reads from file handle #1 to TempArr(i+1). When it gets to the end of the file, EOF(1) will evaluate to true and terminate the while loop. How does the system know which is the next line? That's handled under the hood for you, by Line Input.
The Line Input function is altering the current line. Pretty tacky stuff.