#include <stdio.h>

int main()
{
    FILE *file;
    char nameFile[32];
    char string[500];
    char ans[2];
    int choice;
    
    do{
        printf("What do you want to do?\n");
        printf("1. Write text to file\n");
        printf("2. Read text file\n");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                printf("Give name to the file (*.txt).\n");
                scanf("%s", nameFile); //This line skips me to line (I have no idea why :()
                system("clear");
                file=fopen(nameFile, "w");
                if(file!=NULL){
                    printf("Input text:\n");
                    scanf("%[^\n]", string);
                    fprintf(file, "%s", string);
                    printf("\n\t\t\t-----------Ended writing.------------\n");
                    fclose(file);
                }
                else
                {
                    printf("Could not open the file.");
                    return -1;
                }
                break;
            case 2:
                printf("Give name to the file (*.txt)");
                scanf("%s", nameFile);
                system("clear");
                file=fopen(nameFile, "r");
                if(file!=NULL){
                    while (!feof(file)) {
                        fscanf(file, "%s", string);
                        printf("%s\n",string); //After printing data from text file, adds instruction from line , and that is printing Error. How to get rid of it?
                    }
                }
                else{
                    printf("Could not open the file.");
                    return -1;
                }
            default:
                printf("Error.");
                break;
        }
        printf("Do you want to restart the program? (y/*)"); //Even if I write 'y', program ends anyway :(
        scanf("%s", ans);
    }
    while(ans=='y');
    return 0;
}