09-13-2003, 12:39 PM
I got a wee bit carried away, so I'll make this into a mini project, but here is build 0.1.0a of "Unsequential File Record Deleter"
Better yet, you can download it (with test files and the config.dat file here.
Please tell me of any errors/problems you have with it. It is alpha, so I am aware of the problems with displaying large files, and some other small bugs.
Eric: I hope it helps you. You can take the editfile sub and use that, and it'll work just fine (the rest of the stuff is pretty stuff ). If you need a hand with explaining the code, just ask in this thread.
Code:
'/*****************************************/
'/* Unstructured File Record Deleter */
'/* */
'/* Author: Oracle (webmaster@qbnz.com) */
'/* Version: 0.1.0a */
'/* Date: 2003/09/13 */
'/* */
'/* This program will let you view an */
'/* unstructured file (ie. not random */
'/* access) and delete any record from */
'/* within it, regardless of location. */
'/* */
'/* Files included: */
'/* FILE.BAS (source code) */
'/* FILE.EXE (executable administration) */
'/* CONFIG.DAT (configuration data file) */
'/* */
'/* This program is released under the */
'/* GPL. This program is provided As Is, */
'/* and as such the author takes no */
'/* responsibility for any damage that */
'/* occurs to your possessions by using */
'/* this program. */
'/* */
'/* It is recommended that you back up */
'/* the files you wish to use this */
'/* program with before using it. */
'/*****************************************/
DEFINT A-Z
' --* Declare Subroutines *--
DECLARE SUB center (Text$, row%, fore%, back%)
DECLARE SUB configure ()
DECLARE SUB drawbackground (background$)
DECLARE SUB drawwindow (YPos, XPos, YSize, XSize, Caption AS STRING, TextLine)
DECLARE SUB editfile ()
DECLARE SUB init ()
DECLARE SUB quit ()
' --* Set Constants *--
CONST LFN$ = "Unstructured File Editor"
CONST SFN$ = "UFE"
CONST TITLE$ = " þ "
background$ = STRING$(80, "²")
DIM SHARED path$
path$ = "C:\qbasic\programs\file\"
init
OPTION BASE 1
SCREEN 0, , 2, 0
CLS
drawbackground background$
COLOR 0, 7
center TITLE$ + LFN + TITLE$, 1, 0, 7
drawwindow 3, 10, 19, 60, "Main Menu", 11
LOCATE 5, 13: PRINT "Welcome! Use the arrow keys to select the item you wish "
LOCATE 6, 13: PRINT "to edit, and then [ENTER] or [SPACE] to confirm that "
LOCATE 7, 13: PRINT "selection. "
COLOR 15, 1
sel = 1
SCREEN 0, , 1, 0
DO
SELECT CASE key$
CASE CHR$(0) + "H": sel = sel - 1
CASE CHR$(0) + "P": sel = sel + 1
CASE CHR$(27): sel = 3
CASE CHR$(13), CHR$(32)
SELECT CASE sel
CASE 1
editfile
CASE 2
configure
CASE 3
quit
END SELECT
SCREEN 0, , 1, 0
END SELECT
IF sel > 3 THEN sel = 1
IF sel < 1 THEN sel = 3
PCOPY 2, 1
IF sel = 1 THEN COLOR 15, 7 ELSE COLOR 15, 1
LOCATE 16, 13: PRINT " View/remove record from a file"
IF sel = 2 THEN COLOR 15, 7 ELSE COLOR 15, 1
LOCATE 17, 13: PRINT " Configure " + SFN$
IF sel = 3 THEN COLOR 15, 7 ELSE COLOR 15, 1
LOCATE 18, 13: PRINT " Exit " + SFN$
PCOPY 1, 0
DO
key$ = UCASE$(INKEY$)
LOOP UNTIL LEN(key$)
LOOP
DEFSNG A-Z
SUB center (Text$, row%, fore%, back%)
'/*****************************************/
'/* This subroutine centers text at a */
'/* location on the screen with a certain*/
'/* foreground and background colour. */
'/*****************************************/
LOCATE row%, 40 - (LEN(Text$) \ 2 + .5)
COLOR fore%, back%
PRINT SPACE$(row%) + Text$ + SPACE$(80 - row% - LEN(Text$))
END SUB
DEFINT A-Z
SUB configure
'/*****************************************/
'/* This subroutine configures FILE. */
'/*****************************************/
drawwindow 8, 27, 12, 26, "Configure FILE", 6
LOCATE 10, 30: PRINT "Here you can configure"
LOCATE 11, 30: PRINT SFN$ + ". This feature is"
LOCATE 12, 30: PRINT "not working yet."
PCOPY 1, 3
sel = 1
SCREEN 0, , 3, 0
DO
SELECT CASE key$
CASE CHR$(0) + "H": sel = sel - 1
CASE CHR$(0) + "P": sel = sel + 1
CASE CHR$(27): EXIT SUB
CASE CHR$(13), CHR$(32)
SELECT CASE sel
CASE 1
LOCATE 19, 34: COLOR 4, 1: PRINT "Not Available"
CASE 2
EXIT DO
END SELECT
END SELECT
IF sel > 2 THEN sel = 1
IF sel < 1 THEN sel = 2
IF sel = 1 THEN COLOR 15, 7 ELSE COLOR 15, 1
LOCATE 16, 30: PRINT "Enable password: NO"
IF sel = 2 THEN COLOR 15, 7 ELSE COLOR 15, 1
LOCATE 17, 30: PRINT "Exit"
PCOPY 3, 0
DO
key$ = UCASE$(INKEY$)
LOOP UNTIL LEN(key$)
LOOP
END SUB
DEFSNG A-Z
SUB drawbackground (back$)
'/*****************************************/
'/* This subroutine draws the background */
'/* for the main menu. Pilfered from */
'/* Lithium's DS4QB++ setup program. */
'/*****************************************/
COLOR 7, 8
FOR i = 1 TO 25
LOCATE i, 1
PRINT back$
NEXT i
END SUB
DEFINT A-Z
SUB drawwindow (YPos, XPos, YSize, XSize, Caption AS STRING, TextLine)
Text$ = " þ " + Caption + " þ "
CPos = (XSize \ 2) - (LEN(Text$) \ 2)
COLOR 15, 1: LOCATE YPos, XPos: PRINT "Ã" + STRING$(CPos, "Ã") + Text$ + STRING$(XSize - CPos - LEN(Text$), "Ã");
COLOR 7, 1: PRINT "¿"
FOR Y = YPos + 1 TO YPos + YSize
COLOR 15, 1: LOCATE Y, XPos: PRINT "³" + SPACE$(XSize);
COLOR 7, 1: PRINT "³";
NEXT Y
COLOR 15, 1
LOCATE Y, XPos: PRINT "Ã";
COLOR 7, 1: PRINT STRING$(XSize, "Ã") + "Ã";
COLOR 15, 1: LOCATE YPos + TextLine, XPos + 2: PRINT STRING$(XSize - 2, "Ã");
COLOR 8, 0:
FOR X = XPos + 1 TO XPos + XSize + 2
LOCATE YPos + YSize + 2, X: PRINT CHR$(SCREEN(YPos + YSize + 2, X));
NEXT
FOR Y = YPos + 1 TO YPos + YSize + 1
LOCATE Y, XPos + XSize + 2: PRINT CHR$(SCREEN(Y, XPos + XSize + 2));
NEXT
COLOR 7, 1
END SUB
SUB editfile
SCREEN 0, , 0, 0: CLS
INPUT "File name? ", filename$
IF RIGHT$(filename$, 4) <> ".txt" THEN filename$ = filename$ + ".txt"
handle = FREEFILE
OPEN path$ + filename$ FOR INPUT AS handle
DO UNTIL EOF(handle%)
INPUT #handle, record$
i = i + 1
wholefile$ = wholefile$ + CHR$(13) + CHR$(10) + record$
LOOP
k = i
PRINT "File " + filename$ + "'s contents:"
PRINT : PRINT wholefile$
CLOSE #handle
PRINT "Choose a record to be deleted, or ESC to exit"
INPUT "", key$
IF key$ = CHR$(27) THEN EXIT SUB
ky = VAL(key$)
'count = 1
FOR i = 1 TO LEN(wholefile$)
check$ = MID$(wholefile$, i, 2)
IF check$ = CHR$(13) + CHR$(10) THEN
count = count + 1
IF count = ky THEN
FOR j = i + 2 TO LEN(wholefile$)
check$ = MID$(wholefile$, j, 2)
IF check$ = CHR$(13) + CHR$(10) THEN
IF count <> k THEN
wholefile$ = LEFT$(wholefile$, i - 1) + RIGHT$(wholefile$, LEN(wholefile$) - j + 1)
ELSE
wholefile$ = LEFT$(wholefile$, i - 1)
END IF
true = -1
EXIT FOR
END IF
NEXT j
END IF
END IF
IF true THEN
wholefile$ = RIGHT$(wholefile$, LEN(wholefile$) - 2)
handle = FREEFILE
OPEN path$ + filename$ FOR OUTPUT AS #handle
PRINT #handle, wholefile$
CLOSE #handle
EXIT FOR
END IF
NEXT i
CLS
PRINT "Altered " + filename$
PRINT : PRINT wholefile$
WHILE INKEY$ <> "": WEND
WHILE INKEY$ = "": WEND
END SUB
DEFSNG A-Z
SUB init
'/*****************************************/
'/* This subroutine initialises the */
'/* program, by reading in configuration */
'/* data. */
'/*****************************************/
handle% = FREEFILE
'OPEN "config.dat" FOR INPUT AS #handle%
' Input stuff
CLOSE #handle%
END SUB
DEFINT A-Z
SUB quit
SCREEN 0, , 0, 0
CLS
END
END SUB
Better yet, you can download it (with test files and the config.dat file here.
Please tell me of any errors/problems you have with it. It is alpha, so I am aware of the problems with displaying large files, and some other small bugs.
Eric: I hope it helps you. You can take the editfile sub and use that, and it'll work just fine (the rest of the stuff is pretty stuff ). If you need a hand with explaining the code, just ask in this thread.