Question: I have a **datas.txt** file :

format : name surname debt payment

```
bir bir 100 2 
iki iki 200 2 
eray alakese 100 5 
john doe 2000 10 

```

I'm learning C and **i know just simple file functions (fscanf, fprinf, fopen etc.)**

I'll

1. ask user name and surname with `scanf`and then assign them to *name* and *surname* variables.
2. It will search file for *name* and *surname* and then assign debt and payment to *debt*, *payment* variables ( `fscanf(file, "%s %s %d %d", name, surname, &debt, &payment);`)
3. Delete or modify this line

This is my source code .

```
    scanf("%s", &name);
    scanf("%s", &surname);
    file = fopen("datas.txt", "r");
    /* this fscanf() is working as expected. There is no problem. */
    fscanf(file, "%s %s %d %d", name, surname, &debt, &payment);

    /* modify and delete actions here */
    fclose(file);

```

Examples :

1. I want to delete record of "John Doe" .
2. I want to decrease "John Doe"'s debt to 100$

Answer: 
You cannot delete/modify[\*] individual lines of a text file; the only solution is to 1) create a new temporary file, 2) copy the contents up to, but not including, the line that you want modified/deleted, 3) output the modified line, 4) copy the rest of the original file, 5) replace the old file with the temporary file.

[\*] Modification is possible ONLY if the modified line has the same length as the original line.

EDIT: PS: Using fgets, followed by sscanf (or some other way of tokenizing the line) will save you much grief.
This is a little hard to do, because C's model of files, inherited from Unix (they were largely codeveloped), does not actually define a file as a list of lines. Instead, it defines a line as a string of bytes terminated with a newline, and a file (roughly) as a stored string of bytes of potentially limited length, where you can possibly skip to different parts. That's considerably vague, but bear with me. 

Whe problem becomes clearer when we try to translate our ideas - "modify this line", "delete that line" - into file operations. We can read a line by just stopping at a newline, but there's simply no command to cut it into sections; only to set the end (ftruncate()). So to change the size of the line, we need to copy all the data that follows it. It can be done, but it's very often easier to just create the file anew. Compare the subtleties of implementing memmove(). 

The traditional method to do this comes in two variants, depending on what side effects you can tolerate. 

One is to write your updated version in another file, and then rename() it into place. This has the advantage that the new file will be complete by the time you put it in place, but the downsides that it may not match the old file precisely as far as permissions etc go, and it won't be visible to other programs that already had the old one open. If two programs modify the file like this, it's a race condition as one of the changes gets overwritten by the other.

The other is to load the data completely and write the modified version down in place. This means the file itself remains in place, permissions and all, but there will be a duration while you're saving that it is a mix of old and new contents. Text editors tend to do this, often whilst saving the old contents as a separate file in case something goes wrong. 

There are tools to manage the side effects too, such as versioned filesystems, file locking, and even libraries prepared for parallel changes (metakit comes to mind). Most of the time we'll be using tools that are already around, like sed -i.