I need a suggestion to find and write strings in a file using c program.
For example, File.txt has following contents
aaaaa bbbbb
ccccc ddddd
eeeee fffff
ggggg
Here, I want to search a string "ddddd" and write new string ("MMMM NNNN") after this line like
File will have following contents after addition of new string,
aaaaa bbbbb
ccccc ddddd
MMMM NNNN
eeeee fffff
ggggg
Following is the example Code that I'm trying to make it work and still working on
int main(int argc, char *argv[])
{
------
------
/* Opening a file based on command line argument*/
fptr = fopen(argv[1], "rw");
while(fgets(buf, buflen, fptr))
{
------------
{
/*Checking the key string "ddddd" and if presents then have to add "MMMM NNNN" in immediate next line*/
if (strstr(buf, "ddddd"))
{
printf("Found Matching for : %s\n", argv[3]);
fprintf(fptr, "\n%s\n", "MMMM NNNN");
}
}
}
----------
}
Is there any way to update the existing file without creating new file ?
Thanks in advance for your responses.