HWZ Forums

Login Register FAQ Mark Forums Read

need help on c

Reply
 
LinkBack Thread Tools
Old 07-07-2010, 10:42 PM   #1
Senior Member
 
Join Date: Mar 2009
Posts: 1,329
need help on c

after using fgets to read a txt file line by line from the command line,
i want to do smth like a regular expression thing like for example if " apple" is in the line. how am i suppose to do that?
i tried (line == apple) but failed where line is the line that i get using fgets
thanks
__________________
i will be happy, i must be!
affinityseven is offline   Reply With Quote
Old 07-07-2010, 11:39 PM   #2
Master Member
 
Join Date: Jun 2007
Posts: 3,781
use strcmp

10char
the-alchemist is offline   Reply With Quote
Old 08-07-2010, 12:15 AM   #3
Senior Member
 
Join Date: Mar 2009
Posts: 1,329
while (fgets(line, LINE_LENGTH, file) != NULL) { printf("%s",line); if(strcmp( line,argv[2])==0 ){ count++;}
so my code is something like that after i use strcmp but it still doesn't work
__________________
i will be happy, i must be!
affinityseven is offline   Reply With Quote
Old 08-07-2010, 12:25 AM   #4
Master Member
 
MoxLotus's Avatar
 
Join Date: Aug 2007
Posts: 2,907
Pardon me with my noobness, try something like char array? And then do a loop through to check?
__________________
Cleverness is not wisdom
MoxLotus is offline   Reply With Quote
Old 08-07-2010, 12:39 AM   #5
Senior Member
 
Join Date: Mar 2009
Posts: 1,329
Pardon me with my noobness, try something like char array? And then do a loop through to check?
nah im a noob too.
er i myself is not too sure after i do the fgets it is stored in the line which is a char[100] but when i try comparing it always fail.

maybe u can give me some guide on how do i store it in a array and loop back and i will try. thanks
__________________
i will be happy, i must be!
affinityseven is offline   Reply With Quote
Old 08-07-2010, 08:15 AM   #6
Member
 
zeroxmxi's Avatar
 
Join Date: Mar 2007
Posts: 239
http://www.edcc.edu/faculty/paul.bla..._functions.htm

If you have access to string.h functions, you may want to try the strstr function if your goal is to test if a string exists inside another string.

Regards.
zeroxmxi is offline   Reply With Quote
Old 08-07-2010, 01:24 PM   #7
Senior Member
 
Join Date: Mar 2009
Posts: 1,329
http://www.edcc.edu/faculty/paul.bla..._functions.htm

If you have access to string.h functions, you may want to try the strstr function if your goal is to test if a string exists inside another string.

Regards.
thanks that helped alot.
but i have a problem how do i actually search for the exact words. cause when using strstr if the first str is asdfgh and the 2nd is asd it is actually still detected.
and any idea how to make it case sensitive and not?( for the search) thanks alot once again
__________________
i will be happy, i must be!
affinityseven is offline   Reply With Quote
Old 08-07-2010, 02:19 PM   #8
Master Member
 
MoxLotus's Avatar
 
Join Date: Aug 2007
Posts: 2,907
thanks that helped alot.
but i have a problem how do i actually search for the exact words. cause when using strstr if the first str is asdfgh and the 2nd is asd it is actually still detected.
and any idea how to make it case sensitive and not?( for the search) thanks alot once again
I think it will be easy to determine if its upper/lower case with the use of acsii code comparison.

if strstr doesnt help u solve the problem, write a custom function to do the job =).
__________________
Cleverness is not wisdom
MoxLotus is offline   Reply With Quote
Old 08-07-2010, 03:43 PM   #9
Senior Member
 
Join Date: Mar 2009
Posts: 1,329
I think it will be easy to determine if its upper/lower case with the use of acsii code comparison.

if strstr doesnt help u solve the problem, write a custom function to do the job =).
cause strstr actualy detect asdfg as true when my other string is asd. so is there any way that i can actually solve this?
and is there any function which i can use to seperate a string into smaller strings?
for example abd sdsd sdsfds
into abd,sdsd,sdsfds sperately
__________________
i will be happy, i must be!
affinityseven is offline   Reply With Quote
Old 08-07-2010, 04:31 PM   #10
Supremacy Member
 
fzhfzh's Avatar
 
Join Date: May 2008
Posts: 8,619
What you want is a string search algorithm.

Question is, for your txt file, is it assumed that there will only be ONE instance of "apple" or do you need to find the position of all instances of "apple"? If your data set is not very large, and you only need to find one instance of a string, then the char string looping brute force method is good enough.

If you just want to know if a string contains a string pattern, and don't mind it being not so efficient, you can just use this brute force method:

bool function bCheckString (char* cInput, char* cSearch, int iInputMax, int iSearchMax)
{
for(int i = 0; i < (iInputMax-iSearchMax); ++i)
{
for(int j = 0; (j < iSearchMax) && (cInput[i+j] == cSearch[j]); ++j)
{
if (j>=iSearchMax) return true;
}
}
return false;
}

A function typed out just like that, didn't test by compiling so check for errors by yourself.
fzhfzh is offline   Reply With Quote
Old 08-07-2010, 04:44 PM   #11
Supremacy Member
 
fzhfzh's Avatar
 
Join Date: May 2008
Posts: 8,619
If you want efficiency, read up on Boyer-Moore algorithm, which is one of the most efficient string search algorithms.
fzhfzh is offline   Reply With Quote
Old 08-07-2010, 05:07 PM   #12
Supremacy Member
 
Join Date: May 2000
Posts: 6,623
Try the below example

#include <stdio.h>
#include <string.h>

int main(void) {
FILE *fptr;
char fstr[6];
int count=0;

/* Open the file. If NULL is returned there was an error */
if((fptr = fopen("test.txt", "r")) == NULL) {
printf("Error Opening File.\n");
exit(1);
}

while( fgets(fstr, 6, fptr) != NULL ) {
if (strcmp(fstr,"apple")==0)
count++;
}

printf("%d apple string found in the test.txt.\n,count);

fclose(fptr);

}
megaweb is offline   Reply With Quote
Old 08-07-2010, 05:22 PM   #13
Supremacy Member
 
fzhfzh's Avatar
 
Join Date: May 2008
Posts: 8,619
Try the below example
It's not going to work, because that code reads characters 6 by 6, if the "apple" string starts at 2nd to 6th character, it will not be taken into account.
fzhfzh is offline   Reply With Quote
Old 08-07-2010, 08:59 PM   #14
Senior Member
 
Join Date: Mar 2009
Posts: 1,329
ok to be clear i need something like megaweb program.
i alr did it hlafway. i have a txt file which i read in from the command line, and usig fgets i read it line by line(done) den i have to search for all instances of a word of a word which is also entered in the command line , for this i used strstr() which failed cause it only detect the word once per line and that if the word is contained in a longer string , it will also be detected and also for strstr, it only detect the word once per line
-what i thinking of doing is spliting each line into different smaller string (probably with a delimeter) den using strcmp() to compare.
- or maybe i copy the line into a new string and use strncmp() ? not very sre about this though is cause when i do strlen i always get +2 characters behind, probably the space and the \0 which might be affecting my search.

anyway thanks for the help so far really appreciate it
__________________
i will be happy, i must be!
affinityseven is offline   Reply With Quote
Old 08-07-2010, 11:51 PM   #15
Master Member
 
MoxLotus's Avatar
 
Join Date: Aug 2007
Posts: 2,907
ok to be clear i need something like megaweb program.
i alr did it hlafway. i have a txt file which i read in from the command line, and usig fgets i read it line by line(done) den i have to search for all instances of a word of a word which is also entered in the command line , for this i used strstr() which failed cause it only detect the word once per line and that if the word is contained in a longer string , it will also be detected and also for strstr, it only detect the word once per line
-what i thinking of doing is spliting each line into different smaller string (probably with a delimeter) den using strcmp() to compare.
- or maybe i copy the line into a new string and use strncmp() ? not very sre about this though is cause when i do strlen i always get +2 characters behind, probably the space and the \0 which might be affecting my search.

anyway thanks for the help so far really appreciate it
sorry peronally i have nvr used strstr function b4. from ur explanation, it seems to return the result rite after a match is found and doesnt proceed further.

if u wanna split the a long string into words, try detecting the acsii code that represent the space. and store in another string array. Hope that helps
__________________
Cleverness is not wisdom
MoxLotus is offline   Reply With Quote
Reply
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ. Forum members and moderators are responsible for their own posts.

Please refer to our Terms of Service for more information.


Thread Tools

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On