william007
asked on
Finding standard code for string operation
Hi, I wish to find the code online for
1) int find(wholeString, searchString)
find will be finding partString in the searchString, if match it will return true
let say searchString=abc
wholeString=abcde
it will return true
(Exclude the use of regular expression, only use of the standard string expression)
2)int findtextinfile(file, searchString)
findfile will find the searchString in the file, if match, it will return true,
but note that the it should not load the entire file in the memory, it should match the string with parts of the file in the memory at anytime
(Exclude the use of regular expression, only use of the standard string expression)
Note that above function signature just for illustration purpose, they are optional to look like this. Since there are fairly standard function, I think there should be many samples online, hence I do not need to reinvent the wheel. The only thing is, I don't know where to find it. Thanks for any suggestion:)
1) int find(wholeString, searchString)
find will be finding partString in the searchString, if match it will return true
let say searchString=abc
wholeString=abcde
it will return true
(Exclude the use of regular expression, only use of the standard string expression)
2)int findtextinfile(file, searchString)
findfile will find the searchString in the file, if match, it will return true,
but note that the it should not load the entire file in the memory, it should match the string with parts of the file in the memory at anytime
(Exclude the use of regular expression, only use of the standard string expression)
Note that above function signature just for illustration purpose, they are optional to look like this. Since there are fairly standard function, I think there should be many samples online, hence I do not need to reinvent the wheel. The only thing is, I don't know where to find it. Thanks for any suggestion:)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi William,
Is this a homework assignment?
sunnycoder
Is this a homework assignment?
sunnycoder
ASKER
There are just the function that I needed for doing some search function. If there are assignment, there are just the assignment that wrote by me:)
For the find function, we can easily find a counterpart in other language.
For the findinfile function, I have done it once in other language...but found out that it needs lots of testing to got it right, especially the tricky part which is considering the joining part of two different buffer. Some parser in java has done it perfectly, like SAX XML parser.
I am not asking anyone to write it from scratch...just that if there are codes that available online, most likely is being tested,. We can use it directly, without wasting time to rewrite everything, and to test everything. But since I am new in C, I don't really know where to locate such "open source" resource.
Else, I can write it by myself also:)
For the find function, we can easily find a counterpart in other language.
For the findinfile function, I have done it once in other language...but found out that it needs lots of testing to got it right, especially the tricky part which is considering the joining part of two different buffer. Some parser in java has done it perfectly, like SAX XML parser.
I am not asking anyone to write it from scratch...just that if there are codes that available online, most likely is being tested,. We can use it directly, without wasting time to rewrite everything, and to test everything. But since I am new in C, I don't really know where to locate such "open source" resource.
Else, I can write it by myself also:)
ASKER
Also, there are chances that C has provide such a function like ozo mentioned. Only a only newbie like me will write such an assignment:)
>especially the tricky part which is considering the joining part of two different buffer.
Would the "searchString" span multiple lines? i.e. would there be a \n as part of search string? If no, then this simple function would do
int findtextinfile(fileptr, searchString)
{
char buffer[BUF_LEN] = { 0 };
while ( fgets(buffer, BUF_LEN, fileptr))
{
if ( strstr(buffer, searchString))
return 1;
}
return 0;
}
Would the "searchString" span multiple lines? i.e. would there be a \n as part of search string? If no, then this simple function would do
int findtextinfile(fileptr, searchString)
{
char buffer[BUF_LEN] = { 0 };
while ( fgets(buffer, BUF_LEN, fileptr))
{
if ( strstr(buffer, searchString))
return 1;
}
return 0;
}
ASKER
1. What is {0}? is it an expression to initialize all the char array value to be null?
2. I agree this is a solution for simple environment, but what I need is more than that:
In some circumstances, some ppl may send in xml file at 70MB - 700MB sizes without space in between; This will not be practical in handling in line since it will greatly slow down the process by dumping all the buffer into the memory. So the algorithm is something like this:
a)Get the characters in a fixed size of buffer;
b)compare that buffer with the string;
c)If not found, using the last of (searchstringlength - 1) chars as an residual for the first buffer, concat with the second buffer, and compare against the search string;
d)If found, return 1(simple cases).
To be more advance,
findtextinfile can be called multiple time, each time it will return a file pointer that matched, else return -1 if EOF met, so we can called it like this.
2. I agree this is a solution for simple environment, but what I need is more than that:
In some circumstances, some ppl may send in xml file at 70MB - 700MB sizes without space in between; This will not be practical in handling in line since it will greatly slow down the process by dumping all the buffer into the memory. So the algorithm is something like this:
a)Get the characters in a fixed size of buffer;
b)compare that buffer with the string;
c)If not found, using the last of (searchstringlength - 1) chars as an residual for the first buffer, concat with the second buffer, and compare against the search string;
d)If found, return 1(simple cases).
To be more advance,
findtextinfile can be called multiple time, each time it will return a file pointer that matched, else return -1 if EOF met, so we can called it like this.
ASKER
*so we can called it like this
We can manipulate it in a while loop.
We can manipulate it in a while loop.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks=)
strstr(..) will return pointer to the located string if found or otherwise null pointer. so you can if and else or as given by ozo to make the function return 1 or 0 OR TRUE or FALSE etc values as you wish.
Thanks