Page 1 of 1

How to get the values in the .txt document?

Posted: Fri Dec 21, 2012 2:01 am
by WZY
A .txt file, forms as follows:

200 6180
201 7000
...

There is a Tab between two values in the same line.
How can I respectively take the value of 200 and 6180?
Thank you.

Posted: Fri Dec 21, 2012 11:08 am
by burger2227
Use LINE INPUT # to get both numbers as a string:

Code: Select all

file$ = "TestData.txt"

OPEN file$ FOR OUTPUT AS #1 'create a test file
PRINT #1, "200"  + CHR$(9) + "6180"
CLOSE #1

OPEN file$ FOR INPUT AS #1 'file name must exist
IF LOF(1) THEN 'file must have a length
  LINE INPUT #1, text$ 'get entire line of text
  posit = INSTR(2, text$, CHR$(9)) 'find position of spacer
  num1$ = MID$(text$, 1, posit - 1) 'first number
  num2$ = MID$(text$, posit + 1) 'second number
  n1 = VAL(num1$) 'convert to a real numbers
  n2 = VAL(num2$) 'VAL will ignore leading spaces
END IF
PRINT n1, n2
CLOSE #1
CHR$(9) is a real tab and not a space. If it is just a space, use SPACE$(1) instead. 2 is used in INSTR because a printed number may have a leading space.

Use WRITE #, to create comma separated file number values in a file that can be opened and read using INPUT:

Code: Select all

file$ = "TestData.txt"
OPEN file$ FOR OUTPUT AS #1
WRITE #1, 200, 6180 'variable values can be substituted for real values
CLOSE #1

OPEN file$ FOR INPUT AS #1
INPUT #1, n1, n2
CLOSE #1

PRINT n1, n2

Thank you.

Posted: Mon Dec 24, 2012 11:05 pm
by WZY
You have solved my problem, thank you very much.
I want to get a copy of QB64, but I cannot open the link http://www.qb64.net/forum/ , so whether you can send it to me? My email address is 17390573@qq.com.