search function print the next line of my found line! c programming [duplicate]












-1















This question already has an answer here:




  • Why does strcmp() return 0 when its inputs are equal?

    7 answers



  • Is it a bad practice to use an if-statement without curly braces? [closed]

    15 answers




I have method add, works good, also I have method view and it's work
in this method



void search(){


FILE* fr;
fr=fopen("record.txt","r");
Record r;
char na[10];
printf(" Enter the name:... ");
scanf("%s",na);

while(fread(&r,sizeof(r),1,fr)){
if(strcmp(na,r.name))
printf(" A match has been found ... ");
printf("%s %s %s n",r.name,r.email,r.phoneNum);
}
fclose(fr);}`


I don't know what's the wrong!
I have a struct and inside it i have an array of name
I ask user to enter names, then write it in a file
if I search for specific name
the method prints the next name of exact name !



what should I do?



***Record is a struct but I use typedef to rename










share|improve this question















marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 at 21:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • @user3121023 is correct. geeksforgeeks.org/strcmp-in-c-cpp
    – Bwebb
    Nov 15 at 20:57










  • Indentation!
    – Antti Haapala
    Nov 15 at 21:05
















-1















This question already has an answer here:




  • Why does strcmp() return 0 when its inputs are equal?

    7 answers



  • Is it a bad practice to use an if-statement without curly braces? [closed]

    15 answers




I have method add, works good, also I have method view and it's work
in this method



void search(){


FILE* fr;
fr=fopen("record.txt","r");
Record r;
char na[10];
printf(" Enter the name:... ");
scanf("%s",na);

while(fread(&r,sizeof(r),1,fr)){
if(strcmp(na,r.name))
printf(" A match has been found ... ");
printf("%s %s %s n",r.name,r.email,r.phoneNum);
}
fclose(fr);}`


I don't know what's the wrong!
I have a struct and inside it i have an array of name
I ask user to enter names, then write it in a file
if I search for specific name
the method prints the next name of exact name !



what should I do?



***Record is a struct but I use typedef to rename










share|improve this question















marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 at 21:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • @user3121023 is correct. geeksforgeeks.org/strcmp-in-c-cpp
    – Bwebb
    Nov 15 at 20:57










  • Indentation!
    – Antti Haapala
    Nov 15 at 21:05














-1












-1








-1








This question already has an answer here:




  • Why does strcmp() return 0 when its inputs are equal?

    7 answers



  • Is it a bad practice to use an if-statement without curly braces? [closed]

    15 answers




I have method add, works good, also I have method view and it's work
in this method



void search(){


FILE* fr;
fr=fopen("record.txt","r");
Record r;
char na[10];
printf(" Enter the name:... ");
scanf("%s",na);

while(fread(&r,sizeof(r),1,fr)){
if(strcmp(na,r.name))
printf(" A match has been found ... ");
printf("%s %s %s n",r.name,r.email,r.phoneNum);
}
fclose(fr);}`


I don't know what's the wrong!
I have a struct and inside it i have an array of name
I ask user to enter names, then write it in a file
if I search for specific name
the method prints the next name of exact name !



what should I do?



***Record is a struct but I use typedef to rename










share|improve this question
















This question already has an answer here:




  • Why does strcmp() return 0 when its inputs are equal?

    7 answers



  • Is it a bad practice to use an if-statement without curly braces? [closed]

    15 answers




I have method add, works good, also I have method view and it's work
in this method



void search(){


FILE* fr;
fr=fopen("record.txt","r");
Record r;
char na[10];
printf(" Enter the name:... ");
scanf("%s",na);

while(fread(&r,sizeof(r),1,fr)){
if(strcmp(na,r.name))
printf(" A match has been found ... ");
printf("%s %s %s n",r.name,r.email,r.phoneNum);
}
fclose(fr);}`


I don't know what's the wrong!
I have a struct and inside it i have an array of name
I ask user to enter names, then write it in a file
if I search for specific name
the method prints the next name of exact name !



what should I do?



***Record is a struct but I use typedef to rename





This question already has an answer here:




  • Why does strcmp() return 0 when its inputs are equal?

    7 answers



  • Is it a bad practice to use an if-statement without curly braces? [closed]

    15 answers








c file struct strcmp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 21:06









Antti Haapala

80.8k16153193




80.8k16153193










asked Nov 15 at 20:51









Jabbar Rema

13




13




marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 at 21:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 at 21:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • @user3121023 is correct. geeksforgeeks.org/strcmp-in-c-cpp
    – Bwebb
    Nov 15 at 20:57










  • Indentation!
    – Antti Haapala
    Nov 15 at 21:05


















  • @user3121023 is correct. geeksforgeeks.org/strcmp-in-c-cpp
    – Bwebb
    Nov 15 at 20:57










  • Indentation!
    – Antti Haapala
    Nov 15 at 21:05
















@user3121023 is correct. geeksforgeeks.org/strcmp-in-c-cpp
– Bwebb
Nov 15 at 20:57




@user3121023 is correct. geeksforgeeks.org/strcmp-in-c-cpp
– Bwebb
Nov 15 at 20:57












Indentation!
– Antti Haapala
Nov 15 at 21:05




Indentation!
– Antti Haapala
Nov 15 at 21:05












1 Answer
1






active

oldest

votes


















1














You don't have the printf("%s %s %s n",r.name,r.email,r.phoneNum); within the if brackets so it is going to display each name, not just the matching one.



In addition strcmp will return 0 if it is a match, so currently printf(" A match has been found ... "); is executing when a match is not found.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You don't have the printf("%s %s %s n",r.name,r.email,r.phoneNum); within the if brackets so it is going to display each name, not just the matching one.



    In addition strcmp will return 0 if it is a match, so currently printf(" A match has been found ... "); is executing when a match is not found.






    share|improve this answer


























      1














      You don't have the printf("%s %s %s n",r.name,r.email,r.phoneNum); within the if brackets so it is going to display each name, not just the matching one.



      In addition strcmp will return 0 if it is a match, so currently printf(" A match has been found ... "); is executing when a match is not found.






      share|improve this answer
























        1












        1








        1






        You don't have the printf("%s %s %s n",r.name,r.email,r.phoneNum); within the if brackets so it is going to display each name, not just the matching one.



        In addition strcmp will return 0 if it is a match, so currently printf(" A match has been found ... "); is executing when a match is not found.






        share|improve this answer












        You don't have the printf("%s %s %s n",r.name,r.email,r.phoneNum); within the if brackets so it is going to display each name, not just the matching one.



        In addition strcmp will return 0 if it is a match, so currently printf(" A match has been found ... "); is executing when a match is not found.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 at 20:58









        lostbard

        3,0531311




        3,0531311















            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents