-
strstr and '\0'
in the following function
If the line has at least two instances of the search string starting at or after the user-supplied interger offset, dump the part of the line between but NOT including the first two instances to the output file along with a '\n'.
Code:
void FileSearch(char *filename, char *OutFile, char *SearchString, int StartSrch)
{
char *ptr1,*ptr2,Line[MAXLEN];
FILE *fpIn, *fpOut;
int Len;
fpIn = Fopen (filename, "r");
fpOut = Fopen (OutFile, "w");
Len = strlen(SearchString);
while (fgets(Line,MAXLEN, fpIn) != NULL)
{
ptr1 = strstr(Line + StartSrch, SearchString);
if(ptr1)
{
ptr2 = strstr(ptr1 + Len,SearchString);
if(ptr2)
{
*ptr2 = '\0'';
ptr1 += Len;
}
}
else ptr1 = Line;
fputs(ptr1, fpOut);
}
Fclose(fpIn);
Fclose(fpOut);
}
If the fputs prints up to the '\0' and I write *ptr2 = '\0'
it continues printing the next line on the same line.
also I have a “wrapper.h”
for the Fopen and Fclose cases.
-
shouldn't you put ptr1 += Len + 1; then?
Also, that is a fancy while loop indentation you got going on there, bud.
-
ok that takes care of the space in the begining,
but it still prints the remainder of what is left after the second SearchString on the next line.
and thanks
I like fancy, bud
-
I am not entirely understanding the point of the second '\0', actually. You just skip past it anyway. The lovely Elysia wrote these maybe you should check them out.
-
The reason I put the *ptr2 = '\0'';
was to get fputs to stop printing before the second SearchString
dump the part of the line between but NOT including the first two instances.
-
If I understood what you wanted correctly, this function I put together should do the trick.
Code:
int Con_Dump(char * src_buffer, int offset, char * delimiter, char * ret_buffer)
{
if (!src_buffer || !delimiter) return -1;
int i;
char * first = strstr(&src_buffer[offset], delimiter);
if (!first) {
return -1;
}
first += (strlen(delimiter));
char * second = strstr(first, delimiter);
if (!second) {
return -1;
}
i = 0;
while ( i + first < second) {
ret_buffer[i] = first[i];
i++;
}
ret_buffer[i] = '\0';
return 0;
}
Hope that helps.
-
Thanks,
I will run this around a few times and see what happens
-
tsk tsk tsk this looks like one of the assignments from class, no no no dNNNy think i know who u are
-
Matus, are you in his class or are you his professor? Either has some potential hilarious factor.
-
ehhhhhhhh wrong
the correct response would have been
Code:
void FileSearch(char *filename, char *OutFile, char *SearchString, int StartSrch)
{
char *ptr1,*ptr2,Line[MAXLEN];
FILE *fpIn, *fpOut;
int Len;
fpIn = Fopen (filename, "r");
fpOut = Fopen (OutFile, "w");
Len = strlen(SearchString);
while (fgets(Line,MAXLEN, fpIn) != NULL){
ptr1 = strstr(Line + StartSrch, SearchString);
if(ptr1){
ptr2 = strstr(ptr1 + Len,SearchString);
if(ptr2){
ptr2[-1] = '\n';
*ptr2 = '\0';
ptr1 += Len + 1;
}
}
else ptr1 = Line;
fputs(ptr1,fpOut);
}
Fclose(fpIn);
Fclose(fpOut);
}
-
If one were using strtok() correctly... the correct response would have been not asking a question at all.
-
there is are are many roads leading to the same location.
you are quite the helpful bee
c+ expert
-
you have a lot of criticism by no helpful code!
take your own advise and go roll around in the grass somewhere.
C+ expert
-
My code does exactly what you asked. Takes a string and returns the characters between the substring and allows you to pick an offset.
You can add the newline character if you want at the end.
-
YES it did work
MeThoDz
and I thank you for your help.
That master5001 guy seemed like he was more interested in putitng people down than helping
again thank you