fgets & storing

This is a discussion on fgets & storing within the C Programming forums, part of the General Programming Boards category; hi all , i just have a problem concerning fgets if i use fgets in a linked list structure fgets ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    13

    fgets & storing

    hi all , i just have a problem concerning fgets

    if i use fgets in a linked list structure

    fgets (element->name,MAX,stdin);

    where "name" is an element of struct~


    how can i store the input from stdin , without , storing the newline character into the linked list??

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > how can i store the input from stdin , without , storing the newline character into the linked list??
    You can't - fgets comes with a newline appended.

    So you have to get rid of it after calling fgets

    Here's an easy way
    Code:
    char *p = strchr( element->name, '\n' );
    if ( p != NULL ) *p = '\0';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 02:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21