Remove \0 from a string

Discussion in 'C Programming' started by collinm, Apr 11, 2005.

  1. collinm

    collinm Guest

    hi


    here my code

    FILE *fp;
    char *line[2];
    #define LINE_MAX 30

    fp = fopen("test1.txt", "r");
    if ( ( line[0] = malloc( LINE_MAX) + 1) == NULL )
    exit( EXIT_FAILURE );

    fgets(line[0], LINE_MAX, fp);
    fclose(fp);

    fp = fopen("test2.txt", "r");
    if ( ( line[1] = malloc( LINE_MAX) + 1) == NULL )
    exit( EXIT_FAILURE );

    fgets(line[1], LINE_MAX, fp);
    fclose(fp);


    are there a way to remove \0 for line[0] and line[1]?

    thanks
    collinm, Apr 11, 2005
    #1
    1. Advertising

  2. collinm

    Al Bowers Guest

    collinm wrote:

    > hi
    >
    >
    > here my code
    >
    > FILE *fp;
    > char *line[2];
    > #define LINE_MAX 30
    >
    > fp = fopen("test1.txt", "r");
    > if ( ( line[0] = malloc( LINE_MAX) + 1) == NULL )
    > exit( EXIT_FAILURE );
    >
    > fgets(line[0], LINE_MAX, fp);
    > fclose(fp);
    >
    > fp = fopen("test2.txt", "r");
    > if ( ( line[1] = malloc( LINE_MAX) + 1) == NULL )
    > exit( EXIT_FAILURE );
    >
    > fgets(line[1], LINE_MAX, fp);
    > fclose(fp);
    >
    >
    > are there a way to remove \0 for line[0] and line[1]?
    >


    Are you sure you want to remove the '\0' character?
    Doing this may render the object to not be a string.
    It is common to desire to remove the newline char, '\n'.
    Perhaps that is what you want to do.
    One way:
    #include <string.h>
    char *s;

    if((s = strrchr(line[0],'\n')) != NULL) *s = '\0';

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
    Al Bowers, Apr 11, 2005
    #2
    1. Advertising

  3. collinm

    Eric Sosman Guest

    collinm wrote:
    > hi
    >
    >
    > here my code
    >
    > FILE *fp;
    > char *line[2];
    > #define LINE_MAX 30
    >
    > fp = fopen("test1.txt", "r");
    > if ( ( line[0] = malloc( LINE_MAX) + 1) == NULL )
    > exit( EXIT_FAILURE );
    >
    > fgets(line[0], LINE_MAX, fp);
    > fclose(fp);
    >
    > fp = fopen("test2.txt", "r");
    > if ( ( line[1] = malloc( LINE_MAX) + 1) == NULL )
    > exit( EXIT_FAILURE );
    >
    > fgets(line[1], LINE_MAX, fp);
    > fclose(fp);
    >
    >
    > are there a way to remove \0 for line[0] and line[1]?


    I am not sure what you are asking, but it *might*
    be about one or another of these issues:

    - The malloc() calls and the way you test for their
    success are wrong. Look carefully: You ask malloc() for
    LINE_MAX bytes, then you add 1 to whatever value malloc()
    returns, then you store the sum in line[0] or line[1],
    and finally you compare the sum to NULL. If malloc()
    succeeds, line[0] or line[1] points to the second byte
    of the allocated area, which is probably not what you
    want -- in particular, you need to write free(line[0]-1)
    when you release the area. If malloc() fails, adding 1
    to NULL produces undefined behavior -- the most likely
    outcome is that you will get a sum that is not equal to
    NULL but is useless because "it doesn't point anywhere."

    - You might have intended malloc(LINE_MAX + 1) instead
    of malloc(LINE_MAX) + 1; if so, that would be correct from
    the standpoint of memory management. However, it would be
    unnecessary because fgets() will never attempt to store
    anything in that "extra" byte. The second argument to
    fgets() is the entire length of the buffer *including*
    space for the '\0' fgets() will store; fgets() will store
    at most LINE_MAX characters *including* the '\0'.

    - When you say you want to "remove \0," do you mean
    that you want to eliminate the "+1" from the allocations?
    If that's it, go ahead: as mentioned above, fgets() will
    not store more bytes than its second argument indicates.

    - ... or do you actually want to "remove" the '\0'
    that fgets() stores after the end of each input line?
    That seems a very stupid and dangerous thing to do, but
    if you really want you can overwrite the '\0' with a
    different character:

    *strchr(line[0], '\0') = 'X';

    - ... or do you want to remove the '\n' (not '\0')
    at the end of the input line? That makes more sense,
    since the trailing '\n' is often not wanted. You need
    to be just a little bit careful, though, because if the
    input line is too long for the buffer fgets() will store
    only the first part of it and there will be no '\n' to
    "remove." Here's one way to do it:

    char *p;
    ...
    p = strchr(line[0], '\n');
    if (p != NULL)
    *p = '\0';

    .... and here's a shorter alternative using a less familiar
    function:

    line[0][ strcspn(line[0], "\n") ] = '\0';

    - Finally, it is a good thing that you are careful to
    check for failure of malloc(), even though you have done
    it incorrectly. However, it is a bad thing that you have
    *not* checked for failure of fopen() or for failure of
    fgets()! High-tech motion-sensing locks on your attic
    windows do not improve the security of your house if you
    leave the front door hanging open.

    --
    Eric Sosman, Apr 11, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    403
    Hans Kesting
    Jul 8, 2004
  2. =?Utf-8?B?SklNLkgu?=

    Q: remove a certain string in string

    =?Utf-8?B?SklNLkgu?=, Feb 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    334
    Joshua Flanagan
    Feb 28, 2005
  3. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,353
    Henrik
    Aug 10, 2005
  4. tshad
    Replies:
    6
    Views:
    21,438
    tshad
    Aug 8, 2006
  5. soni29

    remove a string from within a string

    soni29, Aug 5, 2004, in forum: Javascript
    Replies:
    2
    Views:
    80
Loading...

Share This Page