I want to know how to read a file in c ++. The first row has the size of the array and in the rest has the array elements. Each element is separated by a space, and there may be negative numbers. Rows end with \n
.
I did this but it has not worked very well.
int main() {
FILE *file = NULL;
int *array = NULL;
int matrixSize = 0;
int arraySize = 0;
char linha[255];
fopen_s(&file, "matriz.txt", "rt");
rewind(file);
fscanf_s(file, "%d", &matrixSize);
printf("%d", matrixSize);
arraySize = matrixSize * matrixSize;
alocateArray(&array, arraySize);
cleanArray(array, arraySize);
int i = 0;
while(!feof(file)) {
fgets(linha, 255, file);
char *valores = strtok(linha, " ");
while(valores != NULL) {
//array[i++] = (int) valores;
printf("%s", valores);
}
}
fclose(file);
system("PAUSE");
}