LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2007, 06:53 AM   #1
Mr Tummyfluff
LQ Newbie
 
Registered: Aug 2006
Location: Southampton
Distribution: Solaris 5.8, Red Hat Enterprise 3, OS X 10.4
Posts: 10

Rep: Reputation: 0
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
 
Old 03-25-2007, 07:33 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 03-25-2007, 08:18 AM   #3
Mr Tummyfluff
LQ Newbie
 
Registered: Aug 2006
Location: Southampton
Distribution: Solaris 5.8, Red Hat Enterprise 3, OS X 10.4
Posts: 10

Original Poster
Rep: Reputation: 0
almost there!

Doh! Sorry, my c's a touch on the rusty side.

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.
 
Old 03-26-2007, 09:19 AM   #4
KenJennings
LQ Newbie
 
Registered: Mar 2005
Location: FL, USA
Distribution: SuSE
Posts: 28

Rep: Reputation: 15
Quote:
Originally Posted by Mr Tummyfluff
Doh! Sorry, my c's a touch on the rusty side.
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.
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple c code but whenever running getting segmentation fault with getch() invincible.rish Linux - General 1 01-26-2007 06:55 AM
when code tries to execute at that time i am getting segmentation fault dayalan_cse Programming 1 12-17-2006 10:30 AM
code for Segmentation Memory alirezadotnet Programming 10 05-08-2005 04:06 AM
dpkg returned an error code (1) Segmentation fault questionman Debian 6 11-02-2004 12:04 AM
segmentation fault in following peice of code :please help linorg Programming 2 03-18-2004 08:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration