LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   fgets (https://www.linuxquestions.org/questions/programming-9/fgets-127077/)

h/w 12-18-2003 06:42 PM

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.

infamous41md 12-18-2003 08:00 PM

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.

h/w 12-18-2003 08:51 PM

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.

Tarts 12-18-2003 11:17 PM

Re: fgets
 
Quote:

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.

You could always read my sig.. :D

Tarts

infamous41md 12-19-2003 01:51 PM

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.

Tarts 12-19-2003 07:28 PM

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

Code:

char buf[100];
nlnuke(buf, sizeof(buf));

Tarts

infamous41md 12-19-2003 08:51 PM

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.

infamous41md 12-19-2003 08:53 PM

Code:

[n00b@highjack3d] cat fgetsbuf.c
#include <stdio.h>

int main()
{
        int    i = 0;
        char    buf[12];

        fgets(buf, sizeof(buf), stdin);

        while(buf[i] != '\n')
                i++;

        return 0;
}
[n00b@highjack3d] ./a.out
alksfjlsadjlskdjfslkdfjslkfjsldkfjslkfjasklfjlsakfj
Segmentation fault


Tarts 12-19-2003 09:06 PM

Try running this code, tell me:

A.) does it seg fault?

B.) How you can get it to seg fault.
Code:

#include <stdio.h>
#include <strings.h>

int nlnuke(char st[], size_t in)
{
        int i = 0;
        char s[in];
        if (fgets(s, sizeof(s), stdin) == NULL) {
                printf("fgets\n");
                return -1;
        }
        while (s[i] != '\n')
                i++;
        s[i] = '\0';
        strcpy(st, s);
        return 0;
}

int main()
{
        char name[100];
        printf("Please enter your name...\n");
        if (nlnuke(name, sizeof(name)) == -1) {
                printf("newlnuke\n");
                return -1;
        }
        printf("Your name is %s", name);
        return 0;
}

Tarts ;)

infamous41md 12-19-2003 09:10 PM

well if u insist....
Code:

[n00b@highjack3d] cat blah.c
#include <stdio.h>
#include <strings.h>


int nlnuke(char st[], size_t in)
{
        int i = 0;
        char s[in];
       
        if (fgets(s, sizeof(s), stdin) == NULL) {
                printf("fgets\n");
                return -1;
        }
        while (s[i] != '\n')
                i++;
        s[i] = '\0';
        strcpy(st, s);
        return 0;
}


int main()
{
        char    buf[12];
        size_t  sz = 12;

        nlnuke(buf, sz);

        return 0;
}
[n00b@highjack3d] ./a.out
asldfjaslkfjasldfjaslkfjaslk;fjasklfjaslkfjasl;fsf
Segmentation fault


infamous41md 12-19-2003 09:12 PM


Tarts 12-19-2003 09:20 PM

I fixed your code as follows, it may be bad form...

Code:

#include <stdio.h>

int main()
{
        int    i = 0;
        char    buf[12];

        fgets(buf, sizeof(buf), stdin);

        while(buf[i] != '\n' && buf[i] != '\0')
                i++;

        return 0;
}

Tarts

infamous41md 12-19-2003 09:22 PM

yes there u go. now do that to ur code and ur good to go :) you do understand what the problem was?

Tarts 12-19-2003 09:40 PM

Quote:

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.
*feels foolish*

Thanks infamous41md!

Tarts

infamous41md 12-19-2003 09:43 PM

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.