tags:

views:

69

answers:

3

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);

}
+2  A: 

In getLine(), when you give the infile FILE* to the getNum() function, you dereference it:

 l=getNum(value,*infile);

But getNum() would just expect a normal FILE*, not a dereferenced one. So pass infile to that function unchanged:

 l=getNum(value,infile);

Additionally, the while(value != '\n') loop will probably run forever, writing past the end of the lines array until you get a segmentation fault. value, which is controlling when the loop will terminate, is never modified (also it isn't initialized, making it start with an arbitrary value). The getNum() function, which probably is supposed to modify value, gets a copy of the integer passed as a parameter and then modifies this copy. The original value is never changed.

If you want the function to change the value variable you have to use a pointer that points to value and that is used to modify that variable:

int getNum(int *value, ...) {
   *value = 5;
   ...
}

l=getNum(&value, infile);

Also it is a little dubious that value, an integer variable, is assigned and compared against '\n', a character literal. Are you sure you want to use the integer value of '\n' as a termination condition of your loop?

sth
I fixed that, yet I still get a segmentation fault
John
@John: I added some more stuff to my answer.
sth
A: 

While not a direct answer, I'd recommend sticking a number of printf statements in at random spots; that will let you narrow down the exact point of the crash relatively quickly. Move them around until you have two printfs bracketing a single line of code that you then know to be the crashing culprit, which will let you diagnose better.

RonLugge
Thanks, I think I have narrowed it down to value, it is changed in getNum, but once back in getLine it doesn't keep the value it was changed to in getNum.
John
RonLugge
+2  A: 

Skimming your code ...

int getNum();
int getLine();
int getMatrix();
int det1();
/* ... */

These declarations say to the compiler: "hey compiler, please be aware I'll be calling functions with these names (getNum, getLine, getMatrix, det1, ...) and they return int, but I'm not telling you what parameters they accept. Just trust me when I use them"

It's better if you use the prototype right when you introduce the functions to the compiler

int getNum(int value, FILE *infile);
int getLine(int line[], int lineSize, FILE *infile);
/* ... */

These declarations say to the compiler: "hey compiler, please be aware I'll ba calling function with these names, they return int and accept these parameters. If I make a mistake, do complain to let me know of my mistake"

... continuing inside main()

      /* ... */
      int lineSize;
      int error;
      getLine(line,lineSize,infile);
      /* ... */

you declared lineSize but didn't provide a value for the variable. When the program calls getLine, the value for lineSize is almost certainly the wrong value (it might even make your computer crash even before calling the function). Initialize (almost) all variables before using them.

      /* ... */
      int lineSize = 0;
      int error = 0;
      getLine(line,lineSize,infile);
      /* ... */

I haven't skimmed more ...

Suggestion: crank up your compiler warning level and do not run your program while compilation produces warnings.

pmg