좋아요. 제 할당은 파일에서 알 수없는 nxn 행렬을 읽고 특정 방식으로 행렬식을 계산하는 프로그램을 만드는 것입니다. 나는 파일에서 파일을 가져온 뒤 번호가 뒤죽박죽 인 것만 제외하고는 거의 끝났습니다.C에서 파일에서 행렬 얻기
내 코드를 살펴보면이 부분이 매트릭스를 읽은 직후까지 나왔다. 내가 말했듯이 그 값은 혼란 스럽다. dim 값이 0부터 시작하므로 올바른 횟수만큼 실행해야하므로 i가 0이되지 않습니다. < = 희미합니다.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char* argv[])
{
FILE *input;
int i, j, temp;
int dim=0;
double det;
const char inp_fn[]="matrix.dat";
/*Open File*/
input = fopen(inp_fn, "r");
/*Find the number of lines and hence dimensions*/
while (EOF != (temp = fgetc(input)))
{
if (temp=='\n')
{
++dim;
}
}
/*Reset pointer to beginning of file and float the matrix*/
fseek(input, 0, SEEK_SET);
float matrix[dim][dim];
/*Check file isn't NULL, if good fill the matrix with the values from the file*/
if((input != (FILE*) NULL))
{
for(i=0; i<=dim; i++)
{
for(j=0; j<=dim; j++)
{
fscanf(input, "%f", &matrix[i][j]);
}
}
fclose(input);
}
else
{
printf("Could not open file!\n");
}
너희들이 뭔가를 볼 수 있다면 나에게 말해줘, 난 정말이게 정말 새롭다. 그래서 나는 분명히 뭔가를 잃어 버렸어. 고마워.
파일의 마지막 줄 뒤에 '\ n'이 있습니까? – Inspired
또한 오류가 발생하지 않았지만 행 수를 세는 첫 번째 루프 앞에'input! = (FILE *) NULL '검사를 수행해야합니다. – Inspired
루프에서 범위를 벗어났습니다. '<='대신 '<'를 사용하십시오. – dnk