I think my problem with my code that the file is not being passed correctly. The input is a file with three lines 1 2 3; 4 5 6; 7 8 9; and the output is a Segmentation fault (core dumped), the output is supposed to print the first line 1 2 3.
#include <stdio.h>
#include <stdlib.h>
int getNum();
int getLine();
int getMatrix();
int det1();
int det2();
int det3();
int det4();
int det5();
int det6();
main(){
FILE *infile;
infile = fopen("matrix.txt","r");
int line[6];
int lineSize;
int error;
getLine(line,lineSize,infile);
printf("%d %d\n", line[0],line[1]);
fclose(infile);
}
/***********************************************
Name : getLine
Description : To get the line of numbers
Arguments : infile - the file pointer with numbers inside
line[] - the line of numbers
lineSize - size of line
Returns : 1 - If no errors were encountered
2 - If END OF FILE was reached
-1 if non number detected
*************************************************/
int getLine(int line[], int lineSize, FILE *infile){
int value;
int l;
lineSize=0;
while(value != '\n'){
value=0;
l=getNum(value,*infile);
if (value==EOF){
return(2);
}
line[lineSize]=value;
lineSize++;
}
if (l == -1){
return(-1);
}
return(1);
}
/***********************************************
Name : getNum
Description : To get the Next number from file
Arguments : infile - the file with numbers inside
value - the value of number grabed
Returns : 1 - If no errors were encountered
-1 - If letter or non number detected
*************************************************/
int getNum(int value, FILE *infile){
int c;
int error=1;
while ((c=getc(infile)) != EOF){
if (c=='\n'){
value = '\n';
return(1);
}
if(c==32){//checking for space
if (error == -1){
return(-1);
}
else{
return(1);
}
}
else {
value = 10*value + c - '0';
}
if((c<=47)||(c>=58)){
printf("incorrect number input %d\n",c);
error = -1;
}
}
value = EOF;
return(1);
}