Qbasicnews.com

Full Version: How can QBasic write to certain lines of a file?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How can qbasic write to a certain line of a file that already has text in it, without using the command APPEND to add on to the end of the file, or use the command OUTPUT to overwrite the file entirely? Thanks!
cannot do using intrinsic QB
best way i know is to open the file, read the contents into an array, shift stuff down and insert, then reoutput

but you nerds can think of better.
that's if you want to insert. He said replace. But it's the same thing w/o shifting.
Although you'd prolly have to modify a lot of code if this is for an existing project, you could take a look at opening files for random-access. Then you can declare a record-length and input/output to any part of the file you want.

see "OPEN filename FOR RANDOM AS #1 LEN = recordlength"

*peace*

Meg.
alternatively you open it so you modify in binary, which allows you to do anything to the file. ALthough for this you may need to write some complicated routine. With ascii text files look at the ascii character codes, every two bits is a ascii code in the file.
Quote:How can qbasic write to a certain line of a file that already has text in it, without using the command APPEND to add on to the end of the file, or use the command OUTPUT to overwrite the file entirely? Thanks!
If you don't have experience with Random or Binary files, and you're not ready to learn now, then you're going to have to:
* Open your input file and open an output file to contain the updated file.
* Read records form the input file, writing them to the output file until you get to the record to be replaced.
* Write the record being replaced.
* Read and write the rest of the records.

Unless you have a very large input file, say more than 10,000 records, or your records are very large, you won't hardly notice the time to do this unless you have a very old, slow machine. Try it.

Keep It Simple.
*****
Quote:How can qbasic write to a certain line of a file that already has text in it, without using the command APPEND to add on to the end of the file, or use the command OUTPUT to overwrite the file entirely? Thanks!


Maybe this will work:

(This will be my first time ever offering help, rather than asking Big Grin)

Code:
INPUT "which line do you want to replace?  choose a number from 1 to 5"; A

'which line of the 5 lines do you want to change?  maybe line 4?
'These are the lines in the text file.  Create a text file
'and make 5 lines like so:  then name it whatever.txt
'12
'23
'45
'56
'98

INPUT "what number do you want to replace it with"; B

'now choose what number you want to have replace what's printed
'on that particular line you chose.

DIM C(1 TO 5)

OPEN "whatever.txt" FOR INPUT AS #1

'C(1 to 5) contains the numbers in the text file

FOR D = 1 TO 5
  INPUT #1, C(D)
  E = E + 1   'E increases 1 each time through the loop.
  IF E = A THEN  'If E equals the line number you chose.....
    C(D) = B  'C(D) changes to the value you chose.
  END IF
NEXT D

CLOSE #1

OPEN "whatever.txt" FOR OUTPUT AS #1

'now everything is printed out to the same text file.

FOR D = 1 TO 5
  PRINT #1, C(D)
NEXT D

CLOSE #1
Thanks guys! I'll check this stuff out..

Actually, what I really wanted to do was just take all the lines in one file, assemble them into one line Big Grin
Quote:Thanks guys! I'll check this stuff out..

Actually, what I really wanted to do was just take all the lines in one file, assemble them into one line Big Grin

Like so: ?

CLS

INPUT "how many lines are we talking here"; A

OPEN "whatever.txt" FOR INPUT AS #1

DIM C(1 TO A)

'load all the lines into memory:

FOR B = 1 TO A
INPUT #1, C(B)
NEXT B

CLOSE #1

OPEN "another.txt" FOR OUTPUT AS #1

'print all the lines back in less lines thanks to the semi-colon ;

FOR B = 1 TO A
PRINT #1, C(B);
NEXT B

CLOSE #1
Quote:Thanks guys! I'll check this stuff out..

Actually, what I really wanted to do was just take all the lines in one file, assemble them into one line Big Grin
Your original request and what you "really want" are totally different. Thanks for sending us on a wild goose chase. :wink:
*****
Pages: 1 2