fgets
when fgets reads a line, it reads in the '\n' too.
is there a way to use fgets so that it stops reading when it encounters the newline? thanks. |
what you could do is push the newline back into the input stream, not sure if that helps you out? i think you use putc(the_char, stdin); to do that... but dont quote me on that i as dont have my advanced programming unix env book handy, but i think that's the function you use.
|
thanks infamous. i wasnt able to get online till now, and ended up doing it some other way, where i dont have to look at the newline.
|
Re: fgets
Quote:
Tarts |
actually you are going to overflow your buffer with that code. fgets guarantees a NULL byte at the end of the buffer, so if you try to read in a line that is longer than your buffer, the last byte will not be a '\n' so your while loop is going to overrun.
|
Quote:
Code:
char buf[100]; Code:
char buf[100]; |
clearly you didnt read my post close enuf. the WHILE loop is going to overrun your buffer b/c the condition while(s[i] != '\n') will never be true if fgets doesnt include the newline in your buffer.
|
Code:
[n00b@highjack3d] cat fgetsbuf.c |
Try running this code, tell me:
A.) does it seg fault? B.) How you can get it to seg fault. Code:
#include <stdio.h> |
well if u insist....
Code:
[n00b@highjack3d] cat blah.c |
|
I fixed your code as follows, it may be bad form...
Code:
#include <stdio.h> |
yes there u go. now do that to ur code and ur good to go :) you do understand what the problem was?
|
Quote:
Quote:
Thanks infamous41md! Tarts |
cool glad u gots it. actually, the man page is lacking b/c it doesnt tell u that. only reason i know is b/c i read it in Stevens Advanced Programming Unix Env.
|
All times are GMT -5. The time now is 07:43 AM. |