What is the preferred method to search for a string and then copy only part of the string? For instance. I want to search for a string called:
"@foobar@"
I only want to copy the "foobar" part.
Thanks!
Flight
What is the preferred method to search for a string and then copy only part of the string? For instance. I want to search for a string called:
"@foobar@"
I only want to copy the "foobar" part.
Thanks!
Flight
>> preferred method
The preferred method is one that you are comfortable using, and does it's job correctly and efficiently.
You can use strstr() to search the string.
There is no need to actually copy from the array you're searching, as you already know what that text is.
There are other ways...Code:#include <stdio.h> #include <string.h> int main(void) { char name[] = "@hammer@"; char foo[BUFSIZ]; if (strstr(name, "hammer")) { strcpy (foo, "hammer"); puts(foo); } return(0); }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
If all you want is a string between @'s you can do something along the lines of this:
Code:Tue Sep 23 7:18:37pm sangut: ~/misc > cat rp.c #include <stdio.h> #include <string.h> char *copy(char *string, char *buffer) { size_t a; size_t b; a = strspn(string, "@"); b = strcspn(string + a, "@"); strncpy(buffer, string + a, b); buffer[b] = '\0'; return buffer; } int main ( ) { char buffer[7]; copy("@foobar@", buffer); puts(buffer); return 0; }
The information given in this message is known to work on FreeBSD 4.8 STABLE.
*The above statement is false if I was too lazy to test it.*
Please take note that I am not a technical writer, nor do I care to become one.
If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
Don't assume that I'm ever entirely serious or entirely joking.
I believe you can use the strstr() function...
char *strstr(const char *string, const char *substring)
char *myString = "@foobar@";
char *target = "foobar";
char *result
result = strstr(myString, target) -- try that.
if the substring is not found, it will return NULL.
John
If you want to copy everything between the @ and the @, here is another idea.Code:#include <stdio.h> int main(void) { const char text[] = "@foobar@"; char found[ sizeof text ]; if ( sscanf(text, "@%[^@]@", found) == 1 ) { puts(found); } return 0; } /* my output foobar */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Posted more for fun than anything else...If you want to copy everything between the @ and the @, here is another idea.
Code:#include <stdio.h> char *foo(const char *input, char delim, char *output, size_t outsize) { /* Returns NULL if 2 delimiters aren't found or we run out of room in the output buffer */ char *p = output; while (*input && *input != delim) input++; while (*input && *++input != delim && --outsize) *output++ = *input; *output = '\0'; if (*input != delim) p = NULL; return p; } int main(void) { char name[] = "@hammer@"; char bar[BUFSIZ]; printf ("%s --> %s\n", name, foo(name, '@', bar, sizeof(bar))); return(0); }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
funny...and thx hammer
I guess I was tired when I originally wrote this. What I really need to do is be able to do the following:
1. Search for multiple occurances of a string.
2. Each string that I ultimately want to write to an output file will be on a line that immediately follows a line with the string "@some_name@".
for example: //@some_name@
// process_data
where "process_data" is the string I need.
3. I would then need to be able to search for other strings that would have different names and write those to a file as well.
Thanks!
Flight
You aren't posting much code. I, and possibly others, may be less inclined to offer assistance if you don't show much of an effort.
However, the following should demonstrate some sort of starting point.Code:/* * @DESCRIPTION@ * Print the line following a line containing at least one keyword. */ #include <stdio.h> #include <string.h> /* @FUNCTION@ */ void foo(const char *line) { fputs(line, stdout); /* or do further processing */ } /* @FUNCTION@ */ void bar(const char *filename, const char *keyword[], size_t size) { FILE *file = fopen(filename, "r"); if ( file != NULL ) { char line[BUFSIZ]; int printnext = 0; while ( fgets(line, sizeof line, file) != NULL ) { size_t i; if ( printnext ) { foo(line); /* keyword found in previous line */ printnext = 0; } for ( i = 0; i < size; ++i ) { if ( strstr(line, keyword[i]) != NULL ) { printnext = 1; /* found keyword in current line */ break; } } } } } /* @FUNCTION@ */ int main(void) { const char *keyword[] = { "@FUNCTION@", "@DESCRIPTION@" }; bar(__FILE__, keyword, sizeof(keyword)/sizeof(*keyword)); return 0; } /* my output * Print the line following a line containing at least one keyword. void foo(const char *line) void bar(const char *filename, const char *keyword[], size_t size) int main(void) bar(__FILE__, keyword, sizeof(keyword)/sizeof(*keyword)); */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Dave, you're right I apologize. I did previously post code although I have changed it quite a bit. The only question I really need answered is:
When I locate the string I want to copy, how do I copy it and into what?
>>When I locate the string I want to copy, >>how do I copy it
Methods as above, including strcpy() etc.
>>and into what?
Whatever you like, but it'll have to ultimately be another char array.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I have tried to develop the following algorithm for my proble, but i need help putting it into codes. I was told by a prof that i can achieve it with 8-10 lines of codes.
Then i tried, but i don't think my code's right, cuz it didn't work.
- Use char buf[BUFSIZ]
-Proceed to read each line from stdin, using fgets in a while loop
-Look through each character in the line for the string i want
-Using strncmp, check if you find a match for the old string
-if the "old" string is found
- print out the "new" string in its place
-increase buf by number of characters in the "new" string
-else if you can't find a match, just print the characters.
Now here's how i put it into code and it won't work.
Help please!!
Thanks a lot people.
I had posted my problem before, it's just not working yet. I need to replace an old string with a new one from a line as read in from stdin, onto the stdout.
M