Being more specific, I want to merge two sorted lists of integers into a third list that is also sorted. I can't figure out why my code isn't working! Here's some example of entries and the code I made:

File1:

```
1
2
3
4
5
6

```

File2:

```
7
8
9
9
9
10

```

C code:

```
#include <stdio.h>
#include <stdlib.h>

void merge(char* arq1, char* arq2){
    FILE* final;
    final = fopen("merge.txt",'w');
    FILE* f1 = fopen(arq1,'rt');
    FILE* f2 = fopen(arq2,'rt');
    if(!f1 || !f2) exit(1);
    // n1 and n2 represents the actual value that's been read. r1 and r2 the status of each file.
    int n1,n2,r1,r2,equal;
    r1 = fscanf(f1,"%d",&n1);
    r2 = fscanf(f2,"%d",&n2);
    while(r1 || r2){
         if((!r1) || (n2 < n1)){
            fprintf(final,"%d ",n2);
            r2 = fscanf(f2,"%d",&n2);
        }else if((!r2) || (n1 < n2)){
            fprintf(final,"%d ",n1);
            r1 = fscanf(f1,"%d",&n1);
        }else{
            equal = n1;
            fprintf(final,"%d ",equal);
            while(n1 == equal) r1 = fscanf(f1,"%d",&n1);
            while(n2 == equal) r2 = fscanf(f2,"%d",&n2);
        }
    }
    fclose(final);
    fclose(f1);
    fclose(f2);
}

int main(int argc, char const *argv[]){
    merge("merge1.txt","merge2.txt"); 

    return 0;
}

```

The algorithm is supposed to work as follows:

While at least one of the files still has content, its content would be then saved at the `merge.txt` file. If file 1 and 2 would possess the same content, I'd save the content and make the 2 files read until they find a different value so that the merged file does not have repeated values.

How do I merge two sorted files into one sorted file?