You are here:

Qbasic, Quickbasic/geting data from text file of purchase bill/invoice

Advertisement


Question
QUESTION: Hi Alex,
I tried above code.I got first record.but there is confusion about next record.
Here is what I tried :
DIM Linetxt AS STRING
DIM InvNo AS LONG    'bill no
DIM Invdt AS STRING 'bill date
DIM PrdNm AS STRING 'Product name
DIM Packsz AS STRING 'Pack Size
DIM CmpNm AS STRING'Company Name
DIM Batch AS STRING'Batch No
DIM DExp AS STRING 'Expiry Date
DIM Rate AS DOUBLE'Rate
DIM Mrp AS DOUBLE'Mrp
DIM Tprd AS DOUBLE'Total of Item
DIM Qty AS LONG'Quantity
DIM Sch AS LONG'Free Qty if any
DIM Dis AS DOUBLE'Discount
DIM VatRt AS DOUBLE'Vat rate
DIM VatAm AS DOUBLE'Vat amount
DIM NetAm AS DOUBLE'Net Amount

OPEN "shr.txt" FOR INPUT AS #1
INPUT #1, Linetxt
CLOSE #1

InvNo = VAL(LEFT$(Linetxt, 8))'start to length
Invdt = MID$(Linetxt, 9, 10) 'start to length
PrdNm = MID$(Linetxt, 29, 24)'Start to length
Packsz = MID$(Linetxt, 54, 6)'start to length
CmpNm = MID$(Linetxt, 71, 6)'start to length
Batch = MID$(Linetxt, 100, 9)'start to length
DExp = MID$(Linetxt, 111, 5)'start to length
Rate = VAL(MID$(Linetxt, 120, 6))'From left 120 to 6 characters
Mrp = VAL(MID$(Linetxt, 128, 6))'From left 128 to 6 characters
Qty = VAL(MID$(Linetxt, 148, 3))'From left 148 to 3 characters
Sch = VAL(MID$(Linetxt, 153, 3))'From left 153 to 3 characters
Dis = VAL(MID$(Linetxt, 173, 6))'From left 173 to 6 characters
VatRt = VAL(MID$(Linetxt, 178, 5))'From left 178 to 5 characters
VatAm = VAL(MID$(Linetxt, 186, 6))'From left 186 to 6 characters
NetAm = VAL(MID$(Linetxt, 220, 8))'From left 220 to 9 characters

   A = InvNo
   B$ = Invdt
   P$ = PrdNm
   P1$ = Packsz
   C$ = CmpNm
   B1$ = Batch
   E$ = DExp
   R = Rate
   M = Mrp
   Q = Qty
   T = (R * Q)  'Rate*Qty=Total of Item
   S = Sch
   D = Dis
   V = VatRt
   Va = VatAm
   N = NetAm
CLS

ANSWER: Hi Amit,

So, based on the code you provided, each record is exactly 228 characters long.  With this information,we can loop through each record easily, like this:

CONST recordLength% = 228

OPEN "shr.txt" FOR INPUT AS #1

DIM dataBuffer AS STRING
DIM Linetxt AS STRING

WHILE NOT EOF(1)
 LINE INPUT #1, fileData$
 dataBuffer = dataBuffer + fileData$
 IF LEN(dataBuffer) >= recordLength% THEN
   Linetxt = LTRIM$(RTRIM$(MID$(dataBuffer, 1, recordLength%)))
   dataBuffer = LEFT$(dataBuffer, LEN(dataBuffer) - recordLength% + 1)

   DIM Linetxt AS STRING
   DIM InvNo AS LONG    'bill no
   DIM Invdt AS STRING 'bill date
   DIM PrdNm AS STRING 'Product name
   DIM Packsz AS STRING 'Pack Size
   DIM CmpNm AS STRING'Company Name
   DIM Batch AS STRING'Batch No
   DIM DExp AS STRING 'Expiry Date
   DIM Rate AS DOUBLE'Rate
   DIM Mrp AS DOUBLE'Mrp
   DIM Tprd AS DOUBLE'Total of Item
   DIM Qty AS LONG'Quantity
   DIM Sch AS LONG'Free Qty if any
   DIM Dis AS DOUBLE'Discount
   DIM VatRt AS DOUBLE'Vat rate
   DIM VatAm AS DOUBLE'Vat amount
   DIM NetAm AS DOUBLE'Net Amount

   InvNo = VAL(LEFT$(Linetxt, 8))'start to length
   Invdt = MID$(Linetxt, 9, 10) 'start to length
   PrdNm = MID$(Linetxt, 29, 24)'Start to length
   Packsz = MID$(Linetxt, 54, 6)'start to length
   CmpNm = MID$(Linetxt, 71, 6)'start to length
   Batch = MID$(Linetxt, 100, 9)'start to length
   DExp = MID$(Linetxt, 111, 5)'start to length
   Rate = VAL(MID$(Linetxt, 120, 6))'From left 120 to 6 characters
   Mrp = VAL(MID$(Linetxt, 128, 6))'From left 128 to 6 characters
   Qty = VAL(MID$(Linetxt, 148, 3))'From left 148 to 3 characters
   Sch = VAL(MID$(Linetxt, 153, 3))'From left 153 to 3 characters
   Dis = VAL(MID$(Linetxt, 173, 6))'From left 173 to 6 characters
   VatRt = VAL(MID$(Linetxt, 178, 5))'From left 178 to 5 characters
   VatAm = VAL(MID$(Linetxt, 186, 6))'From left 186 to 6 characters
   NetAm = VAL(MID$(Linetxt, 220, 8))'From left 220 to 9 characters

   ' Do something with the data
 END IF
