LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c code segmentation fuault error (https://www.linuxquestions.org/questions/programming-9/c-code-segmentation-fuault-error-540420/)

Mr Tummyfluff 03-25-2007 06:53 AM

c code segmentation fuault error
 
Hey,

I'm trying to write a c code to read a binary file so that I can edit its contents and spit it back out as again. The bit of code to read the file is given below:

#include<stdio.h>
#include<math.h>

/* open main program */
int main ()
{
/* define variables for looping round */
int s, x1, x2, nsa, n1m, n2m;
unsigned char *velinp;
char inmodel[100];
FILE *vscreen;

/* specify size of model */
nsa = 49;
n1m = 128;
n2m = 128;
/* read in command line input of model filename */
printf("\n Please enter model file: ");
scanf("%s", &inmodel[0]);

/* Open binary input file of screens */
vscreen=fopen(inmodel,"rb");
if (vscreen==NULL) {
printf("Cannot open binary input file\n");
exit(1);
}

/* read code grid in time, x1, and x2 */
for(s=0;s<nsa;s++){
for(x1=0;x1<n1m;x1++){
for(x2=0;x2<n2m;x2++){
fread(&velinp[x2+n2m*(x1+n1m*s)],sizeof(unsigned char),1,vscreen);
}
}
}
/* close model file */
fclose(vscreen);
/* close main program */
return 0;
}

What I'm reading is a binary file which describes the properties of a cube of squares nsa large in the s direction, n1m large in the x1 direction, and n2m in x2. Each of these squares will be described by an unsigned character byte (i.e. 0-255).

Everything compiles fine, but when I try to execute it it gets as far as the fread statement and spits out a segmentation fault error.

I realise this is probably going to be simple and I probably look like a moron, but any help would be much appreciated.

Cheers

jlliagre 03-25-2007 07:33 AM

Welcome to LQ :)

velimp variable isn't initialized.

Your code is missing something like:
Code:

velimp=malloc(size);
with size being large enough to hold all the elements.

Mr Tummyfluff 03-25-2007 08:18 AM

almost there!
 
Doh! Sorry, my c's a touch on the rusty side. :rolleyes:

Thanks, I added the following line but it's still kicking out an segementation fault when it reaches the fread line.

Code:

velinp=(unsigned char *)malloc(nsa*n1m*n2m*sizeof(unsigned char));
Any ideas.:scratch:

KenJennings 03-26-2007 09:19 AM

Quote:

Originally Posted by Mr Tummyfluff
Doh! Sorry, my c's a touch on the rusty side. :rolleyes:
Thanks, I added the following line but it's still kicking out an segementation fault when it reaches the fread line.
Code:

velinp=(unsigned char *)malloc(nsa*n1m*n2m*sizeof(unsigned char));
Any ideas.:scratch:

Dunno. It works here. I added a bunch of debugging to report the actual indexes and the multiplied offset and everything appeared to work properly here.

As written, the code doesn't even test for end of file, so reading past the end of a file smaller than the expected input continued "successfully" stuffing 0 bytes into the remainder of the velinp array.

Linux linux 2.6.13-15.15-default #1 Mon Feb 26 14:11:33 UTC 2007 i686 i686 i386 GNU/Linux
gcc version 4.0.2 20050901 (prerelease) (SUSE Linux)


All times are GMT -5. The time now is 05:32 PM.