C how to search string in a file?
up vote
1
down vote
favorite
I have a problem with my code, I'm trying to search a string in a file and I can read it but, when I compare two strings it takes only the last one of the file as equal to the the first string entered with the scanf()
.
So imagine I wrote in my file three words and each one is returning to the line.
test
test12
test123
If in my scanf()
I write test12
for example or test
when it's going to read it will return false to the compare so (!== 0). But if I write test123
it will works because it's the last word of the file but I don't know why?
char word[26];
char singleLine[26];
FILE *file = fopen("bin/Release/myWords.txt", "a+");
scanf("%26s", word);
if (file != NULL) {
while (!feof(file)) {
fgets(singleLine, 26, file);
compare = strcmp(singleLine, word);
if (compare == 0) {
printf("n%sn",word);
}
}
fclose(file);
}
c file string-comparison
add a comment |
up vote
1
down vote
favorite
I have a problem with my code, I'm trying to search a string in a file and I can read it but, when I compare two strings it takes only the last one of the file as equal to the the first string entered with the scanf()
.
So imagine I wrote in my file three words and each one is returning to the line.
test
test12
test123
If in my scanf()
I write test12
for example or test
when it's going to read it will return false to the compare so (!== 0). But if I write test123
it will works because it's the last word of the file but I don't know why?
char word[26];
char singleLine[26];
FILE *file = fopen("bin/Release/myWords.txt", "a+");
scanf("%26s", word);
if (file != NULL) {
while (!feof(file)) {
fgets(singleLine, 26, file);
compare = strcmp(singleLine, word);
if (compare == 0) {
printf("n%sn",word);
}
}
fclose(file);
}
c file string-comparison
1
see Why is “while ( !feof (file) )” always wrong?
– yano
yesterday
fgets
includes the final 'n`, so it gets compared along with the rest of your string.
– usr2564301
yesterday
you need to learn how to use the debugger and use it to see the strings being compared. Until you do this you will never be able to write code.
– john elemans
yesterday
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a problem with my code, I'm trying to search a string in a file and I can read it but, when I compare two strings it takes only the last one of the file as equal to the the first string entered with the scanf()
.
So imagine I wrote in my file three words and each one is returning to the line.
test
test12
test123
If in my scanf()
I write test12
for example or test
when it's going to read it will return false to the compare so (!== 0). But if I write test123
it will works because it's the last word of the file but I don't know why?
char word[26];
char singleLine[26];
FILE *file = fopen("bin/Release/myWords.txt", "a+");
scanf("%26s", word);
if (file != NULL) {
while (!feof(file)) {
fgets(singleLine, 26, file);
compare = strcmp(singleLine, word);
if (compare == 0) {
printf("n%sn",word);
}
}
fclose(file);
}
c file string-comparison
I have a problem with my code, I'm trying to search a string in a file and I can read it but, when I compare two strings it takes only the last one of the file as equal to the the first string entered with the scanf()
.
So imagine I wrote in my file three words and each one is returning to the line.
test
test12
test123
If in my scanf()
I write test12
for example or test
when it's going to read it will return false to the compare so (!== 0). But if I write test123
it will works because it's the last word of the file but I don't know why?
char word[26];
char singleLine[26];
FILE *file = fopen("bin/Release/myWords.txt", "a+");
scanf("%26s", word);
if (file != NULL) {
while (!feof(file)) {
fgets(singleLine, 26, file);
compare = strcmp(singleLine, word);
if (compare == 0) {
printf("n%sn",word);
}
}
fclose(file);
}
c file string-comparison
c file string-comparison
edited yesterday
halfer
14.2k757105
14.2k757105
asked yesterday
Louis Hervé
153
153
1
see Why is “while ( !feof (file) )” always wrong?
– yano
yesterday
fgets
includes the final 'n`, so it gets compared along with the rest of your string.
– usr2564301
yesterday
you need to learn how to use the debugger and use it to see the strings being compared. Until you do this you will never be able to write code.
– john elemans
yesterday
add a comment |
1
see Why is “while ( !feof (file) )” always wrong?
– yano
yesterday
fgets
includes the final 'n`, so it gets compared along with the rest of your string.
– usr2564301
yesterday
you need to learn how to use the debugger and use it to see the strings being compared. Until you do this you will never be able to write code.
– john elemans
yesterday
1
1
see Why is “while ( !feof (file) )” always wrong?
– yano
yesterday
see Why is “while ( !feof (file) )” always wrong?
– yano
yesterday
fgets
includes the final 'n`, so it gets compared along with the rest of your string.– usr2564301
yesterday
fgets
includes the final 'n`, so it gets compared along with the rest of your string.– usr2564301
yesterday
you need to learn how to use the debugger and use it to see the strings being compared. Until you do this you will never be able to write code.
– john elemans
yesterday
you need to learn how to use the debugger and use it to see the strings being compared. Until you do this you will never be able to write code.
– john elemans
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Your program only works in very special cases:
scanf("%26s", word);
may affect up to 27 bytes in the destination array, which is defined with a length of only26
.- furthermore, you should check the return value to avoid undefined behavior on invalid input.
fopen("bin/Release/myWords.txt", "a+");
opens the file in append mode: is this necessary?
while (!feof(file))
is always wrong, you should instead check the return value offgets()
that returnsNULL
at end of file.
compare = strcmp(singleLine, word);
only compares for an exact math of the full line, which can only happen if the word has 25 characters, otherwise the trailing newline insingleLine
will cause the comparison to fail. Furthermore, broken lines may cause unexpected results, as well as if the file does not end with a newline.- the reason it matches the last word in the file is you forget to write a trailing newline after the last word in the file, so the last
fgets()
fills the buffer with the exact word and no trailing newline. - if you search for matches inside the line, you should use a larger buffer and search for a match with
strstr
. - if you search for a exact match, you should strip the trailing newline before the comparison.
Here is a modified version:
#include <stdio.h>
#include <string.h>
int main() {
char word[27];
char singleLine[256];
FILE *file = fopen("bin/Release/myWords.txt", "r");
if (scanf("%26s", word) != 1)
return 1;
if (file != NULL) {
while (fgets(singleLine, sizeof singleLine, file)) {
singleLine[strcspn(singleLine, "n")] = ''; // strip the newline if any
compare = strcmp(singleLine, word);
if (compare == 0) {
printf("n%sn", word);
}
}
fclose(file);
}
return 0;
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your program only works in very special cases:
scanf("%26s", word);
may affect up to 27 bytes in the destination array, which is defined with a length of only26
.- furthermore, you should check the return value to avoid undefined behavior on invalid input.
fopen("bin/Release/myWords.txt", "a+");
opens the file in append mode: is this necessary?
while (!feof(file))
is always wrong, you should instead check the return value offgets()
that returnsNULL
at end of file.
compare = strcmp(singleLine, word);
only compares for an exact math of the full line, which can only happen if the word has 25 characters, otherwise the trailing newline insingleLine
will cause the comparison to fail. Furthermore, broken lines may cause unexpected results, as well as if the file does not end with a newline.- the reason it matches the last word in the file is you forget to write a trailing newline after the last word in the file, so the last
fgets()
fills the buffer with the exact word and no trailing newline. - if you search for matches inside the line, you should use a larger buffer and search for a match with
strstr
. - if you search for a exact match, you should strip the trailing newline before the comparison.
Here is a modified version:
#include <stdio.h>
#include <string.h>
int main() {
char word[27];
char singleLine[256];
FILE *file = fopen("bin/Release/myWords.txt", "r");
if (scanf("%26s", word) != 1)
return 1;
if (file != NULL) {
while (fgets(singleLine, sizeof singleLine, file)) {
singleLine[strcspn(singleLine, "n")] = ''; // strip the newline if any
compare = strcmp(singleLine, word);
if (compare == 0) {
printf("n%sn", word);
}
}
fclose(file);
}
return 0;
}
add a comment |
up vote
1
down vote
accepted
Your program only works in very special cases:
scanf("%26s", word);
may affect up to 27 bytes in the destination array, which is defined with a length of only26
.- furthermore, you should check the return value to avoid undefined behavior on invalid input.
fopen("bin/Release/myWords.txt", "a+");
opens the file in append mode: is this necessary?
while (!feof(file))
is always wrong, you should instead check the return value offgets()
that returnsNULL
at end of file.
compare = strcmp(singleLine, word);
only compares for an exact math of the full line, which can only happen if the word has 25 characters, otherwise the trailing newline insingleLine
will cause the comparison to fail. Furthermore, broken lines may cause unexpected results, as well as if the file does not end with a newline.- the reason it matches the last word in the file is you forget to write a trailing newline after the last word in the file, so the last
fgets()
fills the buffer with the exact word and no trailing newline. - if you search for matches inside the line, you should use a larger buffer and search for a match with
strstr
. - if you search for a exact match, you should strip the trailing newline before the comparison.
Here is a modified version:
#include <stdio.h>
#include <string.h>
int main() {
char word[27];
char singleLine[256];
FILE *file = fopen("bin/Release/myWords.txt", "r");
if (scanf("%26s", word) != 1)
return 1;
if (file != NULL) {
while (fgets(singleLine, sizeof singleLine, file)) {
singleLine[strcspn(singleLine, "n")] = ''; // strip the newline if any
compare = strcmp(singleLine, word);
if (compare == 0) {
printf("n%sn", word);
}
}
fclose(file);
}
return 0;
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your program only works in very special cases:
scanf("%26s", word);
may affect up to 27 bytes in the destination array, which is defined with a length of only26
.- furthermore, you should check the return value to avoid undefined behavior on invalid input.
fopen("bin/Release/myWords.txt", "a+");
opens the file in append mode: is this necessary?
while (!feof(file))
is always wrong, you should instead check the return value offgets()
that returnsNULL
at end of file.
compare = strcmp(singleLine, word);
only compares for an exact math of the full line, which can only happen if the word has 25 characters, otherwise the trailing newline insingleLine
will cause the comparison to fail. Furthermore, broken lines may cause unexpected results, as well as if the file does not end with a newline.- the reason it matches the last word in the file is you forget to write a trailing newline after the last word in the file, so the last
fgets()
fills the buffer with the exact word and no trailing newline. - if you search for matches inside the line, you should use a larger buffer and search for a match with
strstr
. - if you search for a exact match, you should strip the trailing newline before the comparison.
Here is a modified version:
#include <stdio.h>
#include <string.h>
int main() {
char word[27];
char singleLine[256];
FILE *file = fopen("bin/Release/myWords.txt", "r");
if (scanf("%26s", word) != 1)
return 1;
if (file != NULL) {
while (fgets(singleLine, sizeof singleLine, file)) {
singleLine[strcspn(singleLine, "n")] = ''; // strip the newline if any
compare = strcmp(singleLine, word);
if (compare == 0) {
printf("n%sn", word);
}
}
fclose(file);
}
return 0;
}
Your program only works in very special cases:
scanf("%26s", word);
may affect up to 27 bytes in the destination array, which is defined with a length of only26
.- furthermore, you should check the return value to avoid undefined behavior on invalid input.
fopen("bin/Release/myWords.txt", "a+");
opens the file in append mode: is this necessary?
while (!feof(file))
is always wrong, you should instead check the return value offgets()
that returnsNULL
at end of file.
compare = strcmp(singleLine, word);
only compares for an exact math of the full line, which can only happen if the word has 25 characters, otherwise the trailing newline insingleLine
will cause the comparison to fail. Furthermore, broken lines may cause unexpected results, as well as if the file does not end with a newline.- the reason it matches the last word in the file is you forget to write a trailing newline after the last word in the file, so the last
fgets()
fills the buffer with the exact word and no trailing newline. - if you search for matches inside the line, you should use a larger buffer and search for a match with
strstr
. - if you search for a exact match, you should strip the trailing newline before the comparison.
Here is a modified version:
#include <stdio.h>
#include <string.h>
int main() {
char word[27];
char singleLine[256];
FILE *file = fopen("bin/Release/myWords.txt", "r");
if (scanf("%26s", word) != 1)
return 1;
if (file != NULL) {
while (fgets(singleLine, sizeof singleLine, file)) {
singleLine[strcspn(singleLine, "n")] = ''; // strip the newline if any
compare = strcmp(singleLine, word);
if (compare == 0) {
printf("n%sn", word);
}
}
fclose(file);
}
return 0;
}
edited yesterday
answered yesterday
chqrlie
58.1k745100
58.1k745100
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401808%2fc-how-to-search-string-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
see Why is “while ( !feof (file) )” always wrong?
– yano
yesterday
fgets
includes the final 'n`, so it gets compared along with the rest of your string.– usr2564301
yesterday
you need to learn how to use the debugger and use it to see the strings being compared. Until you do this you will never be able to write code.
– john elemans
yesterday