ccharnewlinec-stringsfgets

how the null value '\0' assign in the exacted \n index?


printf("enter destination file name to print : ");
    fgets(target_file, sizeof(target_file), stdin);
    target_file[strcspn(target_file, "\n")] = '\0'
    if(target_file == NULL){
        printf("enter any key to exit ....!\n");
        exit(EXIT_FAILURE);
    }
    
    writting_file = fopen(target_file, "w");

    if(writting_file == NULL){
        printf("error to open the file\n");
        printf("please enter any key to EXIT.......!\n");
        exit(EXIT_FAILURE);
    }
;

** target_file[strcspn(target_file, "\n")] = '\0' **

how that change in the exact index?


Solution

  • this error was accurate by using of before scanf statement. that statement add a new line in the end of the line so, can modifiy the scanf to skip the newline character.

    scanf("%d*c",a);