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

J

jan247

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);
.....
 
A

August Karlstrom

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
 
K

Keith Thompson

jan247 said:
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>
 
P

pemo

jan247 said:
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.
 
R

Richard Heathfield

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.
 
K

Keith Thompson

pemo said:
jan247 said:
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.
 
M

M. Nejat AYDIN

jan247 said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top