У меня есть функция, которая принимает поток файлов и считывает целые числа, хранящиеся в этом файле, в 1D-матрицу. Проблема, с которой я сталкиваюсь, заключается в том, что мой fscanf последовательно возвращает 0, а не 1, как я ожидаю. Я знаю, что начало моего файла отформатировано правильно и, как ожидалось, но я не могу понять, почему он не будет читать самую первую строку. Что я делаю не так?fscanf постоянно возвращает 0, когда он должен возвращаться 1
/* FUNCTION: readToMatrix
DESCRIPTION:
This takes an input stream and reads the file (as described in the header documentation),
filling the array with the integers contained in the input file stream.
INPUTS:
file stream, int *array, matrix width
OUTPUTS:
Writes to array
RETURN:
Returns 0 on success, nonzero on an unexpected failure.
*/
int readToMatrix(FILE *input, int *array, size_t matWidth)
{
int x,y;
long num;
for (y = 0; y < matWidth; ++y)
{
for (x = 0; x < matWidth-1; ++x)
{
// if fscanf doesn't read 1 number or if EOF then return
if (fscanf(input, "%ld,", &num) != 1 || feof(input)) return -1;
array[x + y*matWidth] = num;
}
if (fscanf(input, "%ld ", &num) != 1 || feof(input)) return -1;
array[x + y*matWidth] = num;
}
return 0;
}
ПРИМЕЧАНИЕ. Ниже приведен короткий фрагмент начала входного файла.
12177,12690,12499,12985,13005,12574,12882,12896,13026,14539,13704,13539,15182,14361,14539,15333,14615,15231,
Где вы открыть файл? – ameyCU
Файл был открыт в вызываемой функции. – LTClipp
Какой вызов fscanf? – immibis