Буфер ввода flu sh действует по-разному при ручном вводе и вводе файла? - PullRequest
0 голосов
/ 03 мая 2020

Я работаю над программой для сортировки разреженных матриц (условия не имеют отношения к вопросу). Дело в том, что мне нужен код для запроса нового ввода, если это не матрица N * 6, состоящая из целых чисел. Я использовал fseek для flu sh входного буфера, но он ведет себя по-другому, когда я ввожу себя, в отличие от перенаправления файлов. При вводе (например) «1 e» самостоятельно, он печатает сообщение об ошибке и ждет нового ввода; при использовании файла, содержащего «1 e», он бесконечно печатает сообщение об ошибке. Почему это происходит, и как я могу это исправить?

Вот код:

#include <stdio.h>
#define COLUMNS 6

int main()
{
    int  index, nonzeroes_column[COLUMNS]={0}, row_nonzeroes, error, finish=0, total_zeroes, total_nonzeroes, reset_index, current_row[COLUMNS]={0};
    for (1;1;1){
        total_nonzeroes=0, error=0, total_zeroes=0;
    /*the while condition is a way to give us control over the options*/
        while (error==0 && finish==0){
            /*this for loop will reset after completing one row of the matrix, then start over, as long as the while loop hasn't been instructed to stop*/
            for (index=0, row_nonzeroes=0;index<COLUMNS;index++){
                if (scanf("%d", &current_row[index]) != 1) { /*failed input*/
                    error=1;
                    for (reset_index=0; reset_index<COLUMNS; ++reset_index){ /*reset the count before starting over*/
                            nonzeroes_column[reset_index]= 0;
                    }
                    printf("Illegal input. Please enter input from start\n");
                    fseek(stdin, 0, SEEK_END);
                    break;
                    }
                else if (current_row[index]>0){ /*Here, we tally the non-zero entries, and if they are arranged in such a way that the matrix will not be sparse, tell the user so and get set to receive input again*/
                    ++nonzeroes_column[index];
                    ++row_nonzeroes;
                    ++total_nonzeroes;
                    if(row_nonzeroes>2 || nonzeroes_column[index]>2){
                        finish=1;
                        printf("Matrix not sparse\n");
                        break;
                    }
                }
                else if (current_row[index]==0){
                    ++total_zeroes;
                }
                else{/*negative value; this signifies the end of the input stream*/
                    if (((total_nonzeroes+total_zeroes)%COLUMNS) != 0 || (total_nonzeroes+total_zeroes)<COLUMNS){ /*meaning that there are not 6 columns in each row*/
                        error=2;
                        for (reset_index=0; reset_index<COLUMNS; ++reset_index){ /*reset the count before starting over*/
                            nonzeroes_column[reset_index]= 0;
                    }
                        printf("Matrix row not full. Please enter input from start\n");
                        fseek(stdin, 0, SEEK_END);
                        break;
                    }
                    else if (total_nonzeroes>((total_nonzeroes+total_zeroes)/3.0)){
                        printf("Matrix not sparse\n");
                        finish=2;
                        break;
                    }
                    else{
                        printf("Matrix is sparse\n");
                        finish=3;
                        break;
                    }
                }
            }
        }
            if (finish != 0)
            break;
    }


    return 0;
}
...