Thread: Remove the newline from buffer

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    21

    Remove the newline from buffer

    Hi

    I've done fgets and store it but the thing is i dont want the newline it automatically stores. Any idea how to remove it? or not get it in the first place?

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about searching the string for the newline and replacing that character with something like a null or a space or what not? Did you give it any thought at all? Or should we just do everything for you forever?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
    mystring[strlen(mystring) - 1] = '\0';

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > mystring[strlen(mystring) - 1] = '\0';
    Except fgets() doesn't guarantee that the last char in the buffer will be a \n
    If the buffer fills, you won't have a \n, though you will always have a \0

  6. #6
    I agree.

    There are many ways to remove the last '\n' occurance from a buffer, safely. Instead of explaining all of the possibilities, here is a thread that discusses this matter in great detail: Using fgets, removing last character


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. flushall()
    By salvadoravi in forum C Programming
    Replies: 21
    Last Post: 12-24-2007, 12:39 PM
  2. Replies: 15
    Last Post: 10-31-2005, 08:29 AM
  3. DirectSound - multiple sounds
    By Magos in forum Game Programming
    Replies: 9
    Last Post: 03-03-2004, 04:33 PM
  4. Writing past ...
    By Ana Val sazi in forum C++ Programming
    Replies: 8
    Last Post: 06-29-2002, 08:43 AM
  5. Does anyone Know How to..?
    By kwigibo in forum C Programming
    Replies: 12
    Last Post: 09-20-2001, 08:16 AM