LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2003, 06:42 PM   #1
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
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.
 
Old 12-18-2003, 08:00 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 12-18-2003, 08:51 PM   #3
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
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.
 
Old 12-18-2003, 11:17 PM   #4
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
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..

Tarts
 
Old 12-19-2003, 01:51 PM   #5
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 12-19-2003, 07:28 PM   #6
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
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
 
Old 12-19-2003, 08:51 PM   #7
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 12-19-2003, 08:53 PM   #8
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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
 
Old 12-19-2003, 09:06 PM   #9
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
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

Last edited by Tarts; 12-19-2003 at 09:31 PM.
 
Old 12-19-2003, 09:10 PM   #10
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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
 
Old 12-19-2003, 09:12 PM   #11
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
 
Old 12-19-2003, 09:20 PM   #12
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
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

Last edited by Tarts; 12-19-2003 at 09:21 PM.
 
Old 12-19-2003, 09:22 PM   #13
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
yes there u go. now do that to ur code and ur good to go you do understand what the problem was?
 
Old 12-19-2003, 09:40 PM   #14
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
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
 
Old 12-19-2003, 09:43 PM   #15
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php fgets really slow pasholy2001 Programming 0 04-22-2005 02:17 AM
reading in string with fgets bostonbanana Programming 4 12-12-2004 02:10 PM
Is fgets functions secure? mullog Programming 2 10-02-2004 01:22 PM
fgets() problems in C AMMullan Programming 4 03-12-2004 04:39 AM
fgets vs gets cxel91a Programming 2 12-01-2003 12:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration