Re: Segmentation fault on end of loop... (getline)
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Tue, 27 Dec 2005 03:49:53 GMT
"jan247" <an2king_kid@xxxxxxxxx> writes:
> hi, im new here... um, this code works except that, at the end of the
> loop, i always get a "Segmentation fault". Can anyone help me on this?
>
> using GNU C:
>
> ....
> FILE *fp;
> fp = fopen("./prg.c", "r");
>
> char *line;
> int x=1;
> int nbytes = 100;
> while(getline( &line, &nbytes, fp ) != -1)
> {
> printf("%d: %s", x, line );
> x++;
> }
> fclose(fp);
> ....
You call fopen(), but you don't check whether it succeeded.
getline() is not a standard C function.
<OT>
You might look into what getline() does if its first argument is a
pointer to an uninitialized pointer object (the value of line is
garbage).
You might also look at the type of the second argument.
</OT>
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- Segmentation fault on end of loop... (getline)
- From: jan247
- Segmentation fault on end of loop... (getline)
- Prev by Date: Re: Segmentation fault on end of loop... (getline)
- Next by Date: A beginner's problem
- Previous by thread: Re: Segmentation fault on end of loop... (getline)
- Next by thread: Re: Segmentation fault on end of loop... (getline)
- Index(es):
Relevant Pages
|
|