PUT creates binary Random-Access file?

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
Geminga
Coder
Posts: 10
Joined: Fri Sep 24, 2010 3:26 pm

PUT creates binary Random-Access file?

Post by Geminga »

The following code is supposed to re-format a fixed-width database file as CSV.
The output contains the correct number of bytes, but is no longer text:(
The book 'QBasic by Example' sheds no light on this.

What did I do wrong?
TIA
------------------------------------------------------------------------------------------
'A program to convert a database from fixed-width to CSV format.
'The source is DOS formatted text, 2,501,314 records.
'The All-Sky Compiled Catalog of the 2.5 million Brightest Stars
CLS
TYPE star 'each record has all data for a single star
col1 AS STRING * 12 '38 fields within each record
col2 AS STRING * 12 'string data in all fields
col3 AS STRING * 4
col4 AS STRING * 4
col5 AS STRING * 6
col6 AS STRING * 5
col7 AS STRING * 7
col8 AS STRING * 7
col9 AS STRING * 5
col10 AS STRING * 5
col11 AS STRING * 5
col12 AS STRING * 5
col13 AS STRING * 4
col14 AS STRING * 4
col15 AS STRING * 4
col16 AS STRING * 1
col17 AS STRING * 1
col18 AS STRING * 1
col19 AS STRING * 1
col20 AS STRING * 2
col21 AS STRING * 1
col22 AS STRING * 1
col23 AS STRING * 1
col24 AS STRING * 1
col25 AS STRING * 20
col26 AS STRING * 4
col27 AS STRING * 5
col28 AS STRING * 1
col29 AS STRING * 6
col30 AS STRING * 6
col31 AS STRING * 8
col32 AS STRING * 7
col33 AS STRING * 5
col34 AS STRING * 4
col35 AS STRING * 5
col36 AS STRING * 4
col37 AS STRING * 5
col38 AS STRING * 4
END TYPE
OPEN "c:\280b\i280b.txt" FOR RANDOM AS #1
OPEN "c:\windows\desktop\i280b.txt" FOR RANDOM AS #2
DO
GET #1 'read one record at a time starting at 1
PUT #2, , star 'write one record to the end of the file
LOOP UNTIL EOF(1)
CLOSE
PRINT
PRINT "Done"
END
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Simple, the CSV file IS NOT a RANDOM file!

A Random file has fixed length records while a CSV file is a comma separated text file. It is considered a sequencial file.

Random files are read by GET in bytes that cannot SKIP any bytes like commas. BINARY and RANDOM files will convert numerical values to ASCII characters too.

My advise, read the CSV file as a CSV file with INPUT #. Then you can do what you desire with the data. You MUST define a DOT variable to use the TYPE variables.

Code: Select all

TYPE StarType
col1 AS STRING * 12   'this value uses 12 bytes
col2 AS STRING * 3     ' 3 bytes
etc.
END TYPE

DIM Star AS StarType   '<<<<<<<<<<<<<< important!
Now you have a DOT variable name. So the first variable will be named Star.col1 etc.

When using a RANDOM file you MUST define the LENGTH of the RECORD or it will default to 128 bytes! You COULD just add up all of the TYPE string lengths OR you can just measure your TYPE variable Star.

Code: Select all

RecordLEN% = LEN(Star)  'just use the length of your TYPE variable

OPEN file1$ FOR INPUT AS #1
OPEN file2$ FOR RANDOM AS #2 LEN = RecordLEN%

DO UNTIL EOF(1)

INPUT #1. Star.col1, Star.col2, Star.col3....Star.col38  'you are defining the values as you go.
PUT #2, , Star   'PUT the entire TYPE data in ONE PUT using DOT variable first name < neat huh?

LOOP

CLOSE

To read the data later, use the SAME TYPE with GET #, ,Star and DOT variable names.


Ted
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
Geminga
Coder
Posts: 10
Joined: Fri Sep 24, 2010 3:26 pm

Post by Geminga »

Thanks burger!

I'll give that a try.
Geminga
Coder
Posts: 10
Joined: Fri Sep 24, 2010 3:26 pm

Post by Geminga »

Well, still a problem.
The code as sugested wouldn't "debug" in QBasic. An error about including a "."
"Identifier cannot include a period". I see in other threads that this a common prob...
--------------------------------
CLS
TYPE startype
col1 AS STRING * 12
.
.
.
col37 AS STRING * 5
col38 AS STRING * 4
END TYPE
DIM star AS startype
RecordLEN% = LEN(star)
OPEN "c:\280b\i280b.txt" FOR INPUT AS #1
OPEN "c:\windows\desktop\i280b.txt" FOR RANDOM AS #2 LEN = RecordLEN%
DO
INPUT #1, star.col1...col37.col38
PUT #2, , star
LOOP UNTIL EOF(1)
CLOSE
PRINT
PRINT "Done"
END
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Where is the error pointing to? When you click OK in error box, where is the cursor?

You can't do it EXACTLY like I showed you! You have to use 38 variable names with 37 commas between them. I only see ONE STAR.COL1

IF you cannot use DOT variable names in the INPUT #, you will have to assign them.

INPUT #1, col1, col2, ....etc.

Star.col1 = col1: Star.col2 = col2: etc.

PLEASE start using the CODE button or place

Code: Select all

 before and with /code  after program code.
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
Geminga
Coder
Posts: 10
Joined: Fri Sep 24, 2010 3:26 pm

Post by Geminga »

Here's the code:

Code: Select all

DO
  INPUT #1, col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21, col22, col23, col24, col25, col26, col27, col28, col29, col30, col31, col32, col33, col34, col35, col36, col37, col38
  star.col1 = col1: star.col2 = col2:star.col3 = col3:star.col4 = col4:star.col5 = col5:star.col6 = col6:star.col7 = col7:star.col8 = col8:star.col9 = col9:star.col10 = col10:star.col21 = col21: star.col22 = col22:star.col23 = col23:star.col24 = col24:star.col25 = col25:star.col26 = col26:star.col27 = col27:star.col28 = col28:star.col29 = col29:star.col30 = col30:star.col31 = col31: star.col32 = col32:star.col33 = col33:star.col34 = col34:star.col35 = col35:star.col36 = col36:star.col37 = col37:star.col38 = col38:
  PUT #2, , star
LOOP UNTIL EOF(1)
The ERR MSG is now "type mismatch" with the highlight on the first occurance of .col

Thanks again
Geminga
Coder
Posts: 10
Joined: Fri Sep 24, 2010 3:26 pm

Post by Geminga »

CODE button not working as expected.
Yep, I'm a newbie...
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

MY BAD! Add $ after the col variable names as you are not using the array definition of a string.

Code: Select all

INPUT #1, col1$, col2$, col3$....etc.
Star.col1 = col1$: Star.col2 = col2$: Star.col3 = col3$...etc.
OR

Code: Select all

DIM col(38) AS STRING   'NO MULTIPLIER as each string has different length

INPUT #1, col(1), col(2), col(3), ....etc.
Star.col1 = col(1): Star.col2 = col(2): Star.col3 = col(3) ...etc.
The LENGTHS must match the TYPE lengths too or they may contain extra spaces or be cut off!

I edited your post. (Don't try to do that at home kids!) You had the Disable BBCode in this post box checked. :wink:
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
Geminga
Coder
Posts: 10
Joined: Fri Sep 24, 2010 3:26 pm

Post by Geminga »

Ahhh... OK. Well, today is probably my last day of "playing on the computer".
First-of-the-month means returning to other pressing issues.
That last post makes sense, Burger, hopefuly that clears it up. Thanks again, and
maybe we'll bump into each other in the future.
Geminga, in VT
aka Pete R.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Well, stop by and let me know how it went. I'm interested in seeing what file sizes QB can read.

Take care

Ted from Pittsburgh, PA

Home of the Steelers!
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
Theunis
Coder
Posts: 21
Joined: Sun Oct 17, 2010 5:41 am

DOS to CVS

Post by Theunis »

This is obviously homework, But as you tried, here is a program with two errors which you have to find.
One is obvious the other relates to the number of records.
The following steps are suggested to help you with programming.
1.) Download Ted's program.
2.) Check Pete's site for tutorials
3.) check QBcafe for tutorials.
4.) Do a Google search for QB tutorials.

