Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Searching Big Text Files...

Posted on 1999-07-18
11
Medium Priority
?
157 Views
Last Modified: 2010-04-06
Right now my program loads a text file into memory using the TStringList. This beginning to be a memory problem and I need another solution. I do two things to the file, 1. I search it, and 2. I copy the first line to a string and then delete it from the file. Is there any way I can do this without using the TStringList and without loading the file into memory? Please show code.

Thanks.
0
Comment
Question by:dokken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 240 total points
ID: 1389398
I don't think so.. you might try using TMemoryStream and it's Memory property... then use StrPos() which uses PChar, so you can use TMemoryStream's Memory property there....

let me know if you need more help...

..-=ViKtOr=-..
0
 
LVL 15

Expert Comment

by:simonet
ID: 1389399
That was pretty fast, Viktor!
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389400
hehe... here is some test code as you requested ;-))

var
  m : tmemorystream;
  p : pchar;
begin
  m := tmemorystream.create;
  m.LoadFromFile('c:\windows\desktop\link.txt');
  p := StrPos(m.Memory, 'www');
  if p <> nil then Memo1.Text := IntToStr(p-m.Memory+1);
  m.free;
end;

the LINK.TXT files was on my desktop so I decided to use that as a test :)) it contains a few links... i tried to search for WWW... when I used IntToStr(p-m.Memory) it returned 7 but the real position was 8, that was simply because PChars work that way :)).... from 0 up to n-1... anyway.... that's why I added IntToStr(p-m.Memory+1) in the above code, and it works great :))

Lemme know if you need more help with this...

..-=ViKtOr=-..
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:dokken
ID: 1389401
Works great, but how can I grab the first line from the file and then delete the first line from the file? That was the other part of the problem.

Thanks.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389402
okay, you should use something like this..

p := strpos(m.memory, #13);

then you can for example use Move() and copy all the memory from where P points at, up to m.Memory... then use SetSize() to set the size of the Memory stream to m.Size-(p-m.memory)

that'll do it i guess....

-vik-
0
 

Author Comment

by:dokken
ID: 1389403
I just ran into a problem with:  p := StrPos(m.Memory, 'www'); The problem is my search string is always different, how can I convert a string to a pchar?
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389404
simply typecast it...

var
  str : string;
  p : pchar;
begin
  str := 'www';
  p := strpos(m.memory, pchar(str));
...
end;

that'll do it :)
0
 

Author Comment

by:dokken
ID: 1389405
Cool, thanks for all your help!
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389406
No Problem :))

btw, Did you get it to work as you wanted? Axtually hold on a sec..
Let me test what i told you and give you a working example....

...time passes... :-))

k, i'm back... here is an example.. this one wouldn't delete the first line if it's the only one, but that is easier to do ;)) hehe... ok, here it goes...

procedure TForm1.Button1Click(Sender: TObject);
var
  m : tmemorystream;
  p : pchar;
  len : integer;
begin
  m := tmemorystream.create;
  m.LoadFromFile('c:\windows\desktop\test.txt');
  Memo1.Text := StrPas(m.memory)+#13#10+'--------'+#13#10;
  p := StrPos(m.Memory, #13);
  if p <> nil then begin
    len := p-m.memory;
    Move((p+2)^, m.Memory^, m.size-len);
    m.size := m.size-len;
  end;
  Memo1.Text := Memo1.Text + StrPas(m.memory);
  m.free;
end;

Hope this helps... If you're still having problems, please let me know..

..-=ViKtOr=-..
0
 

Author Comment

by:dokken
ID: 1389407
Your example is good except for one thing, the line: Memo1.Text := StrPas(m.memory)+#13#10+'--------'+#13#10; loads the whole file into the memo (I commented out the other part so it wouldn't show up in the memo).  I also tried changing it to just: Memo1.Text := StrPas(m.memory)+#13#10; but it also loads the whole thing into the memo.  That's the part where it's supposed to grab just the first line, right?
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389408
okay, let me explain it :))

this was just a test, not your original program, so I just made a test program to get it to work...

first, it loads the WHOLE file... writes a new line some dashes ------ and then a new line again.. then it removes the first line of the file, and writes the contents of the memory once again,. under the ------------- dashed line.... so it actually lods the memory into the memo twice... once with the whole text, and then without the first line.. you can now take this and implemented into your code by just using the second piece of code that removes the line and loads the text.. you can remove the #13#10 and the ---------, etc...

btw, the above code does not change the contents of the file.. only the data which is in memory... if you want to save it to file, simply do m.SaveToFile(...)...

Good Luck!!

..-=ViKtOr=-..
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question