Unix questions - Segmentation Fault

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > October 2005 > Segmentation Fault





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Segmentation Fault
hirenshah.05@gmail.com

2005-10-24, 3:46 pm

in following program i am getting segmentation fault. i am not able
find out the problem.

---------------------------------------------------------

#include <stdio.h>
#include <string.h>

struct record
{
char lname[10];
char fname[10];
int phno;
char deptname[10];

};

struct record r[10];

main(int argc, char *argv[])
{




FILE * fp;
char buf[500] , num[3];
char *p;
int i=1;

fp = fopen( "biology.dat" , "r" );

while(feof(fp)==0)
{
fscanf(fp ,"%s", buf);
strcpy(r[i].lname,buf);
fscanf(fp ,"%s", buf);
strcpy(r[i].fname,buf);
fscanf(fp ,"%s", buf);
r[i].phno=atoi(buf) ;
fscanf(fp ,"%s", buf);
strcpy(r[i].deptname,buf);
i++;
}


fclose(fp);
for (i=1;i<=12;i++)
{
printf("\n%s" , r[1].lname);
printf("\n%s" , r[2].fname);
printf("\n%s" , r[7].lname);
printf("\n%d" , r[8].phno);
}

}



----------------------------------------------------------------

Chris F.A. Johnson

2005-10-24, 3:46 pm

On 2005-10-22, hirenshah.05@gmail.com wrote:
> in following program i am getting segmentation fault. i am not able
> find out the problem.
>
> ---------------------------------------------------------
>
> #include <stdio.h>
> #include <string.h>
>
> struct record
> {
> char lname[10];
> char fname[10];
> int phno;
> char deptname[10];
>
> };
>
> struct record r[10];
>
> main(int argc, char *argv[])
> {
>
>
>
>
> FILE * fp;
> char buf[500] , num[3];
> char *p;
> int i=1;
>
> fp = fopen( "biology.dat" , "r" );


Was fopen successful? You forgot to check.

> while(feof(fp)==0)
> {
> fscanf(fp ,"%s", buf);
> strcpy(r[i].lname,buf);
> fscanf(fp ,"%s", buf);
> strcpy(r[i].fname,buf);
> fscanf(fp ,"%s", buf);
> r[i].phno=atoi(buf) ;
> fscanf(fp ,"%s", buf);
> strcpy(r[i].deptname,buf);
> i++;
> }
>
>
> fclose(fp);
> for (i=1;i<=12;i++)
> {
> printf("\n%s" , r[1].lname);
> printf("\n%s" , r[2].fname);
> printf("\n%s" , r[7].lname);
> printf("\n%d" , r[8].phno);
> }
>
> }



--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
hirenshah.05@gmail.com

2005-10-24, 3:46 pm

hi,

this program was running. but suddenly it started giving segmentation
fault. i think there some problem of buffer overflow. but i am able to
solve it.

Chris F.A. Johnson

2005-10-24, 3:46 pm

On 2005-10-23, hirenshah.05@gmail.com wrote:
> hi,
>
> this program was running.


What program?

This is Usenet, not a web forum (though it is also bastardized on
several web sites). You cannot know whether the reader can see or
has seen the previous posts, or, if they have been seen, whether
the reader remembers what they were about.

When using groups.google.com to reply to a Usenet article (better
to use a real newsreader), click on "show options" at the top of
the article, then click on the "Reply" at the bottom of the
article headers. This will quote the previous message in the
accepted manner. Trim the parts of it that are not relevant to
your follow-up.

> but suddenly it started giving segmentation fault. i think there
> some problem of buffer overflow. but i am able to solve it.




--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Barry Margolin

2005-10-24, 3:46 pm

In article <1130024869.141843.291390@g47g2000cwa.googlegroups.com>,
"hirenshah.05@gmail.com" <hirenshah.05@gmail.com> wrote:

> in following program i am getting segmentation fault. i am not able
> find out the problem.


Which statement is getting the segmentation fault?

If you don't know, it means you haven't even tried running the program
under a debugger. If you're not willing to make the slightest effort at
debugging your own program, why should we?

> ---------------------------------------------------------
>
> #include <stdio.h>
> #include <string.h>
>
> struct record
> {
> char lname[10];
> char fname[10];
> int phno;
> char deptname[10];
>
> };
>
> struct record r[10];
>
> main(int argc, char *argv[])
> {
>
>
>
>
> FILE * fp;
> char buf[500] , num[3];
> char *p;
> int i=1;


Why are you starting at 1? C arrays start at 0.

>
> fp = fopen( "biology.dat" , "r" );
>
> while(feof(fp)==0)


What happens if there are more lines than elements of the r[] array?

> {
> fscanf(fp ,"%s", buf);
> strcpy(r[i].lname,buf);


Why don't you scan directly into lname, instead of using buf? What
happens if the name is longer than 9 characters?

> fscanf(fp ,"%s", buf);
> strcpy(r[i].fname,buf);


Same questions as before.

> fscanf(fp ,"%s", buf);
> r[i].phno=atoi(buf) ;


Why not use fscanf's %d to scan directly into phno?

> fscanf(fp ,"%s", buf);
> strcpy(r[i].deptname,buf);


Same questions as the first one.

> i++;
> }
>
>
> fclose(fp);
> for (i=1;i<=12;i++)
> {
> printf("\n%s" , r[1].lname);
> printf("\n%s" , r[2].fname);
> printf("\n%s" , r[7].lname);
> printf("\n%d" , r[8].phno);


Why are you printing fields from different records? Shouldn't these all
be r[i]? And in that case, why does i go up to 12, when there are only
10 elements in r[]? And what will happen if you try to print out more
elements of r[] than there were lines in the file?

> }
>
> }
>
>
>
> ----------------------------------------------------------------


--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com