Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
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? –  MichaelT Aug 10 '12 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. –  poly Aug 10 '12 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). –  Lars Viklund Aug 10 '12 at 17:33
    
You can even use a database for that - Depends on the problem. –  Emmad Kareem Aug 11 '12 at 11:20

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 –  poly Aug 10 '12 at 21:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.