Edit .txt

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
semiex
Newbie
Posts:2
Joined:Fri Feb 06, 2004 8:41 am
Edit .txt

Post by semiex » Fri Feb 06, 2004 9:34 am

I would like to edit a .txt file using Qbasic,
The .txt file was created via another program as a .db file and then opened via wordpad and saved as .txt file
I have replaced the header in the text via

post$= chr$(0)
OPEN "C:\"+filename+".txt" for Binary as #6
for count = 1 to 2057
Put #6,count,post$
Close #6

(1) I would like to count the number of Chr$(41) within the rest of the file.
(2) Replace Chr$(41) with Chr$(13) within the rest of the file.

I cannot use command "GET$ LINE INPUT" as there is no return chr$ within the text and all is loaded

"INPUT,num,varib" not suitable for application

:)
































































 

Artie
Jr. Member
Posts:23
Joined:Fri Feb 06, 2004 12:48 am
Location:Florida
Contact:

Re: Edit .txt

Post by Artie » Fri Feb 06, 2004 9:30 pm

The following worked on my machine.
Be sure not to leave out the line:

a$ = STRING$(1, 32)

That line establishes a$ as being one character long, so that the GET statement that comes later will work properly.


CLS
file$ = "C:\" + filename$ + ".TXT"
a$ = STRING$(1, 32)
post$ = CHR$(0)
test$ = CHR$(41)
replace$ = CHR$(13)
OPEN file$ FOR BINARY AS #6
 length = LOF(6)
 FOR x = 1 TO 2057
   PUT #6, x, post$
 NEXT x
 FOR x = 2058 TO length
   GET #6, x, a$
   IF a$ = test$ THEN PUT #6, x, replace$
 NEXT x
CLOSE #6
END


BTW - 2057 characters is a heck of a header. I hope you have that number correct.  Also, CHR$(41) is the paranthesis key.  ")"  I hope thats what you meant also.

Artie

Artie
Jr. Member
Posts:23
Joined:Fri Feb 06, 2004 12:48 am
Location:Florida
Contact:

Re: Edit .txt

Post by Artie » Fri Feb 06, 2004 10:25 pm

BTW - If its not absolutely neccessary to keep 2057 zero's at the beginning of the file, you can eliminate them with this little code variation:

CLS
file1$ = "C:\" + filename$ + ".txt"
file2$ = "C:\" + filename$ + ".bak"
header = 2057
a$ = STRING$(1, 32)
test$ = CHR$(41)
replace$ = CHR$(13)
OPEN file1$ FOR BINARY AS #6
OPEN file2$ FOR BINARY AS #7
 length = LOF(6)
 FOR x = (header + 1) TO length
   y = x - header
   GET #6, x, a$
   IF a$ = test$ THEN PUT #7, y, replace$ ELSE PUT #7, y, a$
 NEXT x
CLOSE
END

This will keep the old file intact, and create a new one with the extension .BAK.  (filename.bak)

Artie

semiex
Newbie
Posts:2
Joined:Fri Feb 06, 2004 8:41 am

Re: Edit .txt

Post by semiex » Sat Feb 07, 2004 3:27 am

Thank You Artie
I will try both those surgestions later today
actually 2059 is the beginning of the text i wont to work with and i had to get rid of header and find out where the start was, the final outcome will be a .dbf file with headers replaced with my choosing, the parenthesis 'CHR$(41)' is the only common thing in the text that is seperating the information i wont (and the length of the string between the CHR$(41)is of a set length :D

Post Reply