Thread: Deleting first portion of a string

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Deleting first portion of a string

    Im reading from a file using fgets.

    And the file has the follwoing

    Date: the date
    Name: Bob...
    Year: 1923


    I use fgets to scan the line, and then display it in a text field. The problem is I dont want the 'Date:' displayed in the text field. How can I go about just taking the information I want?

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    char blurb[] = "Date: 10/05/83";
    char *p;
    
    puts (&blurb[5]);
    
    /* Or */
    
    p = strchr (blurb, ' ');
    if (p)
      puts(p);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Im using the first approach. Nice and easy.

    Thanks a lot.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    /* 
       remove all instances of 'pat' in 'str'.
       note: guaranteed not to return NULL.
       needs: string.h
    */
    
     char * remove(char * str, const char * pat)
    {
     char * ptr = str;
     
     int len = strlen(pat);
     
         while(0 != (ptr = strstr(ptr, pat)))
        {
         strcpy(ptr, ptr+len);
        } 
        
     return str;    
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>strcpy(ptr, ptr+len);
    This results in undefined behaviour, as the source and target strings overlap.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    i'm surprised anyone hasn't suggested using strtok
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
      char str[]="DATE: 27/02/2004";
      char *p;
      
      p = str;
      p = strtok (p,":");
      p = strtok (NULL,": ");
      puts(p);
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM