ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Originally posted by h/w 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.
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.
Originally posted by infamous41md 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.
The only way you can input a line longer than the buffer is if the programmer uses the function incorrectly, such as:
Code:
char buf[100];
nlnuke(buf, 1000);
I usually use 'sizeof()' to make sure that doesnt happen:
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.
Originally posted by infamous41md yes there u go. now do that to ur code and ur good to go you do understand what the problem was?
I'll venture a guess,
Quote:
originally posted by infamous41md
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.