Code: Select all

' A Program to convert a database from fixed-width to CSV format.
' The source is DOS formatted text, 2,501,314 records.
' The All-Sky Compiled Catalog of the 2.5 million Brightest Stars
' Written for Geminga By Theunis on 2010-10-18
' File modified from a similar program I wrote in 1996
' This program contains two errors

REM $Dynamic

DECLARE SUB ConvertCSV()
DIM SHARED CVS$

TYPE Star                         ' each record has all data for a single star
    col1 AS STRING * 12     ' 38 fields within each record
    col2 AS STRING * 12     ' string data in all fields
    col3 AS STRING * 4
    col4 AS STRING * 4
    col5 AS STRING * 6
    col6 AS STRING * 5
    col7 AS STRING * 7
    col8 AS STRING * 7
    col9 AS STRING * 5
    col10 AS STRING * 5
    col11 AS STRING * 5
    col12 AS STRING * 5
    col13 AS STRING * 4
    col14 AS STRING * 4
    col15 AS STRING * 4
    col16 AS STRING * 1
    col17 AS STRING * 1
    col18 AS STRING * 1
    col19 AS STRING * 1
    col20 AS STRING * 2
    col21 AS STRING * 1
    col22 AS STRING * 1
    col23 AS STRING * 1
    col24 AS STRING * 1
    col25 AS STRING * 20
    col26 AS STRING * 4
    col27 AS STRING * 5
    col28 AS STRING * 1
    col29 AS STRING * 6
    col30 AS STRING * 6
    col31 AS STRING * 8
    col32 AS STRING * 7
    col33 AS STRING * 5
    col34 AS STRING * 4
    col35 AS STRING * 5
    col36 AS STRING * 4
    col37 AS STRING * 5
    col38 AS STRING * 4
