473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Is this function correct? please help

momotaro
357 100+
am trying to write the code of a function that search for an employee given its employee id on a binary file that contains only employee structure.
the function displays the corresponding data whene found a "not found" message otherwise.
my struct contains: int id, char *name, double salary, int rank.

Expand|Select|Wrap|Line Numbers
  1. void search(FILE *bf, int id){
  2. employee temp;
  3. fb = fopen("fb", "rb");
  4. while(fread(&temp, sizeof(employee), 1, bf)){
  5. if(strcmp(temp.id, id)){
  6. display;
  7. break;
  8. }
  9. else
  10. printf("not found");
  11. }
  12. }
  13.  
thx!
Jun 26 '07 #1
2 1078
So what your program does is prints a lot of "Not Found" and finally a "Found" if it gets a match in the file, right?

I am assuming this is problem you are talking about as you didint mention anything.

You see, in the while loop, every time it reads a data from the file, it checks for match. It if matches then it displays that and breaks out of the loop. But if the current data doesnt match? Then it prints "Not found" and moves on to next data from file...that results in printing so many "Not Found".

What you can do is, set a flag (an integer may be) to 0 at the beginning of the function. Then if you find a match then make the flag 1 and break out of the loop as it is now. You can remove the else part now.

Now once you are out of the loop, you can check the value of the flag, if still it is 0, then "Not Found"..otherwise ou have already shown the data.

Hope it helps!
Sorower
Jun 26 '07 #2
niskin
109 100+
Expand|Select|Wrap|Line Numbers
  1. fb = fopen("fb", "rb");
  2.  
Not so sure about this line. What is the fb pointing to?
Jun 29 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Surya Kiran | last post by:
Hi all, I've written a function template. say template <class T> fn (T var) { ... } Is there any way, from within the function, can we check what type of argument we've passed on to the...
3
by: CoolPint | last post by:
After upgrading to gcc 3.4.2 from gcc 3.2.3, I got compiler errors that I could not figure out. After reading other postings, I learned that my coding was not compliant to the standard in the first...
2
by: Chuck Martin | last post by:
I am having a most frustrating problem that references, web searches, and other resources are no help so far in solving. Basically, I'm trying to design a pop-up window to be called with a funciton...
8
by: Berhack | last post by:
I am not too familiar with C# interop so please help me out. I need to call the following C function (in a DLL): // this creates an array of strings // LPTSTR is just char * void C_Func(LPTSTR...
26
by: jshanman | last post by:
I am writing a timeline that uses Google Maps. I have a function that converts a date time to latitude coords. This function is used to draw the markers on the timeline. I need a reverse function...
17
by: Eric Brunel | last post by:
Hi all, I just stepped on a thing that I can't explain. Here is some code showing the problem: ----------------------------- class C: f = None def __init__(self): if self.f is not None:
5
by: mk | last post by:
Greetings all - im new to this newsgroup so pls dont flame me :p I need some help! Please view the html below in a browser. Or goto this url -http://firebrain.co.uk/java-problem.htm (Assuming...
20
by: jung | last post by:
Hello!. can anyone help me? i have develop a simple program for quering programs and the tables they use. now, the problem is about how to display the correct total number of tables a...
3
by: =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?= | last post by:
Mo <Mehile.Orloff@gmail.comwrote: may be the iteration should be inside your function qryFunc ? or, return $fQuery (when no error) and then : $reponse=qryFunc($select, $from);
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.