How to get the values in the .txt document?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
WZY
Newbie
Posts: 2
Joined: Fri Dec 21, 2012 1:21 am

How to get the values in the .txt document?

Post 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.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
WZY
Newbie
Posts: 2
Joined: Fri Dec 21, 2012 1:21 am

Thank you.

Post 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.
Post Reply