I have a text file with 500000 records , each record contain student ID and another index number.
i insert this recors with separate function ,and i want when the user enter the student id the program get the corsponding index number and put it in avariable and use it in another function.

can any one help.

i only want to no how can i get the number and but it in a variable.

thanks

You have five options that I can think of right off-hand. How much effort you want to put into it depends on your goals -- just to satisfy a student course requirement or do you want to implement it on-the-job. Will it be just a one-time project, or are you going to use this program continuously over a long period of time.

  1. When you enter a student id read the file one line at a time, reading sequentially from beginning of the file to the end. With 500,000 records that might be somewhat time consuming, especially if you want a lot if id's.
  2. Get a list of all the ids you want to look for then searh the file for all of them at the same time. This will be a little faster than the first option because the file only has to be read once.
  3. Read the entire file into an array when the program starts then the program can search the in-memory array instead of the file. With today's computers and compilers reading 500,000 records into memory at one time will probably not be much a burden on system resources.
  4. If this is going to be an ongoing thing, such as use the file over a long period of time then you might consider indexing the file or rewriting it so that it has fixed-length records and is stored in sorted order. With that you could do a binary search algorithm to search the file very very fast.
  5. Option 3 can be enhanced by reading all records in memory, sorting them, then use a binary search algorithm the find the desired student ids.

If you actually own the written data and it is put though an array or class use fseek:

//For example to fetch record #5
fseek ( File , (5-1)*sizeof(CLASS) , SEEK_SET );
fread ( &VAR , 1 , sizeof(CLASS) , File );
/* Here CLASS can be replaced by it's instance or the array
And VAR is a variable for buffer storage */
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.