reading from a text file problem

  • Thread starter Thread starter SteMc
  • Start date Start date
S

SteMc

today I tackled, for the first time, opening and reading from a text
file.

Following the example on the MSDN and declared a variable, strline as a
string and objstreamreader as a streamreader.

Basically I read in a line of a text file - this works fine.
I then read in another line. This works fine
However, the third time round I read character by character, until I
get to to the 40th character on the line, because this is the position
of some information that is a variable that I need. The problem is that
when using the read command (strline = objstreamreader.read) each
character is being given as a number (for example, when a space is read
in the string shows the number "32"). When I get to the correct
character I should get the string "1", instead I'm getting something
else. (A different number, but it's definately a translation of 1, not
the wrong character because all other characters on the line are
letters from a to z).

does anyone know why this happens and how I can re-translate the
number/code back into an alphanumeric version?

thanks,

Steve
 
SteMc said:
today I tackled, for the first time, opening and reading from a text
file.

Following the example on the MSDN and declared a variable, strline as a
string and objstreamreader as a streamreader.

Basically I read in a line of a text file - this works fine.
I then read in another line. This works fine
However, the third time round I read character by character, until I
get to to the 40th character on the line, because this is the position
of some information that is a variable that I need. The problem is that
when using the read command (strline = objstreamreader.read) each
character is being given as a number (for example, when a space is read
in the string shows the number "32"). When I get to the correct
character I should get the string "1", instead I'm getting something
else. (A different number, but it's definately a translation of 1, not
the wrong character because all other characters on the line are
letters from a to z).

does anyone know why this happens and how I can re-translate the
number/code back into an alphanumeric version?

thanks,

Steve

When you read character by character you are getting the ASCII
representation of the character. If you do Chr(strline) on the one you
expect as "1" you will get your "1".

You may want to just read in the entire line and then do a substring on it.

strline = objstreamreader.readline
debug.writeline("My 40th Char is: " & strline.substring(39,1))

Chris
 
Ste,

The streamreader reads forever into a String (It can be in another format on
disk and than you can use the encoding). A String is in .Net forever in
Unicode format.

To get the value for that you can use the convert to Ascii which was in past
Asc and because that the characterset is now more wide better to use the
newer AscW("2"). The last instruction will give you "32".

In the otherway to get from a Unicode a Charachter it is ChrW(32) which will
give a 2.

I hope this helps,

Cor
 
Back
Top