Qbasicnews.com

Full Version: Noob Help !! How can i get QB to insert a line into a file?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Noob ALERT lol!! How can i get QB to insert a line into a file without destroying existing data.

I have tried messing round with the open "filename.ext" for append/input/output as #1 sort of thing but i can't get it to change lines Sad lol


_-_ - also, is there a way of inserting a whole text file onto the end of another text file without doing each line?? Big Grin Thx
append adds to the end if you want to change you would have to input all the lines to an array or soemthing then print the lines up to the insert. then you insert then the rest of the lines


Code:
OPEN file FOR INPUT AS #1
INPUT #1, to array
FOR i = 1 TO maxlines
IF i = 5 THEN PRINT #1, "Blarg"
PRINT #1, array$(i)
NEXT

that would insert Blarg at line 5. except it doesn't work becuase I'm lazy.
Quote:Noob ALERT lol!! How can i get QB to insert a line into a file without destroying existing data.

I have tried messing round with the open "filename.ext" for append/input/output as #1 sort of thing but i can't get it to change lines Sad lol

_-_ - also, is there a way of inserting a whole text file onto the end of another text file without doing each line?? Big Grin Thx

Question #1: "insert a line into a file". Do you want to insert this new line in a particular place; i.e., after some given record in the file, or do you want to insert the new line at the end of the file. There's a big difference. To insert in a particular place you must read and write each record (to a new file) until the point where the new record goes, and then read and write the rest of the records. To insert at the end you just open for append and then write new record. SO, WHICH DO YOU WANT TO DO? I'll help you with the code.

QUESTION #2: "... can't get it to change lines." ARE WE CHANGING LINES OR INSERTING LINES --- BIG DIFFERENCE.

QUESTION #3: ... inserting whole test file onto the end of another". You can do this from the command-line:
COPY FILEA+FILENEW/B FILEB

FILEB now has the combined files; i.e., the concatenation of FILENEW onto the end of FILEA.
Let me know if you want to do this from within your QB program.
*****
Well, we could use the wasier method - binary file Tongue (i luv them)

Code:
OPEN "File.ext" FOR BINARY AS #1

''Get the number of indexes in the file...
idx% = 0
DO UNTIL EOF(1)
  GET #1,,temp$
  PRINT temp$; " ";
  idx% = idx% + 1
LOOP

''idx% now is the number of indexes in the file

''Create an array taht can hold each index of data
DIM FileContents(0 to idx%) AS STRING

'Reset the file position...
CLOSE #1
OPEN "File.ext" FOR BINARY AS #1

''Print info, and ask what to add, and where to add it
PRINT idx%;" index(s) in file"
INPUT "Insert Index: ", eidx%
INPUT "Data to Insert: ", info$

''Just make sure its in range... more than 0, and less than the max
IF eidx% > idx% or eidx% < 0 THEN
  PRINT "Insert Index is invalid!"
  END
END IF

''Put the stuff in the array
FOR i% = 0 TO idx%
  GET #1,,temp$
  FileContents(i%) = temp$
NEXT i%

''Start puttin stuff in the file until we get to the index you want to add stuff in...
FOR i% = 1 to eidx%
  PUT #1,, FileContents(i%)
NEXT i%

''Put in the addition
PUT #1,,info$


''Resume copying the file into itself
FOR i% = eidx% TO idx%
  PUT #1,, FileContents(i%)
NEXT i%

CLOSE #1

END

This code is untested, and not commented really well - sorry, ran out of time..

Oz~
Hello all, thx for the fast responses! Wow - where do i start! Rite - REMEMBER I'M KINDA NEW TO QB - so go easy on me please Big Grin

I want to be able to insert a line into a file. Eg.

Line1
Line2
Line3
Line4
Other info1
Other info2

Now what i want to do is insert a 'Line5' beneath 'line4' without overwriting 'Other info1'.

Also I would like to be able to 'Change Lines' that i can write to but that will be for another day....

And yes I would like to be able to combine files in a QB program, if its less complex than writing it into a batch file!!!!

Again, thanks
I'll comment mine right now

Oz~
Lost_jedi,
I find some questionable code in OZ's solution. In my opinion the DIM of the Filecontents array should be (1 to idx%), and thereafter all FOR loops should go from 1 to whatever.

The reason for the above is that there is no ZEROETH record (or index) on the file, plus the user is going to specify which record to write after with a number starting from 1. (This is my interpretation of Oz's code.)

At the end of this program we have:
''Resume copying the file into itself
FOR i% = eidx% TO idx%

We already wrote the eidx% record out before, so this should be:
FOR i% = eidx%+1 TO idx%

For the case when we're appending a record to a file, the program should check before attempting to write out any remaining records which don't exist.

I haven't tested OZ's program but it seems to be the beginning of a good, general purpose program to insert records into a file. Of course, you can't insert a record at the front of the file, but then again that was not in the specifications.

I personally would approach this program in a different manner.
1) I would read the existing file and write to a new file; that is, one input file and one output file.
2) Doing it this way I would not have to store the data records into an array, which for large files would not work.
3) I would not use OPEN FOR BINARY, just plain open.

WHICH WAY DO YOU PREFER, OZ'S WAY OR MINE? If you prefer my approach, I'll post some code.
*****
I would go for moneo's.

What I do is opening a file for input and a file for output. You copy lines from input file into the output file. When you pass the line "Line4", you print "line5" to output file. Then you keep copying.
Well - i'm spoilt for choice Big Grin

however u ppl are WAYYYY over my head.... hence the title lol

the file i will be editing is quite big so it wuld be better if it was not opened into arrays.

i have an array with the data i want, so all i want to do is insert the data into into specific places,

which eva u ppl think is the best.... :???: moneo - post away!

thx again
[syntax="QBASIC"]DECLARE SUB insert (file$, text$, linenum!, replace!, before!)
CALL insert("Blarg.txt", "Moo", 5, 1, 0)

SUB insert (file$, text$, linenum, replace, before)
OPEN "blarg.txt" FOR INPUT AS #1
DO
LINE INPUT #1, a$
a = a + 1
LOOP UNTIL EOF(1)
DIM file$(a)
SEEK 1, 1
FOR i = 1 TO a
LINE INPUT #1, file$(i)
NEXT i
CLOSE
OPEN file$ FOR OUTPUT AS #1
FOR i = 1 TO a
IF i = linenum AND before = 1 THEN PRINT #1, text$
IF i <> linenum OR replace = 0 THEN PRINT #1, file$(i)
IF i = linenum AND before = 0 THEN PRINT #1, text$
NEXT i
CLOSE

END SUB[/syntax]



EDIT: Woot @ Nath 5600 =P
Pages: 1 2 3