Hi all,
i have a problem.. i want to search a string say "abc" from a file and replace this string with say "xyz" can anyone help me to do this....please gimme some tips or sample codes...
thanks....
Hi all,
i have a problem.. i want to search a string say "abc" from a file and replace this string with say "xyz" can anyone help me to do this....please gimme some tips or sample codes...
thanks....
easiest way (for not to big files
read the whole file into buffer
append 0 at the end
use strstr to locate the string
replace it with another string
write buffer back to file
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Thanks vart,
i have already done the same thing.... but i am not able to get how to replace the string..... i have used fgets() and strstr()... can u please gimme a simple code for this...
strstr() returns a pointer to a location within the string. To replace it, you'd do a memcpy() or loop through one char at a time from apointer[0] - apointer[strlen() - 1].
hope - this will help
Code:#include <stdio.h> #include <string.h> int main() { char buffer[]="123abc789"; char* p = strstr(buffer,"abc"); if(p) { strncpy(p,"456",3); } printf("%s",buffer); return 0; } /* My output 123456789 */
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
vart, shame on you for doing compile's homework!!!
Thanks vart,
and thanks kennedy too....
kennedy this is not a home work... and i am a professional in electronics and communications... but i am a begginer in "C"... thats y asking help with u people..hope u will make me a good "C" programmer too...
Hi friends once again some clarifications....
in the above program.... if the search string is say "gram" and if that string is not there but if there is a string "program" then it says the string found... how to overcome this. .that is adding a space before and after the string.. ???
thanks..
NOTE: you will have check for the return value str that is *p;Code:#include <stdio.h> #include <string.h> int main() { char str[] = "This is a test pro gram"; char search[] = "gram"; char *p; p = strstr(str,search); printf("Serach string -> %s\n",str); if((*(p-1) == ' ') && (*(p + strlen(search)) == ' ' || *(p + strlen(search)) == '\0')) printf("Result : String found\n"); else printf("Result : String not found\n"); getchar(); return 0; } /* my output Serach string -> This is a test program Result : String not found Serach string -> This is a test pro gram Result : String found */
Hope this will help
ssharish2005
Last edited by ssharish2005; 01-12-2007 at 06:49 AM.
Well until you match at the start of a string at least.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
wow thannks ssharish2005,
thanks a lot.....
but one more doubt struck my mind..... say i have a very large text file.... and i want to search a string and replace there.... without making one more copy of the file.. how to do this...
thanks...
You'll likely still have to create a new output file with your modified output. Then delete the old one and rename the new one.
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
oh.. fine.. but why cant we search and replace directly in a file itself ???
thanks....
You can:
1) If the string you are replacing is the same length.
2) If the string you are replacing is longer than the one you are inserting you'd have to move all of the remaining chars in the file up to the offset (it'd be a lot of fseek()ing but you _could_ do it).
3) If the string you are replacing is shorter than the one you are inserting you'd have to move the EOF marker (which is already cleared) and insert the string keeping a short LIFO buffer to advance yourself through the file (again, lots of fseek()ing).
If you notice, two of the three options above, where doable, aren't the smartest things in the world. Coping a file to memory (assuming that the file will fit into memory) then modifing it and returning it to a newly written file is MUCH easier. Even file-to-file modifications are much prefered.