Solved

C - fast Searching for a string in a textfile

Posted on 1998-08-22
3
217 Views
Last Modified: 2010-04-15
hi folks,

i'm developing with borland c++ 4.52 under nt 4.0 but the project is compiled as an easywin 16-bit application.

is it possible to open a very large textfile (2-4 GB) and is so how can i find a certain string-token (e.g. a filename) as fastest as it can be? is this the right way or are there any other opportunities to do that, which lead to a better performance?
0
Comment
Question by:tha_incredible_bo
[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
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1252263
A 16-bit application is limited by the underlying OS architecture.  Unfortunately, files larger than 2^31 bytes (2GB) are not supported on 16bit applications because all the functions that deal with file positions are designed to work with 32bit signed values.

Finding a string in a file is easier if the file consists of fixed-size records.  In that case, you can read a buffer at a time (each buffer will consist of n records) and search each record using strcmp().  If there are no fixed-size records than you'll have to deal with the possibility that the string may spawn across two buffers so you'll have to make sure that the buffers you read overlap by the length of the string.

In any case, if you are accessing the file in a purely sequential way, you can save some overhead by using OS commands directly instead of C run-time functions.
0
 
LVL 1

Author Comment

by:tha_incredible_bo
ID: 1252264
hi alexo,

thanks for your answer. Unfortunately the records are not with fixed length so that the work to handle them is a little bit difficult. Allright I think this will cost some time for me.

If you'd be so kind will you please explain what you mean with using OS commands directly and which ones to use (instead of C run-time functions)?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1252265
Well, C is a very small language but it comes with an extensive run-time library (it is either linked statically to your program or dynamically - as a DLL).  This library has many functions that deal with files.  Those can be categorized into:
1. High level: fopen(), fread(), fwite(), etc.
2. Low level: open(), read(), write(), etc.

Now, those functions try to be efficient for the *general case* by using caching, bufferring, etc.  However, if you only want to access a file sequentially (open it, read it, close it) you can save some unnecessery (in your case) overhead by using the underlying OS functions instead.  In the case of Win3.x those are _lopen(), _hread(), _hwrite(), etc.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

707 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