Qbasicnews.com
April 09, 2020, 06:50:10 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: input past end of file  (Read 6311 times)
andy
Senior Member
**
Posts: 175



WWW
« on: January 03, 2004, 07:37:00 PM »

I use input #1,a$ to input a line of text from a file, I do this until I get to the end. I then close the file.
Later I open another file, but when I try to input it gives the error input past end of the file. Even though the file has plenty of lines.
Any ideas why and how it could be fixed.
Logged

eminiscing about trapezoids in conjunction with stratospherical parabolas:

No questions asked.

www.stickskate.com
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #1 on: January 03, 2004, 07:57:45 PM »

Code and what the file contains, please? If you haven't used CLOSE and you've written those lines within the program, it might be buffering.
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #2 on: January 03, 2004, 11:35:41 PM »

Yeah, it's hard to guess at your problem without seeing the actual code. Please post it. It's probably something very simple.
*****
Logged
dadsherm
Member
*
Posts: 75


« Reply #3 on: January 03, 2004, 11:36:21 PM »

I agree with zack. A litttle more info. would help. It sounds like though, without actually seeing what you're trying to do, that you are trying to read in X amount of data each time, but the second file isn't as large as the first so you end up with the "input past end" error message. If you don't know the length of the file, you can always check for the eof character (if it's sequential(sp?)), but I'm just guessing at this point.
Logged

adsherm
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #4 on: January 04, 2004, 12:46:29 AM »

I can tell you something that has plagued a few programmers, when it comes to opening files as BINARY:
If you have a file, file #1, let's call it, opened via the BINARY file mode, and then in one part of your program you write some stuff to #1 using PUT, and then a little later (before you CLOSE #1) you try to read that stuff using GET, it might not be there. Why? Buffering. Because harddrives are relatively slow, QB waits until it gets a worthwhile amount of data to write to the file (or you CLOSE the file. That will automatically write the data). So after you PUT something, it might not really be there.
The answer? CLOSE the file and then reopen it.
This applies with VB, anyway, and I'm pretty sure it's the same with QB.
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Neo
Na_th_an
*****
Posts: 2150



« Reply #5 on: January 04, 2004, 02:23:28 PM »

andy, check for points where your program reads more data than is present in the file. E.g. read data in a loop like this:
Code:
WHILE NOT EOF(1)
   LINE INPUT #1, a$
WEND

And of course, don't forget to close your file before reopening it Wink

Zack: the error read past end of file doesn't occur on Binary mode, it just gets Nulls after the end (there is a limit though).

EDIT: Btw, 1000th post!!!
Logged
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #6 on: January 04, 2004, 02:41:58 PM »

Quote from: "Neo"
Zack: the error read past end of file doesn't occur on Binary mode, it just gets Nulls after the end (there is a limit though).

Really? You learn something new every day. Smiley
Meanwhile, I have some guy trying to tell me what DEFINT does...you'd think he'd take a hint, after I showed him my recursive Fibonacci-finder in C++. :roll:
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
andy
Senior Member
**
Posts: 175



WWW
« Reply #7 on: January 05, 2004, 01:12:20 PM »

OK first I am closing the file, I am opening it for input, I am using a loop statement, to input from the file, and use an on error goro statement to stop reading past the end of a file. The code is very messy, I have not had chance to tidy it up yet, here it is. Oh but first it is a sort of touch typing program, it opens a file and you have to enter the line letter by letter.

Code:

DECLARE SUB menu (file$)
DECLARE SUB errorreport (code, info)
CLS
CHDIR "data"
DIM text$(100)
DIM textarray$(81)
1
position = 1
wrong$ = "o2l8c"

menu file$
PRINT file$
OPEN file$ FOR INPUT AS #1
GOSUB gettext
10
CLOSE #1

FOR currentline = 1 TO lines
 
  position = 1
  length = LEN(text$(currentline))
 
  LOCATE 13
  PRINT "line: "; currentline; " of "; lines
  LOCATE 14
  PRINT "Line length = "; length
  GOSUB update

  LOCATE 1

  FOR i = 1 TO length
    textarray$(i) = MID$(text$(currentline), i, 1)
  NEXT

  PRINT text$(currentline)

  i = 1
  DO
