You are here:

Qbasic, Quickbasic/about txt file editing

Advertisement


Question
greetings, i read your answer to an another question about changing data as a new file, but what happened was that it reached the end of file
here's my program
10 CLS
15 keyp$ = inkeys$
20 no = 0
30 DIM namafile AS STRING
40 DIM text AS STRING
50 DIM jawab AS STRING
60 DIM berat AS INTEGER
61 DIM PILIHAN AS STRING
70 INPUT "INGIN BUAT FILE(A) ATAU EDIT DATA(B)?", PILIHAN
71 PILIHAN = UCASE$(PILIHAN)
SELECT CASE PILIHAN
   CASE "A"
       80 INPUT "masukkan nama file :", namafile
       90 OPEN namafile FOR OUTPUT AS #1
       100 PRINT "Silahkan masukkan data,jika ingin berhenti Ketik SELESAI"
       110 no = no + 1
       120 PRINT "DATA KE "; no
       130 INPUT "NO PENGIRIMAN"; nopeng$
       140 IF nopeng$ = "SELESAI" OR nopeng$ = "selesai" THEN GOTO 230
       150 INPUT "NAMA"; nama$
       151 IF LEN(nama$) > 6 THEN
         152 nama$ = MID$(nama$, 1, 6)
       154 END IF

       160 INPUT "BERAT"; berat
       170
       180 WRITE #1, nopeng$
       190 WRITE #1, nama$
       200 WRITE #1, berat
       INPUT "Data selanjutnya", keyp$
       IF keyp$ = CHR$(27) THEN GOTO 230
       IF keyp$ = CHR$(13) THEN GOTO 230
       210 GOTO 110
       220
       230 INPUT "YAKIN? Y/T"; jawab
       240 jawab = UCASE$(jawab)
       250 IF jawab = "Y" THEN GOTO 280
       260 IF jawab = "T" THEN GOTO 110
       270 GOTO 230
       280 CLOSE #1
   CASE "B"
       INPUT "HAPUS SALAH SATU DATA(1) ATAU TAMBAHKAN DATA?"; PILIHAN2
       SELECT CASE PILIHAN2
         CASE 1
         INPUT "MASUKKAN NAMA FILE :", namafile
         filehapus$ = namafile
         newfile$ = filehapus$ + ".dat"
         OPEN namafile FOR INPUT AS #1
         OPEN newfile$ FOR OUTPUT AS #2

         WHILE NOT EOF(1)
         1000 INPUT "masukkan no pengiriman dari data yang ingin dihapus"; ega$
         1200 LINE INPUT #1, nopeng$
         LINE INPUT #1, nama$
         INPUT #1, berat
         IF nopeng$ = ega$ THEN
         PRINT "masukkan data baru"
         INPUT "Nomor Pengiriman Baru", nopengbaru$
         INPUT "nama baru", namabaru$
         INPUT "berat baru"; berat
         WRITE #2, nopengbaru$
         WRITE #2, namabaru$
         WRITE #2, berat
         INPUT "ingin edit data lain?Y/T"; set$
         set$ = UCASE$(set$)
         IF set$ = "Y" THEN GOTO 1000
         IF set$ = "T" THEN GOTO 1200
         END IF
         WRITE #2, nopeng$
         WRITE #2, nama$
         WRITE #2, berat

         WEND

         CLOSE #2
         CLOSE #1

         kill namafile
         name newfile$ as namafile
         CASE 2
         300 INPUT "masukkan nama file :", namafile
         301 OPEN namafile FOR INPUT AS #1
         302 WHILE NOT EOF(1)
         303 INPUT #1, nopeng$
         304 INPUT #1, nama$
         305 INPUT #1, berat
         306 no = no + 1
         307 WEND
         308 CLOSE #1
         310 OPEN namafile FOR APPEND AS #1
         320 PRINT "Silahkan masukkan data,jika ingin berhenti Ketik SELESAI"
         330
         no = no + 1
         331 PRINT "DATA KE "; no
         332 INPUT "NO PENGIRIMAN"; nopeng$
         IF nopeng$ = "selesai" OR nopeng$ = "SELESAI" THEN GOTO 430
         350 INPUT "NAMA"; nama$
         360 INPUT "BERAT"; berat
         370
         380 WRITE #1, nopeng$
         390 WRITE #1, nama$
         400 WRITE #1, berat

         410 GOTO 330
         420
         430 INPUT "YAKIN? Y/T"; jawab
         440 jawab = UCASE$(jawab)
         450 IF jawab = "Y" THEN GOTO 480
         460 IF jawab = "T" THEN GOTO 330
         470 GOTO 230
         480 CLOSE #1
       END SELECT
490 END SELECT

Answer
Hi Ega,

I'm currently on a system that I don't have access to QBasic, so I can't run your code, and I don't read Indonesian(?) so I'm not 100% sure what data I would have to feel your program to make it run into that error.

One thing that I can see that might be causing some issue is mixing LINE INPUT # and INPUT #.  WRITE # may not be putting new lines when data is sent to the file, where PRINT # will have this behaviour.

For instance, if I do the following:

OPEN "test.txt" FOR OUTPUT AS #1
WRITE #1, "first"
WRITE #1, "second"
CLOSE #1

test.txt may contain something like (I'm just going from memory right now while I can't test your code, so this may not be 100% accurate):
"first" "second"

But, if I replace WRITE # with PRINT #, my code would be:

OPEN "test.txt" FOR OUTPUT AS #1
PRINT #1, "first"
PRINT #1, "second"
CLOSE #1

My test.txt file should look similar to:
first
second

So what I think is probably happening for you is LINE INPUT # is actually grabbing more than just one field of data.

Try changing from:

1200 LINE INPUT #1, nopeng$
LINE INPUT #1, nama$
INPUT #1, berat

To:

1200 INPUT #1, nopeng$
INPUT #1, nama$
INPUT #1, berat

I suspect this is your issue, but you also didn't indicate where QBasic was actually throwing the error about reaching the end of file.

Let me know if that fixes your problem, and if not, please indicate which part of the code is causing the error, and what data you inputted to the program to make it error.

I hope that helps,
-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.