-2

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.

Sakthivel
  • 61
  • 7
  • What code have you tried writing so solve your problem? We can help you with problems with your code, but we aren't here to write it for you. – Thomas Jager Aug 17 '18 at 12:39
  • Copy the File.txt line by line to a temporary file and after each line containing "ddddd" write also "MMMM NNNN" to the temporary file. At the end delete File.txt and rename the temporary file to File.txt. That's the logic. Now it's up to you to implement it. – Jabberwocky Aug 17 '18 at 12:43
  • As you can see from the comments and the answer, people here are very unclear about which part of your project is giving you trouble. Is it about copying a file line by line? Is it about detecting the key string? Is it about writing the new string? Those things seem trivial to experienced programmers, a few of them, except one, are also simple to you. But nobody here knows which one is giving you trouble. Show the code you have, which almost but not quite achieves your goal and then highlight the part which you cannot do, or the part where you suspect the problem is caused. – Yunnosch Aug 17 '18 at 13:07
  • Also, you should avoid the impression of mistaking StackOverflow for a free code writing service, because it is a serious risk of being downvoted and - more importantly - being ignored. That, too, can be avoided by showing your almost working code. – Yunnosch Aug 17 '18 at 13:08
  • sorry for missed to add the code that I'm working. Now, I have updated code and would like to know the possibilities of updating existing file instead of creating new file. – Sakthivel Aug 17 '18 at 16:05
  • If you don't want to create a new file, you'll first need to read the entire file into memory, then process the contents and write to the file. – dbush Aug 17 '18 at 16:15
  • See [Write in the middle of a binary file without overwriting any existing content](https://stackoverflow.com/questions/10467711) for how to insert data into the middle of a file. Then decide it is a bad idea and make a new file with extra information. – Jonathan Leffler Aug 17 '18 at 16:53

1 Answers1

0

You need to "spool" the file. That is, open the file for reading, open a new file for writing, read from the file, "do your stuff" and write to the new file, close the files, delete the old one and rename the new one to the old one. Coding this you must do yourself.

Note that a text file is essentially a sequential file. It means that, would you have opened the file in read/write mode, you cannot "insert" data because that would overwrite other existing data.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Thanks for your response. But, I'm trying to change existing file itself. if there any way to do in C ? – Sakthivel Aug 17 '18 at 16:06
  • No, you can't. Changing the "existing file" is done by replacing it. (The rename at the end of my proposal.) The file is an entry in the filesystem. After the procedure a file with the same name exists in the file system. See update. – Paul Ogilvie Aug 17 '18 at 16:17