20
    GOSUB nextchar
    DO
      khb$ = INKEY$
    LOOP UNTIL khb$ <> ""
    IF khb$ = CHR$(27) THEN CLS : GOSUB startover
    IF khb$ <> textarray$(i) THEN
      PLAY wrong$
      incorrect = incorrect + 1
      GOSUB update
      GOTO 20
    END IF
   
    correct = correct + 1
    GOSUB update

    LOCATE 2, position
    IF textarray$(i) = " " THEN
      PRINT "_";
    ELSE
      PRINT textarray$(i);
    END IF
    position = position + 1
    i = i + 1
  LOOP UNTIL i > length
30
  tag = 1
  GOSUB nextchar
  tag = 0
  DO
    khb$ = INKEY$
  LOOP UNTIL khb$ <> ""
  IF khb$ <> CHR$(13) THEN
    PLAY wrong$
    incorrect = incorrect + 1
    GOSUB update
    GOTO 30
  END IF
 
  correct = correct + 1
  GOSUB update

  CLS

NEXT currentline

GOSUB update


PRINT "Press esc to exit or any other key to choose anoher exercise"

DO
khb$ = INKEY$
LOOP UNTIL khb$ <> ""

IF khb$ <> CHR$(27) THEN
  GOSUB startover
END IF

END

gettext:
lines = 0
ON ERROR GOTO 1000
FOR a = 1 TO 100
  INPUT #1, text$(a)
  IF text$(a) <> "end" THEN
    lines = lines + 1
    IF text$(a) = "" THEN
      a = a - 1
      lines = lines - 1
    END IF
  ELSE
    GOTO 1000
  END IF
NEXT
errorreport 2, 0
1000

ON ERROR GOTO 2000

FOR a = 1 TO lines
  length = LEN(text$(a))
  IF length > 80 THEN
    errorreport 1, a
  END IF
NEXT
GOTO 10
RETURN

update:
LOCATE 15, 1
PRINT "                                                                                                                           "
LOCATE 15, 1
IF correct + incorrect = 0 THEN
  PRINT "You have made: "; incorrect; " errors. That is N/A percent correct."
ELSE
  PRINT "You have made: "; incorrect; " errors. That is "; INT((correct / (correct + incorrect)) * 100); " percent correct."
END IF
RETURN

nextchar:
LOCATE 16, 1
PRINT "                                                    "
LOCATE 16, 1
IF textarray$(i) = " " THEN
  PRINT "Next character: space"
ELSEIF tag = 1 THEN
  PRINT "Next character: return"
ELSE
  PRINT "Next character: "; textarray$(i)
END IF

RETURN

startover:
CLS
FOR a = 1 TO 100
  text$(a) = ""
  IF a < 82 THEN textarray$(a) = ""
NEXT
a = 0
i = 0
GOTO 1
RETURN

2000
PRINT "there was an error"
END

SUB errorreport (code, info)

SELECT CASE code
  CASE 1
    PRINT "Line: "; info; " Is greater than 80 characters. Please edit the line."
   
  CASE 2
    PRINT "File length exceeds 100 lines. Please alter the file length."
 
END SELECT
END
END SUB

SUB menu (file$)
LOCATE 1, 1: PRINT "Exercise1  (a)"
LOCATE 2, 1: PRINT "Exercise2  (b)"
LOCATE 3, 1: PRINT "Exercise3  (c)"
LOCATE 4, 1: PRINT "Exercise4  (d)"
LOCATE 5, 1: PRINT "Exercise5  (e)"
LOCATE 6, 1: PRINT "Exercise6  (f)"
LOCATE 7, 1: PRINT "Exercise7  (g)"
LOCATE 8, 1: PRINT "Exercise8  (h)"
LOCATE 9, 1: PRINT "Exercise9  (i)"
LOCATE 10, 1: PRINT "Exercise10 (j)"
LOCATE 11, 1: PRINT "Exercise11 (k)"
LOCATE 12, 1: PRINT "Exercise12 (l)"
LOCATE 13, 1: PRINT "Exercise13 (m)"
LOCATE 14, 1: PRINT "Exercise14 (n)"
LOCATE 15, 1: PRINT "Exercise15 (o)"


