Quick File access

This is a discussion on Quick File access within the C Programming forums, part of the General Programming Boards category; Hi Please can anyone advise me on how to tackle this task - I have a C file can be ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    Question Quick File access

    Hi

    Please can anyone advise me on how to tackle this task -

    I have a C file can be text or binary it's up to me how I create it which contains 1000s of records. I also have a user interface wrritten in Ansi C which allows users to access this file by entering search parameters, they can also specify a sort criteria. Currently the data file uses special VMS formats which allows for effcient searching and retrieval of multiple records.

    My task is to rewrite this user interface using Ansic C that is portable across other OS i.e I can't use these super duper VMS file functions. I had a look at C and I can't see how to create an index file, I had a look at the fseek, but I can't see how that will help. Also does anyone know of any sorting algorithms.

    Any clues/hints would be much appreciated.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > I had a look at C and I can't see how to create an index file,
    An index file is just going to be an array of offsets, which you pass to fseek

    Normally, you'd store the index file in memory,
    Code:
    long offset;
    int record_num;
    
    // read from a file
    fseek( index_file_fp, record_num * sizeof(long), SEEK_SET );
    fread( &offset, sizeof(long), 1, index_file_fp );
    // or read from an array, which is the in-memory copy of index_file
    offset = index_array[record_num];
    
    // then you seek to that offset in the data file, and read/write the record
    fseek( data_file_fp, offset, SEEK_SET );
    fread( &record, sizeof(record), 1, data_file_fp );
    > Also does anyone know of any sorting algorithms.
    qsort in stdlib.h is pretty good most of the time
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Sayeh
    Guest
    Salem, thank you thank you thank you. So many people never figure out that the fast way to work with a file is to put it in memory....

    Thank _you_.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    Lightbulb

    Thanks Salem for your response - I have been told that there is something called hash functins in C and that this is the best way to search for records ... can anyone elaborate ?? I'm told that I use the hash function to locate the record and that I use the fseek command to pull the records fromn the file. That's all I know !

  5. #5
    Sayeh
    Guest

    Hashing

    'Hashing' is a term used for index management for a database. (height balanced, n-way, k-way, and binary tree management).


    to 'hash' a file is to zip through the file once and build an index that can be sorted on for that file. Since you are using records which should be created out of typedef'd structures, calculating a file position is very easy--

    Let's say you want to read record 5 out of the file--


    typdef struct
    {
    ... /* fields */
    }rec;

    that is simply fseek() to "sizeof(rec) * 4L"

    4 = index_position - 1 /* because a file starts at zero, subtract one)

    then read in sizeof(rec) bytes from that location.

    ---

    The idea is that you can manipulate a very small amount of data (your 'hash' table, which might be nothing more than an array of longs containing some unique index in each one relevant to a specific record), using small amounts of RAM and processing time, to get big results.

    If you had a database of 10,000 entries, would it not be easier to move and swap and sort an array of 10,000 longs, as opposed to fooling around with 10,000 records? Plus, the hash table is manipulated in RAM, not on disk, so it's incredibly fast.

    enjoy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. quick question on direct file access
    By Stevek in forum C++ Programming
    Replies: 2
    Last Post: 03-21-2003, 12:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21