Results 1 to 6 of 6
HI All
I am new to c programming on linux. I've tried a simple program on file. It works fine in windows environment (Turbo c). But whenever I try to ...
- 11-04-2008 #1Just Joined!
- Join Date
- Nov 2008
- Posts
- 2
Segmentation error in cprogram
HI All
I am new to c programming on linux. I've tried a simple program on file. It works fine in windows environment (Turbo c). But whenever I try to run it in linux I am getting error "Segmentation Fault".
#include<stdio.h>
#include<stdlib.h>
struct std
{
char sname[10];
int rno;
char class[10];
};
struct emp
{
char ename[10];
int eid;
char desg[10];
};
int main()
{
FILE *fp,*fs;
char ch=' ';
struct std s;
struct emp e;
fp=fopen("stdu","wb");
if(fp==NULL)
{
printf("\n can not open file");
exit(0);
}
do
{
printf("\n enter student details:");
printf("\n enter student name, number and class");
scanf("%s %d %s",s.sname,&s.rno,s.class);
fwrite(&s,sizeof(s),1,fp);
printf("\n do you want to continue(y/n)");
fflush(stdin);
scanf("%c",&ch);
printf("u r in the student details\n");
}while(ch == 'y');
fclose(fp);
fs=fopen("employee","wb");
if(fs==NULL){
printf("\n CAN NOT OPEN FILE");
exit(0);
}
do
{
printf("\n enter employee details");
printf("\n enter ename,eid and designation");
scanf("%s %d %s",e.ename,&e.eid,e.desg);
fwrite(&e,sizeof(e),1,fs);
printf("\n Do you want to continue(y/n)");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='y');
fclose(fs);
return 0;
}
Pl help me If I went wrong in my code.
Thanks in advance
Satya
- 11-04-2008 #2Just Joined!
- Join Date
- Nov 2008
- Posts
- 3
You need to say what you were doing when you got the segmentation fault, but without knowing anything else about how you're running the program, I can see an issue with your structure arrays:
sname, class, ename, and desg are limited to 9 characters. If you entered strings longer than 9 characters, you would probably get a segmentation fault since your character arrays would be too small and you would overwrite them.Code:struct std { char sname[10]; int rno; char class[10]; }; struct emp { char ename[10]; int eid; char desg[10]; };
You should add a length check on your input, or alternatively increase the size of those arrays to something like 1024 chars...
- 11-04-2008 #3Linux Newbie
- Join Date
- Jan 2008
- Location
- UK
- Posts
- 211
Hi,
I agree with rrojas1979 about the size of arrays, also I have copied your code & compiled it run it and cannot get a segmentation fault yet.
Although it does behave oddly depending on what you input.
wowbag1
- 11-05-2008 #4Just Joined!
- Join Date
- Nov 2008
- Posts
- 2
I am able to read the student details successfully, but when I try to read the employee details I am gettin segmentation fault error
I am trying with strings lower than 9 characters anyhow...
I have changed the string size to 1024 but still the problem arising...
any help..
Thanks-Satya
- 11-05-2008 #5
What does gdb say about where your program gets the SIGSEGV?
--
Bill
Old age and treachery will overcome youth and skill.
- 11-06-2008 #6Just Joined!
- Join Date
- Nov 2008
- Posts
- 5
Hi,
How you are compiling your program is importent thing here...post that o/p to here..will guess error easily...
otherwise use GDB..
rgds,
gemlin


Reply With Quote