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

VB6 - read from text file

P: n/a
sea
I have text files in the following format:

123,34,
,345,890,
123,23
..
..
..

As you can see, the problem is that (1) the commas can occur in
before, in the middle or after the 2 numbers and (2) there are no line
breaks in the file

Because there are no line breaks, line input does not work. I tried to
read character by character using Input(1, #1) but got stuck because
could not figure a way to tell the program to (1) ignore the 1st
comma, if any (2) start putting all numbers after 1st comma if any
into array1 (3) ignore middle comma if any (4) start putting all
numbers between middle and last commas (if any) into array2.

I know that C has an easy function, I am told, to read numbers alone
in separate columns but how to do this in Visual Basic?

Any help will be deeply appreciated. Thank you very much in advance.
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 18 Nov 2003 14:45:01 -0800, se*****@hotmail.com (sea) wrote:
I have text files in the following format:

123,34,
,345,890,
123,23
.
.
.

As you can see, the problem is that (1) the commas can occur in
before, in the middle or after the 2 numbers and (2) there are no line
breaks in the file

<snip>

Simplistically

Line Input

and

Split()
Jul 17 '05 #2

P: n/a
sea <se*****@hotmail.com> schreef in berichtnieuws
f8**************************@posting.google.com...

Hello Sea,
I have text files in the following format:

123,34,
,345,890,
123,23
.
.
.

As you can see, the problem is that (1) the commas can occur in
before, in the middle or after the 2 numbers and (2) there are no line
breaks in the file
No line-breaks ? And how do those numbers get on different lines then ?
Maybe you mean that VB does not *recognise* the line-breaks in your file ?
Because there are no line breaks, line input does not work. I tried to
read character by character using Input(1, #1) but got stuck because
could not figure a way to tell the program to (1) ignore the 1st
comma, if any
And what would happen if you would set a Flag if a numeric-character ("0"
thru "9") was found ? Just stick all chars together up until the next
comma, and check that flag. If that flag is not set, you have not got a
number :-)
(2) start putting all numbers after 1st comma if any
into array1
First comma (per line I suppose) ? And no line-breaks ? what dayamean ?
(3) ignore middle comma if any
Nope. That's not something you really want to do, ignoring comma's That
would effectivily stick two numbers together as one. In your example the
middle line would result in the number 345890 ....
(4) start putting all numbers between middle and
last commas (if any) into array2.
Advance an Index-variabele whenever you've encountered a comma *and* you
have a number.
I know that C has an easy function, I am told, to read numbers alone
in separate columns but how to do this in Visual Basic?
Not with the example you gave it does not. :-\
Any help will be deeply appreciated. Thank you very much in advance.


You're welcome.

If you have any further questions, post it & your code, and we will probably
be able to answer. :-)

By the way : Are you *sure* that all those comma's are seperation-comma's ?
I for one find it *very* strange to see lines without starting and/or ending
comma's, and see other lines with both (in short : a different count of
comma's per line)...

Regards,
Rudy Wieser

Jul 17 '05 #3

P: n/a
sea
Thank you all so very much for your valuable inputs. Yes, this text
file is really wierd, VB does not recognize line breaks, also the
format of the file does not match a specific pattern because the
numbers can be separated either by commas or spaces or both. This is
an output from an Unix machine and when it is opened using Notepad,
there are no line breaks at all, but when opened in WordPad, the line
breaks are visible sometimes.

Of all the proposed solutions, I think the one that might work best in
this particular situation is to flag numbers, and whenever a non
numerical character is found that would be the start of the second
number except for periods (because some numbers have decimal points
too, but fortunately there are no commas within the numbers!). Split
may be difficult because of inconsistent separation characters, either
commas or spaces.

So thank you all again and if I still have problems I will post the
code as suggested!

================================================== =======================


"R.Wieser" <rw***************@xs4all.nl> wrote in message news:<3f*********************@dreader5.news.xs4all .nl>...
sea <se*****@hotmail.com> schreef in berichtnieuws
f8**************************@posting.google.com...

Hello Sea,
I have text files in the following format:

123,34,
,345,890,
123,23
.
.
.

As you can see, the problem is that (1) the commas can occur in
before, in the middle or after the 2 numbers and (2) there are no line
breaks in the file


No line-breaks ? And how do those numbers get on different lines then ?
Maybe you mean that VB does not *recognise* the line-breaks in your file ?
Because there are no line breaks, line input does not work. I tried to
read character by character using Input(1, #1) but got stuck because
could not figure a way to tell the program to (1) ignore the 1st
comma, if any


And what would happen if you would set a Flag if a numeric-character ("0"
thru "9") was found ? Just stick all chars together up until the next
comma, and check that flag. If that flag is not set, you have not got a
number :-)
(2) start putting all numbers after 1st comma if any
into array1


First comma (per line I suppose) ? And no line-breaks ? what dayamean ?
(3) ignore middle comma if any


Nope. That's not something you really want to do, ignoring comma's That
would effectivily stick two numbers together as one. In your example the
middle line would result in the number 345890 ....
(4) start putting all numbers between middle and
last commas (if any) into array2.


Advance an Index-variabele whenever you've encountered a comma *and* you
have a number.
I know that C has an easy function, I am told, to read numbers alone
in separate columns but how to do this in Visual Basic?


Not with the example you gave it does not. :-\
Any help will be deeply appreciated. Thank you very much in advance.


You're welcome.

If you have any further questions, post it & your code, and we will probably
be able to answer. :-)

By the way : Are you *sure* that all those comma's are seperation-comma's ?
I for one find it *very* strange to see lines without starting and/or ending
comma's, and see other lines with both (in short : a different count of
comma's per line)...

