Thread: fgets and new line

  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-05-2008 at 12:30 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

Popular pages Recent additions subscribe to a feed