473,216 Members | 1,351 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,216 software developers and data experts.

VB6 - read from text file

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
6 30923
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
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
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
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
> ....<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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
3
by: Ray | last post by:
Hello World, I made a Windowsform that reads data from a CSV file. It works fine, but when I have read the data of a record I have to re-Debug the form to read another record. So when I put a...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
0
by: alivip | last post by:
Is python provide search in parent folder contain sub folders and files for example folder name is cars and sub file is Toyota,Honda and BMW and Toyota contain file name camry and file name corola,...
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.