Regards,
Rudy Wieser

Jul 17 '05 #4

P: n/a
If this is a unix file with unix returns (as it would appear to be if it
displays correctly in wordpad), try doing a Split with vbLf, rather than
Windows' vbCrLf.

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"sea" <se*****@hotmail.com> wrote in message
news:f8**************************@posting.google.c om...
: Thank you all so very much for your valuable inputs. Yes, this text
: file is really wierd, VB does not recognize line breaks, also the
: format of the file does not match a specific pattern because the
: numbers can be separated either by commas or spaces or both. This is
: an output from an Unix machine and when it is opened using Notepad,
: there are no line breaks at all, but when opened in WordPad, the line
: breaks are visible sometimes.
:
: Of all the proposed solutions, I think the one that might work best in
: this particular situation is to flag numbers, and whenever a non
: numerical character is found that would be the start of the second
: number except for periods (because some numbers have decimal points
: too, but fortunately there are no commas within the numbers!). Split
: may be difficult because of inconsistent separation characters, either
: commas or spaces.
:
: So thank you all again and if I still have problems I will post the
: code as suggested!
:
: ================================================== =======================
:
:
:
:
: "R.Wieser" <rw***************@xs4all.nl> wrote in message
news:<3f*********************@dreader5.news.xs4all .nl>...
: > sea <se*****@hotmail.com> schreef in berichtnieuws
: > f8**************************@posting.google.com...
: >
: > Hello Sea,
: >
: > > I have text files in the following format:
: > >
: > > 123,34,
: > > ,345,890,
: > > 123,23
: > > .
: > > .
: > > .
: > >
: > > As you can see, the problem is that (1) the commas can occur in
: > > before, in the middle or after the 2 numbers and (2) there are no line
: > > breaks in the file
: >
: > No line-breaks ? And how do those numbers get on different lines then ?
: > Maybe you mean that VB does not *recognise* the line-breaks in your file
?
: >
: > > Because there are no line breaks, line input does not work. I tried to
: > > read character by character using Input(1, #1) but got stuck because
: > > could not figure a way to tell the program to (1) ignore the 1st
: > > comma, if any
: >
: > And what would happen if you would set a Flag if a numeric-character
("0"
: > thru "9") was found ? Just stick all chars together up until the next
: > comma, and check that flag. If that flag is not set, you have not got a
: > number :-)
: >
: > > (2) start putting all numbers after 1st comma if any
: > > into array1
: >
: > First comma (per line I suppose) ? And no line-breaks ? what dayamean
?
: >
: > > (3) ignore middle comma if any
: >
: > Nope. That's not something you really want to do, ignoring comma's
That
: > would effectivily stick two numbers together as one. In your example
the
: > middle line would result in the number 345890 ....
: >
: > > (4) start putting all numbers between middle and
: > > last commas (if any) into array2.
: >
: > Advance an Index-variabele whenever you've encountered a comma *and* you
: > have a number.
: >
: > > I know that C has an easy function, I am told, to read numbers alone
: > > in separate columns but how to do this in Visual Basic?
: >
: > Not with the example you gave it does not. :-\
: >
: > > Any help will be deeply appreciated. Thank you very much in advance.
: >
: > You're welcome.
: >
: > If you have any further questions, post it & your code, and we will
probably
: > be able to answer. :-)
: >
: > By the way : Are you *sure* that all those comma's are
seperation-comma's ?
: > I for one find it *very* strange to see lines without starting and/or
ending
: > comma's, and see other lines with both (in short : a different count of
: > comma's per line)...
: >
: > Regards,
: > Rudy Wieser
Jul 17 '05 #5

P: n/a
> ....<snip>..... VB does not recognize line breaks, also the
format of the file does not match a specific pattern because the
numbers can be separated either by commas or spaces or both.
This is an output from an Unix machine and ...<snip>....
Amend that to say "VB does not reconize UNIX line breaks". In UNIX, the Line
Feed character is used as its newline character. In MACs, a Carriage Return
is used for the newline character. In VB, the two are combined... VB uses a
Carriage Return followed by a Line Feed as a newline character sequence.
Some programs, like WordPad, recognize the need to work in UNIX and Windows
and honors either system's newline character(s) indicator.

....<snip>..... Split may be difficult because of inconsistent
separation characters, either commas or spaces.


I'm not sure if the newline character placements have significance or not
(your sample data posted earlier seems to indicate not), but you can
overcome the different separators by using the Replace function first.

YourText = "....assign it however you are doing it now...."
PurifiedText = Replace$(YourText, vbNewLine, ",")
PurifiedText = Replace$(PurifiedText, " ", ",")

At this point, the PurifiedText variable should contain all your numbers in
a purely comma delimited file consisting of a single line of text. (Note
that vbNewLine, as well as vbCrLf, is a predefined String constant
containing the characters making up a Windows' newline.) Anyway, you can now
Split this data into an array

Dim IndividualNumbers() As Double
IndividualNumbers = Split(PurifiedText, ",")

Rick - MVP
Jul 17 '05 #6

P: n/a

"sea" <se*****@hotmail.com> wrote in message
news:f8**************************@posting.google.c om...
Thank you all so very much for your valuable inputs. Yes, this text
file is really wierd, VB does not recognize line breaks, also the
format of the file does not match a specific pattern because the
numbers can be separated either by commas or spaces or both. This is
an output from an Unix machine and when it is opened using Notepad,
there are no line breaks at all, but when opened in WordPad, the line
breaks are visible sometimes.

{snip}
You could always use a program like COnversionas PLus (or one of may UNIX -
WINDOWS converters) to convert a UNIX text file to a Windows DOS text file.
Maybe that will help

Scott C. Zielinski

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.