Hey.
I'm having some issues reading in data from a tab delimited text file. I only want to input numbers, and ideally I'd like it to be able to cope with a line or 2 of text at the top in case someone leaves the file headings on when exporting the data from the spreadsheet.
My code is - dim data(,) as double
-
dim lines, i as integer
-
dim s as string
-
-
fileopen(1,"c:\mydatafile.txt", openmode.input)
-
-
lines=0
-
while not (EOF(1))
-
input (1,s)
-
lines=lines+1
-
'counts no of lines in data
-
-
fileclose(1)
-
fileopen(1,"c:\mydatafile.txt", openmode.input)
-
-
redim data(lines,1)
-
-
i=0
-
try
-
while not (EOF(1))
-
input (1,data(i,0))
-
input (1,data(i,1))
-
i=i+1
-
end while
-
catch ex as exception
-
input (1,s)
-
lines=lines-1
-
end try
-
-
fileclose(1)
-
This should read any non-numeric data into a string (lines=lines-1 per error to make looping easier later) and keep it out of my array. However, this seems to just make the entire array just a massive collection of 0s! Any ideas why?
5 1645
sorry, was i unclear about something or do people just not know?
sorry, was i unclear about something or do people just not know?
Hi there tim123,
Sorry but i don't understand. could you just simplify it?
Better Than Yesterday ^^
sorry, was i unclear about something or do people just not know?
The basic problem here is impatience. This site brings together people all over the world, in different timezones, who volunteer what time they can spare to help out other programmers. In these circumstances, you can't expect an instant answer.
Ok, I've had a look at the code. I'm a VB6 developer, so the syntax is slightly unfamiliar. But off-hand, I'd guess the ReDim is erasing all your values. In VB6 at least, you have to say Redim Preserve to "preserve" the existing values in the array.
This may work differently in your version, of course.
Ok, my goof. I see the ReDim is only intended to be done once before populating the array, so shouldn't be an issue. But I think the structure, with nested loops, is all wrong. Maybe try something a bit more like this... - Dim data(,) As Double
-
Dim lines, i As Integer
-
Dim s As String
-
-
fileopen(1,"c:\mydatafile.txt", openmode.input)
-
-
' Count no of lines in data
-
While Not (EOF(1))
-
input (1,s)
-
lines = lines + 1
-
End While
-
-
' Reopen file to reposition at start.
-
fileclose(1)
-
fileopen(1,"c:\mydatafile.txt", openmode.input)
-
-
' Set array to appropriate size.
-
ReDim data(lines,1)
-
-
' Read values into array, hopefully skipping non-numeric lines.
-
While Not(EOF(1))
-
Try
-
Input (1,data(i,0))
-
Input (1,data(i,1))
-
i = i + 1
-
Catch ex As Exception
-
Input (1,s)
-
lines = lines - 1
-
End Try
-
End While
-
-
fileclose(1)
-
If you still have problems, remember to use the debugging tools built into VB - they're your best friend in these situations. You can step through the execution one statement at a time, and find out what is really happening.
Oh, and one more thing. If you're going to post code here, please copy and paste it from your program. Typing it in here usually introduces too many errors, making the whole process that much harder for everyone.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: C G |
last post by:
Dear All,
I have a text file with data like:
1 2 3
4 5 6
7 8 9
i.e. so I have three columns of numbers.
I wish to put this data into a table. However, I do not want it to take up
three...
|
by: snow.carriers |
last post by:
http://www.rafb.net/paste/results/fTQgRW16.html
Here's my program so far. This is what I'm trying to do:
http://contest-cemc.uwaterloo.ca/ccc/2005/senior/phone.pdf
So far it works fine. The only...
|
by: Chris |
last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL.
Meeting info and Meeting docs reside on 2 related tables in the db. Users
may want to upload anywhere from 1 to 10 or...
|
by: Ramper |
last post by:
Have a .txt document as:
String
int
int
int
int
int
int
int
|
by: james121285 |
last post by:
I have been trying this program for ages and am not getting very far. I am trying to input data from a seperate file and use it to work out the max and min values of the data. I have done the second...
|
by: namcintosh |
last post by:
First of all, here is my program:
#include <iostream>
#include <conio>
#include <fstream> //Needed to use files
#include <iostream>
#include <fstream>
|
by: UofFprogrammer |
last post by:
Hello,
Several Weeks ago I asked a question about testing for the end of an input file.
I have been using this method pretty well for inputting information from an external file. I am using C++.
...
|
by: shadowofanubis66 |
last post by:
Basically, the project I'm working on this week is to make a school lunch menu with a GUI, when the person types in a day, the menu for that day shows up. We have two files, ones a day off text file...
|
by: kylie991 |
last post by:
Hi everyone. I am just starting out on my assignment on classes. We havent long learnt about them so I am struggling a little on how to do the following.
Below is a small part of the definition of...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |