find and replace text

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

i think it works only for small files.
i tried it with a 723 KB big file and the compiler
gave me an error message.
My Operatingsystem is WINDOWS NT 4.0 SP 6a.

i would like to make decisions in this program.
when it finds 0128AGK to replace with 5555AFP
when it finds 0128ALK to replace with 7777AFP

Edited by - Ralf on 04 January 2002 12:43:47
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
i think it works only for small files.
i tried it with a 723 KB big file and the compiler
gave me an error message.
I just tested it now with a 3866 KB file (yes, 3.8 MB)
and it worked. I'm talking about the same source code
that I E-Mailed to you. If it's not working, then perhaps
your NT system is to blame. Have you turned on NT-compliancy
in the Compiler Options? And what version of PureBasic are
you using?

Here's my source again. Compile it to an exe, and stick the
text file into the same folder as the exe, and name it original.txt.
Then run the exe. It works. If it doesn't, please explain
to us the exact error message(s) that you receive.

Code: Select all

Procedure$ replace(string$,old$,new$)
  r=1 : pos=1
  While r0
    r=FindString(string$,old$,pos)
    If r0
      string$=Left(string$,r-1)+new$+Mid(string$,r+Len(old$),Len(string$))
      pos=pos+Len(new$)
    EndIf
  Wend
  ProcedureReturn string$
EndProcedure
;
If ReadFile(0,"original.txt")
  If CreateFile(1,"modified.txt")
    Repeat
      UseFile(0)
      s$=ReadString()
      s$=replace(s$,"0128AGK","5555AFP")
      s$=replace(s$,"0128ALK","7777AFP")
      UseFile(1)
      WriteStringN(s$)
    Until Eof(0)0
  EndIf
EndIf

PB - Registered PureBasic Coder


Edited by - PB on 04 January 2002 20:50:33
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

zzz works fine, but it still gives Compilerproblems when the files are bigger.
Maybe it is because i have WINDOWS NT 4.0 SP6a. But maybe one day it will be
fixed in PUREBASIC. I think it can be a great programming language if the development is keep going on.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
zzz works fine, but it still gives Compilerproblems when the files are bigger.
Maybe it is because i have WINDOWS NT 4.0 SP6a.
You still haven't told us what error message you're getting... we may be able
to help if we recognize it...


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

Hello PB and others,
i tried a file with 722 KB (740.097 Bytes)and it crashed.
The Errormessage is like this:
PBCOMPILER.EXE EXCEPTION: Zugriffsverletzung (0x0000005), Adresse: 0x77f7754b.
PureBasic969990.EXE EXCEPTION: Zugriffsverletzung (0x0000005), Adresse: 0x00401a6a

Another Error message i got was the same only other Adress on PUREBASIC990013.EXE Execption: Zugriffsverletzung (0x0000005), Adresse: 0x00401c52.

If you want i can mail you screenshots of it.

My SourceCode is like this:

Procedure$ replace(string$,old$,new$)
r=1 : pos=1
While r0
r=FindString(string$,old$,pos)
If r0
string$=Left(string$,r-1)+new$+Mid(string$,r+Len(old$),Len(string$))
pos=pos+Len(new$)
EndIf
Wend
ProcedureReturn string$
EndProcedure
;
If ReadFile(0,"D:\test\dta.txt")
If CreateFile(1,"D:\test\dta2.txt")

Repeat
UseFile(0)
s$=ReadString()
s$=replace(s$,"0128AGK6002010000000000","0128AGK6002010000000800")
UseFile(1)
WriteString(s$)
Until Eof(0)0
EndIf
EndIf
CloseFile(0)
CloseFile(1)

If ReadFile(2,"C:\DB95-EIN\L_95A")
If CreateFile(3,"C:\DB95-EIN\L_95I")
Repeat
UseFile (2)
s$=ReadString()
s$=replace(s$,"0128ALK6002010000000000","0128ALK6002010000000800")
UseFile(3)
WriteString(s$)
Until Eof(2) 0
EndIf
EndIf
CloseFile(2)
CloseFile(3)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
s$=replace(s$,"0128AGK6002010000000000","0128AGK6002010000000800")
Hang on, all this time you've been telling us that you want to find "0128AGK"
and replace it with "5555AFP" -- and now you show us the longer version above?
This could well have something to do with the problem... we were using different
strings for the search/replace the whole time, which is possibly why we couldn't
reproduce your bug...

