1

Is it possible to use hash function but with File instead of Array, and it's gonna be saving the record in a file position and then search will fseek to that position, but I'm not sure how to open a file with let's say 1000 line, do you think that method could work?

asked Aug 10, 2012 at 16:34
4
  • 3
    You are attempting to find a technical solution to a problem. The approach that you are using is likely to have some issues. What is the original problem that you are trying to solve? Commented Aug 10, 2012 at 16:49
  • Nothing really, I was just thinking if that is possible. for example can that be good enough to be used instead of a DB, I don't mean replace db, but for example if you want to use simple key with some data in a file. Commented Aug 10, 2012 at 16:58
  • 2
    If you have an algorithm that works within a single slab of linear memory, you have something can be translated to a random-access file. You're going to have a hard time if you have variable-width records, handling collisions, rehashing, etc. You can implement a rudimentary system, but I would strongly recommend looking at a lightweight solution like SQLite3, leveldb (or possibly Berkley DB if you can accept copyleft). Commented Aug 10, 2012 at 17:33
  • You can even use a database for that - Depends on the problem. Commented Aug 11, 2012 at 11:20

1 Answer 1

1

Sure, if you have a hashing function that can neatly map your data. Two things to keep an eye on:

  1. Does your OS/file system support sparse files? Can you seek beyond the end of the file and write a new record without intervening data?
  2. How are you going to handle hash collisions? Will each record have some kind of header that contains a pointer to the next bucket in an overflow chain, or will you just overflow into the next available slot? If you just go to the next slot, how will you tell if it's available?

I'd recommend reading up on ISAM files, they're pretty close to what you're looking to do.

answered Aug 10, 2012 at 18:09
1
  • I'm using Linux and I think it does support it, and made also think about how to delete or replace data, that won't be I guess Commented Aug 10, 2012 at 21:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.