search function print the next line of my found line! c programming [duplicate]
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
c file struct strcmp
marked as duplicate by Antti Haapala
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 '18 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.
add a comment |
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
c file struct strcmp
marked as duplicate by Antti Haapala
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 '18 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 '18 at 20:57
Indentation!
– Antti Haapala
Nov 15 '18 at 21:05
add a comment |
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
c file struct strcmp
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
c file struct strcmp
edited Nov 15 '18 at 21:06
Antti Haapala
84.5k16161202
84.5k16161202
asked Nov 15 '18 at 20:51
Jabbar RemaJabbar Rema
13
13
marked as duplicate by Antti Haapala
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 '18 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
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 '18 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 '18 at 20:57
Indentation!
– Antti Haapala
Nov 15 '18 at 21:05
add a comment |
@user3121023 is correct. geeksforgeeks.org/strcmp-in-c-cpp
– Bwebb
Nov 15 '18 at 20:57
Indentation!
– Antti Haapala
Nov 15 '18 at 21:05
@user3121023 is correct. geeksforgeeks.org/strcmp-in-c-cpp
– Bwebb
Nov 15 '18 at 20:57
@user3121023 is correct. geeksforgeeks.org/strcmp-in-c-cpp
– Bwebb
Nov 15 '18 at 20:57
Indentation!
– Antti Haapala
Nov 15 '18 at 21:05
Indentation!
– Antti Haapala
Nov 15 '18 at 21:05
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 15 '18 at 20:58
lostbardlostbard
3,1781412
3,1781412
add a comment |
add a comment |
@user3121023 is correct. geeksforgeeks.org/strcmp-in-c-cpp
– Bwebb
Nov 15 '18 at 20:57
Indentation!
– Antti Haapala
Nov 15 '18 at 21:05