LOCATE 17: INPUT "Enter your choice: ", choice$
PRINT choice$

IF choice$ = "a" THEN file$ = "exer1.txt"
IF choice$ = "b" THEN file$ = "exer2.txt"
IF choice$ = "c" THEN file$ = "exer3.txt"
IF choice$ = "d" THEN file$ = "exer4.txt"
IF choice$ = "e" THEN file$ = "exer5.txt"
IF choice$ = "f" THEN file$ = "exer6.txt"
IF choice$ = "g" THEN file$ = "exer7.txt"
IF choice$ = "h" THEN file$ = "exer8.txt"
IF choice$ = "i" THEN file$ = "exer9.txt"
IF choice$ = "j" THEN file$ = "exer10.txt"
IF choice$ = "k" THEN file$ = "exer11.txt"
IF choice$ = "l" THEN file$ = "exer12.txt"
IF choice$ = "m" THEN file$ = "exer13.txt"
IF choice$ = "n" THEN file$ = "exer14.txt"
IF choice$ = "o" THEN file$ = "exer15.txt"

CLS
END SUB



Um small problem, it is not opening properly in notepad or word
sorry, when I wor out how to copy the text I will post it up
Logged

eminiscing about trapezoids in conjunction with stratospherical parabolas:

No questions asked.

www.stickskate.com
Mango
Wandering Guru
***
Posts: 360



« Reply #8 on: January 05, 2004, 01:22:44 PM »

Quote from: "andy"

Code:

  Q$ cd ..............
.........
.........
m o"] m
 exer15.txt     s   Q  '    


Um small problem, it is not opening properly in notepad or word
sorry, when I wor out how to copy the text I will post it up


Well...at least this forum allows you to EDIT your posts ;-)

You need to open your prog in QB, do a save-as, and select the ascii option.  Then your code will be plain-text.  THen...open the file in notepad and paste the text into your previous post...login first and open it in EDIT mode...

CHeers
Logged
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #9 on: January 05, 2004, 02:02:50 PM »

(note that the IDE says "Save as Text", not "Save as ASCII", as Mango implies)
But yeah, do that.
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
andy
Senior Member
**
Posts: 175



WWW
« Reply #10 on: January 06, 2004, 06:21:27 PM »

Okay, it is podted up now.
Sorry about the previouse code, I'm used to v1.1, it doesn't have that option.
Logged

eminiscing about trapezoids in conjunction with stratospherical parabolas:

No questions asked.

www.stickskate.com
andy
Senior Member
**
Posts: 175



WWW
« Reply #11 on: January 08, 2004, 10:25:54 AM »

Also, the chdir "\data", works the first time you run, but after that it brings up an error, 2invalid dirctory" I guess this is because it changes the directory permenantly, while qb is open, is there any way to stop this, like check if the current directory is the data directory or not?
Logged

eminiscing about trapezoids in conjunction with stratospherical parabolas:

No questions asked.

www.stickskate.com
Meg
Ancient QBer
****
Posts: 483


« Reply #12 on: January 08, 2004, 07:24:44 PM »

just replace

Code:
CHDIR "data"


with

Code:
CHDIR "c:\data"


or whatever your drive is, if not C:

*peace*

Meg.
Logged
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #13 on: January 09, 2004, 07:03:27 AM »

If you wanna get rid of problems, don't *ever* use absolute paths. Use relative paths instead: http://faq.qbasicnews.com/?blast=RelativePaths
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
andy
Senior Member
**
Posts: 175



WWW
« Reply #14 on: January 10, 2004, 03:10:03 PM »

if i use chdir "c:\data =" it will only work if you have the data file on the c:\ and not in any directories?
Logged

eminiscing about trapezoids in conjunction with stratospherical parabolas:

No questions asked.

www.stickskate.com
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!