Passing file pointer into functions, and file not being read correctly

John picture John · Oct 2, 2010 · Viewed 27.8k times · Source

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

}

Answer

pmg picture pmg · Oct 2, 2010

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.