×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Error in reading data file QB64

Error in reading data file QB64

Error in reading data file QB64

(OP)
Dear Qbasic friends,
I am new to this group, I just use the qbasic for my research calculation because it is very easy to write the codes. Now I need your help with data file reading. I downloaded a data file which contains only numbers separated with TAB not the space, each line has 10 numbers, the first line likes 1 2 3 4 5 6 7 8 9 10, the second line likes 11 12 13 14 15 16 17 18 19 20, ......
the input statement is INPUT #1, a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10). However, it only reads the first number from each line, i.e, it only read 1 and 11....

I tested a data file with numbers separated by space created by Notepad, it works well, it will not work if the numbers are separated by tabs.

I cannot change the original data file. So please help me to solve the problem, maybe an easy fix.
I really appreciate your help.

Peter

RE: Error in reading data file QB64

Hi PeterTX,
I tried to read the file line by line and parse numbers from every line by locating the TAB-characters and the numbers, i used INSTR and MID$ functions for this..

Here is the example, which you can try:
text_file.bas

CODE

'writing and reading text file
Const N = 8
Dim A(N - 1, N - 1)
Dim B(N - 1, N - 1)
data_file$ = "my_data.txt"
quote_char$ = Chr$(34)
tab_char$ = Chr$(9)


'populate matrix A with random data and print it
Print "matrix A:"
For i = 0 To N - 1
    For j = 0 To N - 1
        Randomize Timer
        A(i, j) = Int(Rnd * 100)
        Print Using "  ##"; A(i, j);
    Next j
    Print 'next line
Next i
Print


Print "Writing matrix A to the file " _
      + quote_char$ + data_file$ + quote_char$
Print
'write matrix A into the file
'numbers are separated by tab-characters
Open data_file$ For Output As #1
For i = 0 To N - 1
    output_line$ = ""
    For j = 0 To N - 1
        A_i_j$ = LTrim$(Str$(A(i, j)))
        If j < N - 1 Then
            output_line$ = output_line$ + A_i_j$ + tab_char$
        Else
            output_line$ = output_line$ + A_i_j$
        End If
    Next j
    Print #1, output_line$
Next i
Close #1


Print "Reading data from the file " _
      + quote_char$ + data_file$ + quote_char$ _
      " into matrix B"
Print
Open data_file$ For Input As #1
i = 0
Do While Not EOF(1)
    'input_line has the structure:
    'NUM TAB NUM TAB .. TAB NUM CR LF
    Input #1, input_line$
    'parse numbers from the input_line string
    'and store them into the matrix B
    start = 1
    For j = 0 To N - 1
        tab_idx = InStr(start, input_line$, tab_char$)
        If tab_idx Then
            B(i, j) = Val(Mid$(input_line$, start, tab_idx - start + 1))
            start = tab_idx + Len(tab_char$)
        Else
            line_len = Len(input_line$)
            B(i, j) = Val(Mid$(input_line$, start, line_len - start + 1))
        End If
    Next
    i = i + 1
Loop
Close #1


'print matrix B
Print "matrix B:"
For i = 0 To N - 1
    For j = 0 To N - 1
        Print Using "  ##"; B(i, j);
    Next j
    Print 'next line
Next i 

The program first creates the matrix A of random numbers and prints it.
Then it writes the matrix A into text file with the structure:
NUM TAB NUM TAB .. TAB NUM CR LF
like this:
my_data.txt

CODE

4	95	73	13	94	77	3	23
90	89	65	38	91	34	58	85
50	83	56	64	14	91	86	74
10	4	48	90	37	48	41	63
69	98	39	42	60	79	69	26
56	92	31	68	57	36	97	15
16	12	22	93	80	94	52	77
75	6	14	19	3	51	80	66 

At end it reads the data from the file into matrix B and prints it.

RE: Error in reading data file QB64

Hi PeterTX,

Is it working for you ?

RE: Error in reading data file QB64

(OP)
Dear Mikrom,
Thank you for your great help. It really solved the problem.

Peter

RE: Error in reading data file QB64

PeterTX,
you are welcome

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close