END TYPE

DIM SHARED Stars as Star
CLS
OPEN "c:\280b\i280b.txt" FOR RANDOM AS #1 LEN = LEN(Stars)
  ' Establish number of records
  NumRecords = LOF(1) \ LEN(Stars)
      
' Open new file as APPEND 
 OPEN "c:\windows\desktop\i280b.CSV" FOR APPEND AS #2 

For i = 1 to NumRecords
	GET #1, i, Stars         ' read one record at a time starting at 1
	ConvertCVS              ' Call Sub to change DOS fixed length to comma delimited.  
	PRINT #2, CSV$          ' write one record to the end of the file
NEXT
CLOSE #1, #2
PRINT "Done"

END


Sub ConvertCVS
  ' This sub concatenates all the fields with a comma between the fields.
  ' i.e same as a CSV file. 
 Inn$(1) = Stars.Col1
 Inn$(2) = Stars.Col2
 Inn$(3) = Stars.col3
 Inn$(4) = Stars.col4
 Inn$(5) = Stars.col5
 Inn$(6) = Stars.col6
 Inn$(7) = Stars.col7
 Inn$(8) = Stars.col8
 Inn$(9) = Stars.col9
 Inn$(10) = Stars.col10
 Inn$(11) = Stars.col11
 Inn$(12) = Stars.col12
 Inn$(13) = Stars.col13
 Inn$(14) = Stars.col14
 Inn$(15) = Stars.col15
 Inn$(16) = Stars.col16
 Inn$(17) = Stars.col17
 Inn$(18) = Stars.col18
 Inn$(19) = Stars.col19
 Inn$(20) = Stars.col20
 Inn$(21) = Stars.col21
 Inn$(22) = Stars.col22
 Inn$(23) = Stars.col23
 Inn$(24) = Stars.col24
 Inn$(25) = Stars.col25
 Inn$(26) = Stars.col26
 Inn$(27) = Stars.col27
 Inn$(28) = Stars.col28
 Inn$(29) = Stars.col29
 Inn$(30) = Stars.col30
 Inn$(31) = Stars.col31
 Inn$(32) = Stars.col32
 Inn$(33) = Stars.col33
 Inn$(34) = Stars.col34
 Inn$(35) = Stars.col35
 Inn$(36) = Stars.col36
 Inn$(37) = Stars.col37
 Inn$(38) = Stars.col38
 CSVString$ = ""   : CSV$ = ""     'Reset to ""
 For C = 1 to 37
     'Concatenate Inn$( 1 to 37). Remove leading/trailing blanks
    CSVString$ = ltrim$(rtrim$(Inn$(C))) + ","  
 NEXT
 CSV$ = CSVString$ + ltrim$(rtrim$(Inn$(38)))   ' Add the last string without trailing  ","
 
END SUB 
Last edited by Theunis on Wed Oct 20, 2010 4:06 am, edited 4 times in total.
Theunis
Coder
Posts: 21
Joined: Sun Oct 17, 2010 5:41 am

Dos to csv

Post by Theunis »

Oh well.
error no 1. Dim shared Inn$(38)
error no 2. No error. Just to see what comment I would get.
QB4.5 Default is Long Integer. i.e. 2 Gig. So integer division \ is correct.
QB4.5 Data file record numbers. 2,147,483,647
QB4.5 Data file size. Available disk space.
Last edited by Theunis on Mon Oct 18, 2010 6:14 am, edited 1 time in total.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

The biggest error was that you cannot check the Disable BBC box and use

Code: Select all

 too! I fixed it for ya.
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
Theunis
Coder
Posts: 21
Joined: Sun Oct 17, 2010 5:41 am

DOS to CSV

Post by Theunis »

Thanks Ted

I wondered what was going on when it didn't show it as code.
This is my ninth attempt to reply.
I keep being shunted to the Log In screen. It shows I am logged in and then returns to the submit and when I click it goes back to the LogIn and my previous LogIn has vanished.
Post Reply