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

142 Views Asked by At
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?

3

There are 3 best solutions below

0
yokeshwaran On BEST ANSWER

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);

0
Eric Postpischil On
fgets(target_file, sizeof(target_file), stdin);
target_file[strcspn(target_file, "\n")] = '\0'

This code will not compile due to the missing semicolon at the end. Let’s assume the semicolon is added.

If fgets is successful, it puts a string of characters in target_file. Note this code is defective because it fails to get the return value of fgetsfgets may leave target_file indeterminate or unchanged if it encounters an error or end-of-file before reading any characters, in which case it returns a null pointer.

Suppose fgets does succeed, target_file contains a string, which means it contains zero or more characters followed by a null character. Also, if fgets read a new-line character, it is the last character in the string, just before the null character. If fgets filled the available space before reading a new-line character, there is no new-line character in target_file.

strcspn(target_file, "\n") examines target_file to see how many characters at its beginning do not contain any characters from "\n". If there is a newline character at index n in the array, then strcspn returns n because none of the characters from 0 to n−1 are a newline character. (fgets never returns a string with more than one newline character because it stops when it sees a newline character.) If there is no newline character in the array, then strcspn returns the length of the string, because it stops at the terminating null character.

Thus, the return from strcspn is either the index of the newline character (if one is in the array) or of the terminating null character (if there is no newline character).

Then target_file[strcspn(target_file, "\n")] = '\0'; sets this element in the array to a null character. So, if it is a newline character, it is changed to a null character, and the string is shorter and no longer includes the newline character. If it is a null character, there is no effect from assigning a null character to that element.

if(target_file == NULL)

This is useless code. If target_file is an array, it cannot be a null pointer. If target_file is a pointer (including a function parameter declared as an array but automatically adjusted to be a pointer), then it its use in fgets(target_file, sizeof(target_file), stdin); was a mistake for two reasons:

  • fgets should not be passed a null pointer, so the target_file == NULL would need to be before the fgets call.
  • sizeof(target_file) would give the size of the pointer, but what is needed there is the number of characters available at the place where the pointer points.
7
Vlad from Moscow On

For starters this sequence of statements

target_file[strcspn(target_file, "\n")] = '\0'
if(target_file == NULL){
    printf("enter any key to exit ....!\n");
    exit(EXIT_FAILURE);
}

does not make great sense. target_file can not be a null pointer except initially before these statements it was already a null pointer. But if it is a pointer to a dynamically allocated array and is not a null pointer then using the operator sizeof in the preceding statement

fgets(target_file, sizeof(target_file), stdin);

again is invalid. It makes sense to use the sizeof operator here if target_file is declared as a character array not as a pointer.

Instead you could write for example

if ( fgets(target_file, sizeof(target_file), stdin) == NULL )
{
    printf("enter any key to exit ....!\n");
    exit(EXIT_FAILURE);
}
target_file[strcspn(target_file, "\n")] = '\0';

As for the function strcspn then firstly bear in mind that the function fgets can append to the read string the new line character '\n' that corresponds to the pressed key Enter and that you need to remove from the string and according to the function descriiption (the C Standard, 7.24.5.3 The strcspn function)

2 The strcspn function computes the length of the maximum initial segment of the string pointed to by s1 which consists entirely of characters not from the string pointed to by s2.

So either the function returns the length of a string before the new line character '\n' and thus you can change it to the zero terminating character '\0' excluding the new line character from the string or it returns the length of the whole string if the new line character is absent and setting the zero-terminating character (the value of the length is equal to the position of the terminating zero character in the string) to zero-terminating character keeps the string as is.