3
I want to know how to read a file in c++. The first line has the size of the matrix and the other one has the elements of the matrix. Each element is separated by a blank space, there may be negative numbers. Lines end with \n
.
I did it but it hasn’t worked out so 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");
}
If you’re going to use this to work and not to study, learn how to work with a database (MS Windows: SQL, Cross-Platform: Mysql (Oracle). http://msdn.microsoft.com/pt-br/library/ktheed7h(v=vs.90). aspx (translation option above) and http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-info.html
– user2692
@Lucashenrique for study purposes only
– Macario1983
@Lucashenrique I don’t see how Bds would be a good alternative to a problem involving matrix manipulation.
– C. E. Gesser
@C.E.Gesser does not mean matrix manipulation, but data search. It is usually done in binary... Learning to deal with databases is very important :)
– user2692
It is certainly important, just as it is important to know the best tool for each problem. By the description probably after reading the matrix will be necessary some kind of manipulation, and for that it is good to have everything in memory even.
– C. E. Gesser
@C.E.Gesser, I’m going to have to parallelize the Jacobi method by calculating the inserted matrix
– Macario1983