General discussion

Locked

Visual Basic

By pr1mcess ·
How do you read data from a .txt file into a visual basic project?

The data is set out as follows:
A - indicates magic number
X - 0 100 X & Y indicate the values for the graph axis
Y - 0 80
M - indicates how many series
N - indicates that the series has a number of data values equal to the number on this line
V - indicates that the remainder of the line consists of integer data values to be read in.

If you have an example code or any advice. Thanks

This conversation is currently closed to new comments.

4 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by john.a.wills In reply to Visual Basic

In Access, which is pretty similar to VB, one uses the Get External Data option under ALT/File. One indicates the file to be read and various windows come up to allow the formatting of the data.

Collapse -

by interstellar In reply to Visual Basic

Dim intFile As Integer 'Used to attribute file number
Dim strLine As String 'Container for each line read from file
Dim strDelimiter As String 'Delimiter used to parse contents of file
Dim aryData() As String 'Array containing parsed data

strDelimiter = "," 'Use comma to split series - change this for space, comma, stop, pipe or whatever you need
intFile = FreeFile 'Number of next free file

Open "c:\temp\table.txt" For Input As #intFile 'Open file
While Not EOF(intFile)
Line Input #intFile, strLine 'Read line by line
aryData = Split(strLine, strDelimiter, -1, vbBinaryCompare) 'Parse line by delimiter
For intCount = 0 To UBound(aryData) 'Obtain parsed data
'Access each datum in turn
If aryData(intCount) = "x" Then DoSomethingWith (aryData(intCount + 1)) 'Need to define DoSomethingWith sub-routine or function
If aryData(intCount) = "y" Then DoSomethingWith (aryData(intCount + 1)) 'Etc
Next
Wend
Close intFile 'Close open files

Collapse -

by wmorgan In reply to Visual Basic

This should allow you to do what you want to do. Don't forget to set a reference to MicrosoftScriptingRuntime!

Dim fso As New FileSystemObject
Dim ts As Textstream
Dim NrLines as Long
Dim cFileIn as String
Dim cCurrentLineData as string

Set ts = fso.OpenTextFile(cFileIn)

' This will count the number of lines in the file
Do While Not TS.AtEndOfStream
TS.ReadLine
NrLines = NrLines + 1
Loop
TS.Close

'Now that you know how many lines are in the file,
'you can use ts.readline or ts.skipline as required to get to the line in the file you want.

Set ts = fso.OpenTextFile(cFileIn)

do while not ts.AtEndOfStream
cCurrentLineData = ts.readline
'insert your logic here
Loop
ts.close

set ts = nothing
set fso = nothing

Collapse -

by RRV In reply to Visual Basic

I assume your text file is organized as field names like A, X, Y, M, N and V are in first line separated by tabs and each line thereafter with your mentioned data with each field separated with tabs. This is a flat text file with tab as field delimiter and newline as record delimiter.
How to program???
If it is a .NET then create a dataset. Connect to this text file using odbcclient. The connection string requires the path of the text file. You can see msdn to know how to connect to Access, Excel, Text or other databases. You run a normal standard SQL query to this text file so you'll get the results in a table inside the dataset. You can now set this data to a chart control in your form. It is that easy.

Reading the text file using FSO, OpenFile method, ASCII method all are round about methods or too straight forward methods to solve your problem. I assumed you have data for a chart in a text file and your program has to display the data as chart.
Good luck
RV

Back to Web Development Forum
4 total posts (Page 1 of 1)  

Related Discussions

Related Forums