473,394 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

inputting from a data file

2
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

Expand|Select|Wrap|Line Numbers
  1. dim data(,) as double
  2. dim lines, i as integer
  3. dim s as string
  4.  
  5. fileopen(1,"c:\mydatafile.txt", openmode.input)
  6.  
  7. lines=0
  8. while not (EOF(1))
  9.   input (1,s)
  10.   lines=lines+1
  11.   'counts no of lines in data
  12.  
  13.   fileclose(1)
  14.   fileopen(1,"c:\mydatafile.txt", openmode.input)
  15.  
  16.   redim data(lines,1)
  17.  
  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
  29.  
  30.   fileclose(1)
  31.  
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
5 1645
tim123
2
sorry, was i unclear about something or do people just not know?
Jan 27 '08 #2
werks
220 100+
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
Killer42
8,435 Expert 8TB
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
Killer42
8,435 Expert 8TB
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
Killer42
8,435 Expert 8TB
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
  4.  
  5. fileopen(1,"c:\mydatafile.txt", openmode.input)
  6.  
  7. ' Count no of lines in data
  8. While Not (EOF(1))
  9.   input (1,s)
  10.   lines = lines + 1
  11. End While
  12.  
  13. ' Reopen file to reposition at start.
  14. fileclose(1)
  15. fileopen(1,"c:\mydatafile.txt", openmode.input)
  16.  
  17. ' Set array to appropriate size.
  18. ReDim data(lines,1)
  19.  
  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
  31.  
  32. fileclose(1)
  33.  
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

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

Similar topics

1
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...
4
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...
5
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...
1
by: Ramper | last post by:
Have a .txt document as: String int int int int int int int
3
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...
6
namcintosh
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>
2
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++. ...
1
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...
15
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...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.