WEND

Let me know if that works for you.

-Alex

---------- FOLLOW-UP ----------

QUESTION: Thanks Alex,
Your answer works.but I want to print each value of variable in column & get sum of all
values in the same variable like " Tprd,VatAm,Dis " etc.I want it to be printed as
header & taler format to create 'csv' file as output.like below:

H...InvDt...InvNo...Tprd...VatAm(sum)...Dis(sum)...
T...PrdNm...Packsz..Batch..Exp..Mrp..Rate..Qty..VatRt..VatAm..Dis..Tprd..1st record
T...PrdNm...Packsz..Batch..Exp..Mrp..Rate..Qty..VatRt..VatAm..Dis..Tprd..2nd record
T...PrdNm...Packsz..Batch..Exp..Mrp..Rate..Qty..VatRt..VatAm..Dis..Tprd..3rd record
T...PrdNm...Packsz..Batch..Exp..Mrp..Rate..Qty..VatRt..VatAm..Dis..Tprd..4th record
to the end of the file
I tried hard but I can not do it.Sorry to disturb you,but your help is very important
for me.thanks.Below is what I do for it but there is some mistake.:
********************************************************************
CLS
DIM REC AS STRING
DIM InvNo AS LONG    'bill no
DIM InvDt AS STRING 'bill date
DIM PrdNm AS STRING 'Product name
DIM Packsz AS STRING 'Pack Size
DIM CmpNm AS STRING'Company Name
DIM Batch AS STRING'Batch No
DIM DExp AS STRING 'Expiry Date
DIM Rate AS DOUBLE'Rate
DIM Mrp AS DOUBLE'Mrp
DIM Tprd AS SINGLE'Total of Item
DIM Qty AS LONG'Quantity
DIM Sch AS LONG'Free Qty if any
DIM Dis AS SINGLE'Discount
DIM VatRt AS DOUBLE'Vat rate
DIM VatAm AS DOUBLE'Vat amount
DIM NetAm AS DOUBLE'Net Amount

INPUT "Enter File name:", NAME$
OPEN NAME$ FOR INPUT AS #1
OPEN "INV.DAT" FOR OUTPUT AS #2


CLS

INPUT #1, REC
InvNo = VAL(LEFT$(REC, 8))'start to length
InvDt = MID$(REC, 9, 10) 'start to length
Tprd = Rate * Qty
DbNt = VAL(MID$(REC, 200, 7))
CrNt = VAL(MID$(REC, 210, 7))
NetAm = VAL(MID$(REC, 220, 8))

PRINT #2, "H", ",", InvDt, ",", InvNo, ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", DbNt, ",", CrNt, ",", "0", ",", "0", ",", GrSt, ","; "0", ",", VatTl, ",", "0", ",", "0", ",", ",", "0", ",", DisT, ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", NetAm
CLOSE #1


INPUT "Enter File name:", NAME$
OPEN NAME$ FOR INPUT AS #1


FOR I = 1 TO 28
   I = (I + 1)

INPUT #1, REC
InvNo = VAL(LEFT$(REC, 8))'start to length
InvDt = MID$(REC, 9, 10) 'start to length
PrdNm = MID$(REC, 29, 24)'Start to length
Packsz = MID$(REC, 54, 6)'start to length
CmpNm = MID$(REC, 71, 6)'start to length
Batch = MID$(REC, 100, 9)'start to length
DExp = MID$(REC, 111, 5)'start to length
Rate = VAL(MID$(REC, 120, 6))'From left 119 to 6 characters
Mrp = VAL(MID$(REC, 128, 6))'From left 128 to 6 characters
Qty = VAL(MID$(REC, 148, 3))'From left 147 to 3 characters
Sch = VAL(MID$(REC, 153, 3))'From left 152 to 3 characters
Dis = VAL(MID$(REC, 173, 5))'From left 172 to 6 characters
VatRt = VAL(MID$(REC, 178, 5))'From left 178 to 5 characters
VatAm = VAL(MID$(REC, 186, 6))'From left 187 to 6 characters
NetAm = VAL(MID$(REC, 220, 8))'From left 212 to 9 characters
Tprd = VAL(MID$(REC, 134, 10))




PRINT #2, "T", ",", "0", ",", PrdNm, ",", Packsz, ",", Batch, ",", DExp, ",", Qty, ",", Sch, ",", Rate, ",", Mrp, ",", "0", ",", "0", ",", VatRt, ",", "0", ",", Tprd, ",", "0", ",", VatAm, ",", "0", ",", "0", ",", "2", ","; Dis, ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ","

NEXT I
      DO
         LINE INPUT #1, REC
         PRINT REC
      LOOP UNTIL (EOF(1))
CLOSE #1
CLOSE #2

END

Answer
Hi Amit,

Can you upload a copy of shr.txt (use something like gist or paste bin).  Also, what do you mean by there is confusion?  Do you get errors?  Can you check your outputs?

-Alex

Qbasic, Quickbasic

All Answers


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2016 About.com. All rights reserved.