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: 
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.
The way I usually handle something like this is to write a function that can "read in" your data and store it to some structure. Then a function to write data from the structure to a file.

This way you can just manipulate the data in an array. This also makes your program more extensible to doing things like sorting, or extra math that you couldnt have done by just writing over the top of the file.

e.g. try writing a function that can read in to a struct like:

```
struct Client
{
    char name[255];
    double owes;
    double paid;
}

```

What you then do make an array of these structures and manipulate those.
You'll learn a lot about structs, dynamic memory allocation, and you'll no doubt run in to some interesting issues that will help you learn.

My advice is to also skip C and go for C++... learning this stuff using iostreams instead of the \*printf/\*scanf functions and vectors is probably going to be better for you in the long run