[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Text File and Seek...

Posted on 1998-05-02
10
Medium Priority
?
603 Views
Last Modified: 2010-04-06
Hi to everyone
Seek doesn't work with text files...How can I solve this problem?
(I use the native in/out routines)....

Thanks
0
Comment
Question by:kkarnez
[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
  • 4
  • 3
  • 3
10 Comments
 
LVL 3

Expert Comment

by:jlove1
ID: 1340601
I suggest defining a filetype of FILE, and using blockread, and seek to find what your wanting. You COULD use the existing variable (and convert to FILE type), or you could simply make a new file variable and assign it to the same file. Hope this helps.

The other method would be to RESET the file, then do a for loop to the number where you want to seek into like this.
for a := 1 to seek do read(f)

 
0
 

Author Comment

by:kkarnez
ID: 1340602
If I use the first method then I will not be able to read with native routines.
The second method is very simple but the reason I want to 'Seek' is for  speed and this doesn't help.

0
 
LVL 3

Expert Comment

by:jlove1
ID: 1340603
I'm sorry, but you'll either have to use some NON-NATIVE routines, or use some slow routine.. Unless you're always "seeking ahead"....

Well, I just had another idea.. It'll only work if your text file is 64K or less.. I was going to post some code where you could load the entire file into memory as an array (pointer to that is) you could then perform all functions on that array... That;s about all I can come up with and still be "native"..

I really don't think there is a way to Natively seek into a text file.
sorry :-(

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 50 total points
ID: 1340604
Use a FileStream.  For example:

var F: TFileStream;
    B: Array[0..20] of Char;

begin
   F := TFileStream.Create('C:\Windows\FAQ.TXT', fmOpenRead);
   F.Seek(20, soFromCurrent); *
   F.Read(B, SizeOf(B));
   ShowMessage(StrPas(B));
   F.Free;
End;

* You can tell the stream to read from the begining of the file (soFromBegining), or from the End of the file (soFromEnd).

It works well - I just tested it.  I hope it helps you.

Stuart.
0
 
LVL 3

Expert Comment

by:jlove1
ID: 1340605
This may work in Delphi, but not in BP/TP.. I just tested it :-(..

0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1340606
JLove,

I presume you where directing your last comment at me.  What has B/T Pascal got to do with anything?  This is a Delphi area.  There was no mention in the question of this being a Pascal question (not that I can see anyway).

Stu.
0
 

Author Comment

by:kkarnez
ID: 1340607
I guess there isn't a way to implement it with native routines...
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1340608
Im sorry, I dont understand what you mean by "native routines"?  Can you ellaborate on this at all?  Do you mean like a direct API call or something?

Stu
0
 

Author Comment

by:kkarnez
ID: 1340609
I mean pascal native routines, like Read and Write and not OpenRead for example
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1340610
When you are doing a seek into a file, you need to be able to specify where you want to seek too.  In a text file, there are no records, or index markers, which enable a seek to work.

Another way you could do it would be to use the BlockRead function, which has been used since the early days of Pascal.  You could open the text file, and use BlockRead to read into the file by a set length, ie

BlockRead(TFile, Buffer, SeekSize, ReadResult);

Obviously, you would not be able to read in more data the the buffer would allow, but you could do it in a loop until you got to the section you require.

Another way you could try it is to open the file as an untyped file (ie: var TFile: File) and then you should be able to use the seek command.  I havent used Pascal for a while, or the Assign, Reset, Rewrite etc commands for ages, so I can't remember exactly.

Anyway, hope this helps.

Stu.
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

656 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