Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Segmentation fault on end of loop... (getline) (http://www.velocityreviews.com/forums/t440661-segmentation-fault-on-end-of-loop-getline.html)

jan247 12-27-2005 03:15 AM

Segmentation fault on end of loop... (getline)
 
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);
.....


August Karlstrom 12-27-2005 03:29 AM

Re: Segmentation fault on end of loop... (getline)
 
jan247 wrote:
-snip-
> 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);

-snip-

You have to allocate memory for `line' before you can use getline:

line = malloc(nbytes + 1);
...


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.

Keith Thompson 12-27-2005 03:49 AM

Re: Segmentation fault on end of loop... (getline)
 
"jan247" <an2king_kid@yahoo.com> 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@mib.org <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.

pemo 12-27-2005 09:09 AM

Re: Segmentation fault on end of loop... (getline)
 

"jan247" <an2king_kid@yahoo.com> wrote in message
news:1135653352.740432.186580@g14g2000cwa.googlegr oups.com...
> 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);
> ....


See what the others have said, plus, use fgets instead of getline.



Richard Heathfield 12-27-2005 09:41 AM

Re: Segmentation fault on end of loop... (getline)
 
pemo said:

> See what the others have said, plus, use fgets instead of getline.


The two do not perform the same task. The getline function does a job which
no single standard library function can do. On the other hand, it isn't a
standard ISO C function. The decision about which to use is not as
clear-cut as you make it sound.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Keith Thompson 12-27-2005 10:03 AM

Re: Segmentation fault on end of loop... (getline)
 
"pemo" <usenetmeister@gmail.com> writes:
> "jan247" <an2king_kid@yahoo.com> wrote in message
> news:1135653352.740432.186580@g14g2000cwa.googlegr oups.com...
>> 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:
>>
>> ....

[snip]
>> while(getline( &line, &nbytes, fp ) != -1)

[snip]
>
> See what the others have said, plus, use fgets instead of getline.


fgets() is standard, while getline() is a GNU extension, so that's
good advice if you're concerned about portability. However, getline()
does have some advantages over fgets() (I won't go into the details),
so it's perfectly appropriate to use it if portability to non-GNU
implementations isn't a concern.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.

M. Nejat AYDIN 12-27-2005 06:12 PM

Re: Segmentation fault on end of loop... (getline)
 
jan247 wrote:
> 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");

You must check the value of fp here.
> char *line;
> int x=1;
> int nbytes = 100;
> while(getline( &line, &nbytes, fp ) != -1)
> {
> printf("%d: %s", x, line );
> x++;
> }
> fclose(fp);


getline is not a standard C function; it is a GNU extension,
but it is not too difficult to implement it in terms of standard
functions.

<OT>
You should set the line pointer to NULL, or a value returned from
a call to malloc, before the first call to getline. For example;
size_t nbytes = 0;
char *line = NULL;
or alternatively,
size_t nbytes = 100; /* any value should work */
char *line = malloc(nbytes);

Read manual page of getline for the details.
</OT>


All times are GMT. The time now is 03:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.