Qbasicnews.com

Full Version: little help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
i need to know if there is a way to search a text file for a line of text and replace that line with a nother
IF q$ = "Qbasic" THEN PRINT "yes" ELSE PRINT YES
so the answers yes. do you want to know how? if yes i be back!

edit: iam back!

Code:
CLS
s$ = "sdhiamcolfgagisfagadfiamcooldsfasdf"
look$ = "iamcool"
s = 1
FOR i = 1 TO LEN(s$)
IF MID$(s$, i, 1) = MID$(look$, s, 1) THEN s = s + 1 ELSE s = 1
IF s = LEN(look$) THEN PRINT "found": END
NEXT i
PRINT "not found"
Quote:i need to know if there is a way to search a text file for a line of text and replace that line with a nother
Yes, I have a utility program to do this.
However, if the search string appears in more than one record (line), it will also replace it. The search string does not have to match an entire line, it can be part of a line. If you want me to send you the utility, send me a PM with your email address.

If you want to do it yourself, and assuming you want to search for the text of an entire line (like you said), then these are the basic steps:
1) Define the search string, example: SER$="what to search for"
2) Define the replacement string, ex: REP$="replace with this"
2) Open the original file for input as #1.
3) Open the new file for output as #2.
4) Read the input file in a DO loop looking for SER$ .... example:
LINE INPUT #1,TXT$
IF TXT$ = SER$ THEN PRINT #2,REP$:SYSTEM ELSE PRINT #2,TXT$
LOOP

That's it. If you want to replace only part of a line, then it takes a few more lines of code to get the front part, insert replacement, get the trailing part, then write.
*****
Haven't you heard of INSTR, whitetiger?

Code:
look$ = "helloworld"
strngtosearch$ = "abcdefghelloworldqrstuvwxyz"
IF INSTR(strngtosearch$, look$) THEN PRINT "Found"

INSTR returns a nonzero if the string is found, the nonzero will be equal to the first character location where the string starts, eg in the example above the "helloworld" string starts at character 8 so INSTR returns 8.

To search a file, do what moneo says. My method allows you to replace part of a record in a file though, not just the whole line.
i am asumeing that i can use like it asked them a question and input "bla bla" awn

i could use awn in the replacement string

and yes i need to know how to just replace a word or 2
SOLANCE,
With the information I gave you in my above post, and the recommendation of Oracle for using an INSTR, you should be able to work it out for yourself. Try it, test it, post it, and we'll take a look.
Or would you like us to code the whole program for you?
*****
So, for example... (the following is a sample program output)

***
The old string is: abcdefghelloworldqrstuvwxyz
String to find? helloworld
Replace it with? hello world
The new string is: abcdefghello worldqrstuvwxyz
***

Is that what you want?
ok i think i got it it should go like this right?
Code:
SER$ = "dasjfdl;asfkj"
REP$ = awnser
open file for input as #1
open file for output as #2

EIDT:i have hit a delmia how would i go say
Ser$ = "what ever"
rep$ = something maps

maps is a var how would i include the var in the replacement
Quote:Haven't you heard of INSTR, whitetiger?

Code:
look$ = "helloworld"
strngtosearch$ = "abcdefghelloworldqrstuvwxyz"
IF INSTR(strngtosearch$, look$) THEN PRINT "Found"

INSTR returns a nonzero if the string is found, the nonzero will be equal to the first character location where the string starts, eg in the example above the "helloworld" string starts at character 8 so INSTR returns 8.

To search a file, do what moneo says. My method allows you to replace part of a record in a file though, not just the whole line.

i feel stupid
You want to add a variable onto the end of a string? Well...

Code:
answer$ = answer$ + rep$

If you want to add it in the middle, you have to use RIGHT$ and LEFT$, like:

Code:
answer$ = LEFT$(answer$, INSTR(answer$, rep$) - 1) + rep$ + RIGHT$(answer$, LEN(answer$) - LEN(LEFT$(answer$, INSTR(answer$, rep$) - 1)

whitetiger: Smile
Pages: 1 2