Search code examples
cinputfile-ionewlinefgets

Why is my conditional not working?using fgets output


I need a method for getting each input characters and putting them in a 2D array (in this case a char array).

But as I need this from file input that has several end of lines (\n), I don't want these to be inserted into the array, so I was trying to make a conditional that if the character is a newline, it should not be placed in the array, but it doesn't seem to work.

Just to make clear, I am trying make this method by myself because I didn't find one online, and fgets was the best method I found.

My code:

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

int main(int argc, char *argv[]) {

    char words[4][4][2];
    char verif[2];
  
    int i, j, l = 0, lin = 4, col = lin;

    printf("Insert numbers: ");
    for (i = 0; i < lin; i++)
    {
        for (j = 0; j < col; j++)
        {
            fgets(verif, 2, stdin);
            //since I was using the IDE to write I also put a condition not to add the enter for the input but it doesn't work too lol

            if (strcmp(verif, "\n\0") != 0 || strcmp(verif, "\r\0") != 0)
            {
                printf("copying...\n");
                printf("%s\n", verif);
                strcpy(words[i][j], verif);
                l++;
            }
            else
                j--;
        }
    }

    printf("\n");
        
    for (i = 0; i < lin; i++)
    {
        for (j = 0; j < col; j++)
        {
            printf("-%s\t", words[i][j]);
        }
        printf("\n");
    }

    return 0;
}

the input I would use to be placed in the matrix:

1234
1234
1234
1234

With a normal input like 1234123412341234 the output (printf matrix) is ok:

-1  -2  -3  -4  
-1  -2  -3  -4  
-1  -2  -3  -4  
-1  -2  -3  -4

Solution

  • fgets(verif, 2, stdin) is a contorted way to read a single byte from the standard input. You could just write int ch = getchar(); and handle the byte value directly instead of using string comparisons.

    Yet the main problem lies in the test if (strcmp(verif, "\n\0") != 0 || strcmp(verif, "\r\0") != 0): you should use && instead of || as you want to handle input that is neither a newline nor a carriage return, which translates to (not a newline) and (not a carriage return).

    Here is a simpler version:

    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        char matrix[4][4];
        int lin = 4, col = lin;
    
        printf("Enter the digits: ");
        for (int i = 0; i < lin; i++) {
            for (int j = 0; j < col;) {
                int c = getchar();
                if (c == EOF) {
                    fprintf(stderr, "premature end of file, aborting.\n");
                    return 1;
                }
                if (c != '\n' && c != '\r') {
                    printf("inserting %c\n", c);
                    matrix[i][j] = c;
                    j++;
                }
            }
        }
    
        printf("\n");
            
        for (int i = 0; i < lin; i++) {
            for (int j = 0; j < col; j++) {
                printf("-%c\t", matrix[i][j]);
            }
            printf("\n");
        }
    
        return 0;
    }