Also, you have two files called "C:\DB95-EIN\L_95A" and "C:\DB95-EIN\L_95I", so
is that their actual filename, or is the extension missing?

Probably best if you zip up your source code and data files and re-send them to
me, so I can try it again with your exact data. I really want to work out what's
going on here...


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

Error should be fixed fast.
Because today i got a file 220 KB big and it didn't work.
I tried it under NT 4.0 and WINDOWS 95. On Both the Creatfile
had 0 Bytes and i got an error message. So the file is to big.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

Hi Ralf,

Some advice; if you open several files do not asume that all of them is openend correctly, because trying to close an unopened file will cause trouble i.e.

Code: Select all

; This is no way to do it!!
If CreateFile(0,"somefile")
  If ReadFile(1,"anotherfile")
    ...
  EndIf
EndIf
CloseFile(0) : CloseFile(1)


; This is the correct way of doing things!
If CreateFile(0,"somefile")
  If ReadFile(1,"anotherfile")
    ...
    CloseFile(1) ; If we put the close command here we're sure that we close an opened file
  EndIf
  CloseFile(0) ; And the same goes here
EndIf

In your code you could get away with not closing the files at all, assuming that your program quits to the desktop after the last line is executed. But if you're planing to place your code in a Procedure that you'll reuse serveral times during execution, you must ensure that you close the files after you finished using them.
This will probably don't fix your current problem but it's good codingstyle to do as i described in the code above. Your problem is perhaps caused by NT4 sp6 that you use. If i'm remembering correctly i've read on this forum about others who have had problems when using PB with NT4 sp6.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

it's not only a NT 4.0 problem.
I treid it on three different WINDOWS Operating Systems:
WINDOWS NT 4.0 SP 6a , WINDOWS 95, WINDOWS 98.
On all three i had the same problem.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
it's not only a NT 4.0 problem.
I treid it on three different WINDOWS Operating Systems:
WINDOWS NT 4.0 SP 6a , WINDOWS 95, WINDOWS 98.
On all three i had the same problem.
Hi Ralf,

From your E-Mailed example I found out the problem -- the file you're trying
to read is all one single string with a size of 700K (approx). That is, the
file does not have any end-of-line characters in it. PureBasic can't read
such large strings yet, which is why it's crashing. My example would work,
and indeed it does, if the file being read has end-of-line characters and
each line is less than 5000 characters in size.

Until PureBasic is updated to handle large strings (it can currently only
read strings of 5000 bytes), you'll need to find another solution. What I can
suggest is to read the file into a MemoryBank, then search/replace the data
in the MemoryBank, then save the MemoryBank back to disk. This should work
because, as far as I know, MemoryBanks can be of any size.

I don't have a working example for you but read this thread (all of it) from
this forum to get some ideas (until PureBasic gets updated):

viewtopic.php?t=136


PB - Registered PureBasic Coder

Edited by - PB on 12 January 2002 22:05:28
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

PB can now handle string until 64 Kb.
BTW, if you parse an ASCII file use ReadString() and it should work in all cases...

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
PB can now handle string until 64 Kb.
BTW, if you parse an ASCII file use ReadString() and it should work in all cases...
The problem was, using ReadString with Ralf's app didn't work because the string
in question was 700K long. That's why PureBasic kept crashing on it.


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

it works with files up to 64 k filesize.
will it be fixed be soon?


Edited by - ralf on 14 January 2002 12:47:22
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

i installed PUREBASIC 2.82 on WINDOWS 95 and tried to compile it
there but i got an error message "Zeile 23: ) missing to close Eof()"
Zeile 23 is LINE 23 which is : Until Eof(0) 0.
But when i compile it under NT 4.0 i have no error message (Except that
the compiler crashes : the old problem).
So the bugs and problems getting more and more.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

Until Eof(0) 0.

should be
Until Eof() 0.

which use the actual filehandle (->Usefile)

Siggi
Post Reply