#include <stdio.h>
#include <string.h>
void main() {
    FILE* fp;
    char line[1024];
    char filename[1024];
    int length;
    int counter=0;
    while (1) {
       // fopen(filename, "w");
        int op = 0;
        printf("1. new file");
        scanf("%d", &op);
       
        switch (op) {
        case -1: break;
        case 1:
            
            printf("filename>>");
            scanf("%s", filename);
            length = strlen(filename);
            printf("length = %ld \n", strlen(filename));
            filename[length] = '.';
            filename[length + 1] = 't';
            filename[length + 2] = 'x';
            filename[length + 3] = 't';
            filename[length + 4] = '\0';
          
            fp=fopen(filename, "w");
            while (fgets(line, sizeof line, stdin) != NULL)
            { 
                if (line[0] == 'q' || line[0] == 'Q')
                {
                    printf("end of line \n");
                    fclose(fp);
                    break;
                }
                else
                    fputs(line, fp);
            }
        }
    }
}
I made this code to make text file with what I want. but the problem is, when I run this program, it writes input starting from the second line, not the first.
Why is this happening? I want to write input starting from the first line. How to change this code to write on the first line?
 
     
     
    