Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error in reading data file QB64

Status
Not open for further replies.

PeterTX

Technical User
Apr 24, 2022
2
US
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
 
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:
[COLOR=#0000ff]'writing and reading text file[/color]
[COLOR=#804040][b]Const[/b][/color] N = [COLOR=#ff00ff]8[/color]
[COLOR=#804040][b]Dim[/b][/color] A(N - [COLOR=#ff00ff]1[/color], N - [COLOR=#ff00ff]1[/color])
[COLOR=#804040][b]Dim[/b][/color] B(N - [COLOR=#ff00ff]1[/color], N - [COLOR=#ff00ff]1[/color])
data_file[COLOR=#2e8b57][b]$[/b][/color] = [COLOR=#ff00ff]"my_data.txt"[/color]
quote_char[COLOR=#2e8b57][b]$[/b][/color] = Chr[COLOR=#2e8b57][b]$[/b][/color]([COLOR=#ff00ff]34[/color])
tab_char[COLOR=#2e8b57][b]$[/b][/color] = Chr[COLOR=#2e8b57][b]$[/b][/color]([COLOR=#ff00ff]9[/color])


[COLOR=#0000ff]'populate matrix A with random data and print it[/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#ff00ff]"matrix A:"[/color]
[COLOR=#804040][b]For[/b][/color] i = [COLOR=#ff00ff]0[/color] To N - [COLOR=#ff00ff]1[/color]
    [COLOR=#804040][b]For[/b][/color] j = [COLOR=#ff00ff]0[/color] To N - [COLOR=#ff00ff]1[/color]
        [COLOR=#804040][b]Randomize[/b][/color] [COLOR=#804040][b]Timer[/b][/color]
        A(i, j) = [COLOR=#008080]Int[/color]([COLOR=#008080]Rnd[/color] * [COLOR=#ff00ff]100[/color])
        [COLOR=#804040][b]Print[/b][/color] [COLOR=#804040][b]Using[/b][/color] [COLOR=#ff00ff]"  ##"[/color]; A(i, j);
    [COLOR=#804040][b]Next[/b][/color] j
    [COLOR=#804040][b]Print[/b][/color] '[COLOR=#804040][b]next[/b][/color] [COLOR=#804040][b]line[/b][/color]
[COLOR=#804040][b]Next[/b][/color] i
[COLOR=#804040][b]Print[/b][/color]


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


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


[COLOR=#0000ff]'print matrix B[/color]
[COLOR=#804040][b]Print[/b][/color] [COLOR=#ff00ff]"matrix B:"[/color]
[COLOR=#804040][b]For[/b][/color] i = [COLOR=#ff00ff]0[/color] To N - [COLOR=#ff00ff]1[/color]
    [COLOR=#804040][b]For[/b][/color] j = [COLOR=#ff00ff]0[/color] To N - [COLOR=#ff00ff]1[/color]
        [COLOR=#804040][b]Print[/b][/color] [COLOR=#804040][b]Using[/b][/color] [COLOR=#ff00ff]"  ##"[/color]; B(i, j);
    [COLOR=#804040][b]Next[/b][/color] j
    [COLOR=#804040][b]Print[/b][/color] '[COLOR=#804040][b]next[/b][/color] [COLOR=#804040][b]line[/b][/color]
[COLOR=#804040][b]Next[/b][/color] 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:
[tt]NUM TAB NUM TAB .. TAB NUM CR LF[/tt]
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.
 
Dear Mikrom,
Thank you for your great help. It really solved the problem.

Peter
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top