Thread: How to discard the rest of the characters in a string?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    37

    How to discard the rest of the characters in a string?

    Dear all:

    I use fopen() and fgets() to read from a file line by line. I would like to extract the string in between, for example:

    2013-123-00:12:11 xxxxxxxxxxxxxxxxx

    "00:12:11" is the part I need to extract, any idea how I should go about it?

    I can't use strstr() because the time changes in every line and I tried using strtok() but the result is not what I want.

    Any help is appreciated, thanks.

    hc

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So what did you try with strtok()?
    Post your attempt, and then we can tell you what you did wrong (so you learn).

    > 2013-123-00:12:11 xxxxxxxxxxxxxxxxx
    If you use " -" as the delimiter, then you extract in 3 calls
    2013
    123
    00:12:11
    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.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I would use scanf for this. It has the advantage that it can tell you if a line is not properly formatted (OR if your format string is not specified correctly).

    Example:
    Code:
    char buf[10000];
    while (fgets(buf, 10000, stdin) != NULL) {    
        char mytime[100];
        if (sscanf(buf, "%*[^-]-%*[^-]-%99[^ ]", mytime) == 1) {
            printf("Mytime: %s\n", mytime);
        }
        else {
            printf("Unrecognized string!\n");
        }
    }

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    37
    Quote Originally Posted by Salem View Post
    So what did you try with strtok()?
    Post your attempt, and then we can tell you what you did wrong (so you learn).

    > 2013-123-00:12:11 xxxxxxxxxxxxxxxxx
    If you use " -" as the delimiter, then you extract in 3 calls
    2013
    123
    00:12:11
    I tried with the delimiter ":" and the result was

    2013-123-00
    12
    11

    I'll take your suggestion using -, thanks.

    hc

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    37
    This works, thanks, but there were some weird numbers in between, I'll fix it, thanks very much.

    hc

    Quote Originally Posted by c99tutorial View Post
    I would use scanf for this. It has the advantage that it can tell you if a line is not properly formatted (OR if your format string is not specified correctly).

    Example:
    Code:
    char buf[10000];
    while (fgets(buf, 10000, stdin) != NULL) {    
        char mytime[100];
        if (sscanf(buf, "%*[^-]-%*[^-]-%99[^ ]", mytime) == 1) {
            printf("Mytime: %s\n", mytime);
        }
        else {
            printf("Unrecognized string!\n");
        }
    }

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    37
    Hi, Guys, problem solved, below is the source code, in case anyone running into the same problem, any suggestion to make it "prettier" is appreciated, thanks again.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define SIZE 50
    #define CROPPED_SIZE 10
    
    /* function prototype */
    char * RemoveColon (char *dest, const char *src);
    
    int main (int argc, char **argv) {
            /* define buffer space for the file */
            char string[SIZE];
            char croppedString[SIZE];
            FILE *pfile;
            int i, previousNum=0, counter=0, timeGap;
            char mytime[CROPPED_SIZE];
            char *croppedTime = mytime;
    
            /* open the existing file from input */
            pfile = fopen (argv[1], "r");
    
            while (fgets (string, SIZE, pfile) != NULL) {
                    strcpy (croppedString, string);
                    /* using sscanf to extract the string */
                    if (sscanf(croppedString, "%*[^-]-%*[^-]-%20[^ ]", mytime) == 1) {
                            /* zero out the unwanted characters */
                            for (i=18; i<SIZE; i++) {
                                    croppedString[i] = '\0';
                            }
                            croppedTime = RemoveColon (croppedTime, mytime);
                            int convertedNum = atoi(croppedTime);
                            timeGap = convertedNum - previousNum;
                            if (timeGap < 0) {
                                    convertedNum = convertedNum + 240000;
                                    timeGap = convertedNum - previousNum;
                            }
                            if ((timeGap >= 10000) && (previousNum != 0)){
                                    counter++;
                                    printf("%s\n", string);
                            }
                            previousNum = convertedNum;
    
                    }
            }
    
            fclose (pfile);
    
            return 0;
    }
    
    
    char * RemoveColon (char *new, const char *str) {
            const char *p;
            char *q;
            for (p=str, q=new; *p != '\0';) {
                    if (*p != ':') {
                            *q = *p;
                            p++;
                            q++;
                    } else {
                            p++;
                    }
            }
            *q = '\0';
            return new;
    }
    Thanks.

    hc

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
                            /* zero out the unwanted characters */
                            for (i=18; i<SIZE; i++) {
                                    croppedString[i] = '\0';
                            }
                            croppedTime = RemoveColon (croppedTime, mytime);
                            int convertedNum = atoi(croppedTime);
    I don't know why you need any of this.
    hh:mm:ss should be stored in mytime, as a direct result of the sscanf call.
    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.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    37
    Because I need to be able to compare and subtract the time with the previous string (record) ...

    Quote Originally Posted by Salem View Post
    Code:
                            /* zero out the unwanted characters */
                            for (i=18; i<SIZE; i++) {
                                    croppedString[i] = '\0';
                            }
                            croppedTime = RemoveColon (croppedTime, mytime);
                            int convertedNum = atoi(croppedTime);
    I don't know why you need any of this.
    hh:mm:ss should be stored in mytime, as a direct result of the sscanf call.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not entirely sure what output you want but you're working too hard to get it.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void) {
    
       char s[]={"2013-123-00:12:11 xxxxxxxxxxxxxxxxx"};
       char raw[10]={""};
       char data[10]={""};
       int i,j,n1,n2;
       sscanf(s, "%d%d%s",&n1,&n2,raw);
    
       printf("%d %d raw: %s\n",n1,n2,raw);
       for(i=0,j=0;raw[i];i++) {
          if(isdigit(raw[i])) {
             data[j]=raw[i];
             ++j;
          }
       }
       data[j]='\0';
       printf("\ndata: %s\n",data);
    
       return 0;
    }

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    37
    Yes, you are right ...

    I changed it and it looks better now, thanks for the tip!

    hc


    Quote Originally Posted by Adak View Post
    I'm not entirely sure what output you want but you're working too hard to get it.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void) {
    
       char s[]={"2013-123-00:12:11 xxxxxxxxxxxxxxxxx"};
       char raw[10]={""};
       char data[10]={""};
       int i,j,n1,n2;
       sscanf(s, "%d%d%s",&n1,&n2,raw);
    
       printf("%d %d raw: %s\n",n1,n2,raw);
       for(i=0,j=0;raw[i];i++) {
          if(isdigit(raw[i])) {
             data[j]=raw[i];
             ++j;
          }
       }
       data[j]='\0';
       printf("\ndata: %s\n",data);
    
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  2. discard characters after space
    By atif7865 in forum C Programming
    Replies: 3
    Last Post: 11-27-2008, 08:35 PM
  3. Discard wrong data from stream
    By mesmer in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 02:30 AM
  4. String Reversal (kinda different from the rest)
    By actionwillspeak in forum C Programming
    Replies: 19
    Last Post: 02-17-2005, 01:32 PM
  5. Concatenating: characters->string->vector (string) :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2002, 01:14 PM