By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,824 Members | 2,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,824 IT Pros & Developers. It's quick & easy.

inputting from a data file

P: 2
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

Expand|Select|Wrap|Line Numbers
  1. dim data(,) as double
  2. dim lines, i as integer
  3. dim s as string
  5. fileopen(1,"c:\mydatafile.txt", openmode.input)
  7. lines=0
  8. while not (EOF(1))
  9.   input (1,s)
  10.   lines=lines+1
  11.   'counts no of lines in data
  13.   fileclose(1)
  14.   fileopen(1,"c:\mydatafile.txt", openmode.input)
  16.   redim data(lines,1)
  18.   i=0
  19.   try
  20.     while not (EOF(1))
  21.       input (1,data(i,0))
  22.       input (1,data(i,1))
  23.       i=i+1
  24.     end while
  25.   catch ex as exception
  26.     input (1,s)
  27.     lines=lines-1
  28.   end try
  30.   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?
Jan 27 '08 #1
Share this Question
Share on Google+
5 Replies

P: 2
sorry, was i unclear about something or do people just not know?
Jan 27 '08 #2

P: 220
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 ^^
Jan 27 '08 #3

Expert 5K+
P: 8,434
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.
Jan 28 '08 #4

Expert 5K+
P: 8,434
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.
Jan 28 '08 #5

Expert 5K+
P: 8,434
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...

Expand|Select|Wrap|Line Numbers
  1. Dim data(,) As Double
  2. Dim lines, i As Integer
  3. Dim s As String
  5. fileopen(1,"c:\mydatafile.txt", openmode.input)
  7. ' Count no of lines in data
  8. While Not (EOF(1))
  9.   input (1,s)
  10.   lines = lines + 1
  11. End While
  13. ' Reopen file to reposition at start.
  14. fileclose(1)
  15. fileopen(1,"c:\mydatafile.txt", openmode.input)
  17. ' Set array to appropriate size.
  18. ReDim data(lines,1)
  20. ' Read values into array, hopefully skipping non-numeric lines.
  21. While Not(EOF(1))
  22.   Try
  23.     Input (1,data(i,0))
  24.     Input (1,data(i,1))
  25.     i = i + 1
  26.   Catch ex As Exception
  27.     Input (1,s)
  28.     lines = lines - 1
  29.   End Try
  30. End While
  32. 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.
Jan 28 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.