Hey Guys,
I'm trying to read individual characters from a file, store them in two arrays then print out the arrays. The input file looks something like this
7
1 5
0 1
2 4
3 7
0 2
5 9
0 3
With the first number being the length of the arrays and the first column being the first array and the second array being the second column. When I run the code below I get a seg fault on the line arrOwner = fgetc(infile);

#include <stdio.h>

int main(void){ 
  int j,i,temp,numSegs = 0;

  FILE *infile = fopen("input.txt","r");
  fscanf(infile,"%d", &numSegs);	
  int arrOwner[numSegs];
  int arrLength[numSegs];	 	

  while(!feof (infile)){
	arrOwner[i] = fgetc(infile); 
	arrLength[i] = fgetc(infile); 
	i++;
 }

 while(numSegs > 0){
  printf("%d", arrOwner[j]);		
  printf("%d", arrLength[j]);		
  j++;
  numSegs--;
 }
}

Your array can't contain all the number. You'll save 2*numsegs to your array[numsegs].

fgetc returns char type but your array is int type.

Maybe that's the problem

Your array can't contain all the number. You'll save 2*numsegs to your array[numsegs].

fgetc returns char type but your array is int type.

Maybe that's the problem

Thanks for the tip on fgetc i changed the code around accordingly, however I am still getting the seg fault on the same line. But why can't the arrays contain all the numbers there are numSeg number*2 numbers but two arrays to store them in.

#include <stdio.h>

int main(void){ 
  int j,i,temp,numSegs = 0;

  FILE *infile = fopen("input.txt","r");
  fscanf(infile,"%d", &numSegs);	
  char arrOwner[numSegs];
  char arrLength[numSegs];
  printf("%d", numSegs);
	 	

 while(!feof (infile)){
	arrOwner[i] = fgetc(infile); //seg fault
	arrLength[i] = fgetc(infile); 
	i++;
 }

 while(numSegs > 0){
  printf("%c", arrOwner[j]);		
  printf("%c", arrLength[j]);		
  j++;
  numSegs--;
 }

fclose(infile);
}

Edited 7 Years Ago by flyballonfly: n/a

fgetc returns char type but your array is int type.
Maybe that's the problem

fgetc() returns an integer as it is prototyped

int fgetc ( FILE * stream );

flyballonfly> however I am still getting the seg fault on the same line.

int j,i,temp,numSegs = 0;

  FILE *infile = fopen("input.txt","r");
  fscanf(infile,"%d", &numSegs);	
  char arrOwner[numSegs];
  char arrLength[numSegs];
  printf("%d", numSegs);
	 	

 while(!feof (infile)){
	arrOwner[i] = fgetc(infile); //seg fault

The compiler needs to know the size of arrOwner and arrLenghth at compile time. It can not be assigned at run time. The only reason that it even compiles is that you set numSegs to zero and that's the size of the arrays when it gets into the while loop. There's not memory for then, thus the segmentation fault.

while(!feof (infile)){
    arrOwner[i] = fgetc(infile); //seg fault
    arrLength[i] = fgetc(infile);
    i++;
}

feof is not acceptable as control of a loop. The result might not be what you expected.
Nothing is preventing fgetc from over flowing the size of arrOwner and arrLenght.
Is it possible for the file to contain more that what those arrays can hold, even if they were declared correctly? The answer is yes.

Edited 7 Years Ago by Aia: n/a

Thank you Aia, for the information for the for loop control!

But about your comment
"The compiler needs to know the size of arrOwner and arrLenghth at compile time. It can not be assigned at run time."
Is there any way to dynamically assign the length of the arrays depending on the input? That was what I was trying to do with the code below.

fscanf(infile,"%d", &numSegs);	
  char arrOwner[numSegs];
  char arrLength[numSegs];

Edited 7 Years Ago by flyballonfly: n/a

Is there any way to dynamically assign the length of the arrays depending on the input? That was what I was trying to do with the code below.

fscanf(infile,"%d", &numSegs);	
  char arrOwner[numSegs];
  char arrLength[numSegs];

use malloc()

char *arrOwner
char *arrLength;
fscanf(infile,"%d", &numSegs);	
arrOwner = malloc(numSegs);
arrLength = malloc(numSegs);
This article has been dead for over six months. Start a new discussion instead.