0

How to extract only the date part from a char* ?

   char *file = "TEST_DATA_20141021_18002.zip"

   char *date ="20141021" 

Thanks!

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
Thulasi
  • 23
  • 6
  • 1
    If the format of that file name of yours is consistent you could do it pretty easily with `strtok` using `_` as the delimiter ... – dragosht Oct 16 '14 at 08:41
  • @dragosht gave you a good idea. After doing that, find a token that consists of 8 symbols of numbers, that's it. – yulian Oct 16 '14 at 08:43
  • @dragosht there are 2 types of file name. char *file ="TEST_DATA_BIG_20141021_1800211.zip" – Thulasi Oct 16 '14 at 08:46

1 Answers1

1
#include <stdio.h>
#include <ctype.h>

int main(){
    char *file = "TEST_DATA_20141021_18002.zip";
    char date[16];
    char *s = file, *d = date;
    while(!isdigit(*s))
        ++s;
    do{
        *d++ = *s++;
    }while(isdigit(*s));
    *d = 0;
    puts(date);
    //or
    sscanf(file, "%*[^0-9]%15[0-9]", date);//0-9 : 0123456789
    puts(date);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70