fgets and new line

This is a discussion on fgets and new line within the C Programming forums, part of the General Programming Boards category; I stored a string in a node using fgets. Its fine that the string has the new line character in ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    9

    fgets and new line

    I stored a string in a node using fgets.
    Its fine that the string has the new line character in it.
    I just want to print the string without the newline character.
    I dont want to change the data in the string, so i use strdup to copy the string before anything happens
    What I do stuffs the memory:

    EDIT: I fixed the problem by myself. What I did was really silly and nobody should ever know what that was
    Last edited by 9988776655; 01-04-2008 at 11:30 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    And your question is?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    strdup isn't a standard function.
    Plus, you don't call free(str) when you're done.

    Look up what something like "%.*s" means for printf.
    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.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Salem View Post
    strdup isn't a standard function.
    Depends which standard you mean. It's POSIX, and Windows has it, so that covers a lot of ground. I'd say use it, and if you hit the odd platform that doesn't have it, implement it yourself.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    > Depends which standard you mean. It's POSIX, and Windows has it
    There's only one standard which can be mentioned here without qualification, and that's the ISO C (and C++) standards.
    Although it's good to mention specific versions of these standards when discussing the finer points of detail.

    But that's just my opinion
    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

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