Question
How to use Binary Search to search in a text file(C language) For example, a text file that contains a list of North American phone
How to use Binary Search to search in a text file(C language)
For example, a text file that contains a list of North American phone number prefixes:
201200Jersey City NJ
201202Hackensack NJ
201203Hackensack NJ
201204Jersey City NJ
201205Jersey City NJ
201206Hackensack NJ
201207Newark NJ
201208Jersey City NJ
201209Jersey City NJ
201210Union City NJ
201212Hackensack NJ
....etc.
The text file is sorted by ascending prefixes.
I need to write a program that always take two arguments: the text filename and a prefix. The program must open the file and search it for the location identified by the prefix specified by the second argument. And the prefix must consist of six digits, which are all numerical (0 thru 9).
The program must open the file and search it for the location identified by the prefix specified by the second argument. If a corresponding line is found, the line must be output on standard output, after stripping the superfluous spaces in the end (and without the prefix).
Example output:
In command line: ./program text.txt 201203
Position:3 Hackensack NJ
And I'm supposed to base my program only on the system calls open, read, write, lseek and close. (Can not use fopen, fread, fprintf, etc.)
For a fast searching method, I'm thinking to use Binary search to do it, but also get stuck on writing the code...
I'm a begnninr in C language, thanks for any helps I can get!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started